当setNestedScrollingEnabled为false时,Recyclerview onscrolllistener无法正常工作

时间:2016-07-04 08:17:09

标签: android android-recyclerview android-nestedscrollview

我想要使用recyclerView实现分页,为此,我将addOnScrollListener添加到recyclerView,但是当我设置{{1}时,RecyclerView.OnScrollListener无法正常工作}}

但当我删除rvGridExplore.setNestedScrollingEnabled(false);时,它工作正常, 我不知道如何处理这件事。

这是代码:

rvGridExplore.setNestedScrollingEnabled(false);

6 个答案:

答案 0 :(得分:5)

这个问题可能已经过时了,但是为了帮助那些偶然发现这个问题的人,我想分享一下我的所作所为。我必须实现onScroll Listener到recyclerview来从服务器加载数据并进行一些UI更改。并且还需要swipeRefresh布局来刷新数据。

这是我的xml文件结构,

-RelativeLayout

 -SwipeRefreshLayout

  -NestedScrollView

   -LinearLayout(Vertical)

    -Multiple views required

在此之后,为了检测向上和向下滚动,我将setOnScrollListener实现为NestedScrollView。

正常使用SwipeRefreshLayout刷新数据。

为了加载更多数据,我在NestedScrollingView的onScrollListener中实现了逻辑。

if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()))  {

    // Load More Data
}

答案 1 :(得分:2)

如果您将recyclerView嵌入到任何 NestedScrollView 中,则应该将onScrollListener附加到NestedScrollView。

这可以工作!

int[] arr = null;

答案 2 :(得分:0)

第1步:创建EndlessRecyclerOnScrollListener

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    // use your LayoutManager instead
    private LinearLayoutManager llm;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager sglm) {
        this.llm = llm;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (!recyclerView.canScrollVertically(1)) {
            onScrolledToEnd();
        }
    }

    public abstract void onScrolledToEnd();
}

第2步:将滚动侦听器应用于回收站视图。

recyclerview.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
            @Override
            public void onScrolledToEnd() {
                Log.e("Position", "Last item reached");
                if (loadMore == true) {
                    // put your Load more code
                    // add 10 by 10 to tempList then notify changing in data
                }
            }
        });

答案 3 :(得分:0)

你在对你的问题的评论中说过"
它位于协调器布局下的NestedScrollView下,如果我删除它,工具栏不会向上滚动"。 这是一个错误

我发现您不能双管齐下,CoordinatorLayout行为会在您RecyclerView内有NestedScrollView的情况下中断行为。你需要使用其中一个。

RecyclerView内有NestedScrollView时,只要您设置RecyclerView.setNestedScrollingEnabled(false),它就会有效,但是当您发现这意味着OnScrollListener未被调用时

所有组件正常工作的唯一方法是删除NestedScrollView,确保不将嵌套滚动设置为false 并从那里开始工作。否则,RecyclerView.OnScrollListener事件将无法正确触发。

答案 4 :(得分:0)

使用线性或相对布局删除嵌套滚动视图,而不是将其用作根元素,则可以编写recyclerview.setNestedScrollEnabled(false);。

答案 5 :(得分:0)

将setOnScrollChangeListner添加到您的NestedScrollView

 nestedScrollview.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()))  {
               if(loading)
                onClick();
               loading=false;
            }
        }
    });

从服务器设置加载数据后,布尔值loading = true。