RecyclerView ScrollListener inside NestedScrollView

▼魔方 西西 提交于 2019-11-28 22:32:21

问题


I have an EndlessRecyclerView at the end of a NestedScrollView. EndlessRecyclerView means: when user scrolls to the bottom of the recyclerView it loads more data. This is already implemented and working elsewhere but when I put the recyclerView inside the NestedScrollView the OnScrollListener events doesn't fire.

XML design:

<NestedScrollView>

     <Other views/>

     <EndlessRecyclerView/>

</NestedScrollView >

Code:

recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            // This is never fired! Here is where I implement the logic of EndlessRecyclerView
        }
    });

How do I get scroll event for above case?

I know that is not good to have two scrollable views inside each other. But, how do I have the above case without having two scrollable views?

I already followed this link but it doesn't work: scroll event for recyclerview inside scrollview android


回答1:


To achieve endless scrolling for recycler view which is under NestedScrollView, you can use "NestedScrollView.OnScrollChangeListener"

nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if(v.getChildAt(v.getChildCount() - 1) != null) {
                if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                        scrollY > oldScrollY) {
                        //code to fetch more data for endless scrolling
                }
            }
        });

Here v.getChildCount() -1 should give you the recycler view for which you be implementing endless scrolling.

Also scrollY > oldScrollY confirms that the page is being scrolled down.

Reference: NestedScrollView.OnScrollChangeListener




回答2:


I had a similar issue, although it was a little different. In my case I had a recycleview within a fragment while the NestedScrollView was in the content_main xml (part of the activity).

I wrapped my recycleview that was within the fragment with SwipeRefreshLayout

This is the code of my fragment:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe_dummy"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/top_series_recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

The only thing is left to do is to disable the SwipeRefreshLayout from the code

mSwipeLayout.isEnabled = false

If you won't do that, when you swipe down it will show endless refresh icon. I wanted to share this solution in case someone will need this functionality or have this issue as well

After you will wrap the recycleview with a SwipeRefreshLayout, you will see addOnScrollListener of the recycleview will be called as usual



来源:https://stackoverflow.com/questions/39894792/recyclerview-scrolllistener-inside-nestedscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!