2

I use recyclerview inside a nestedscrollview as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/mainScrollView"
    android:layout_marginBottom="?attr/actionBarSize"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:overScrollMode="never"
        android:layout_height="250dp"
        android:focusableInTouchMode="true"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/VerticalRV"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</LinearLayout>

</android.support.v4.widget.NestedScrollView>

The problem is that when I want to set a listener (onScrollListener) to this recycler view, it doesn't get working anyway. I also have debugged this piece of code, but it doesn't even catch the event. Below is the java code:

recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
MyAdapter adapter = new MyAdapter(verticalShownData, this.getActivity());
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new HideShowScrollListener() {
     @Override
     public void onHide() {
          animateCallback.animateHide();
     }

     @Override
     public void onShow() {
          animateCallback.animateShow();
     }
});

How can I get this listener working? Thanks in advance.

inverted_index
  • 2,329
  • 21
  • 40
  • put view pager in a collapsingtoolbarlayout in the appbar, remove nestedscrollview and add this flag to recycler view app:layout_behavior="@string/appbar_scrolling_view_behavior" – ugur Aug 06 '17 at 16:13
  • I don't use appbar as you can see in the code. Anyway, could you please provide some pieces of codes to give me a clue? @uguboz – inverted_index Aug 06 '17 at 16:18
  • i don't have android studio installed now but you can follow https://antonioleiva.com/collapsing-toolbar-layout/ tutorial for achieving the same result. – ugur Aug 06 '17 at 16:33
  • you are disabling nested scrolling so AFAIK in this case only nested scrollview will scroll not recycler view. maybe removing recyclerView.setNestedScrollingEnabled(false) may work – ugur Aug 06 '17 at 16:38

2 Answers2

0

I know this might be little late but maybe this will help someone. I was also struck in a problem where my recyclerview keeps calling onScrolled method of RecyclerView as soon as data is inserted into RecyclerView. After some debugging I found that, I'm using NestedScrollView with RecyclerView. So I searched for something related to it and I found a great medium.com tutorial here. It works pretty fine.

Adding some code from link here.

mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        if(v.getChildAt(v.getChildCount() - 1) != null) {
            if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                    scrollY > oldScrollY) {

                int visibleItemCount = mLayoutManager.getChildCount();
                int totalItemCount = mLayoutManager.getItemCount();
                int pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (!isLoadingData()) {

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {

                         //Load Your Data
                    }
                }
            }
        }
    }
});
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
-1

After a couple of days, I finally come up with a solution.

The solution is removing the NestedScrollView and instead using RecyclerView with header. So the first element of the RecyclerView is a ViewPager and the rest are other objects.

I think that using NestedScrollView as parent of RecyclerView is not such a good practice. Since NestedScrollView forces its child to be fully loaded and this is against the concept of RecyclerView.

The solution is inspired by @hister's answer on this thread.

inverted_index
  • 2,329
  • 21
  • 40