嵌套回收器视图的滚动侦听器



我正在开发一个应用程序,其中我的主页填充了具有多个视图类型的回收器视图数据,其中一个水平回收器视图和两个垂直回收器视图。所以我在回收器视图中有Recyclerview。我想为第二个垂直回收器视图添加滚动侦听器,但无法添加,因为所有滚动都由主父回收器视图处理。我尝试在适配器内添加滚动器侦听器,它适用于水平回收器视图,但不适用于垂直视图。如何让它工作?

如果您在 nestedScrollView 中使用垂直回收器视图,则需要为 nestedScrollView 实现滚动侦听器,否则不会触发回收器视图的滚动侦听器。这是您基本上可以实现它的方法,您可以抓住在哪里加载更多数据以进行分页功能

private void implementScrollListener() {
    //Scroll Listener for nestedScrollView. Note that it is used for pagination when user reaches the bottom.
    if(nestedScrollView != null){
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > oldScrollY) {
                    //Scrolling down
                }
                if (scrollY < oldScrollY) {
                    //Scrolling up
                }
                if (scrollY == 0) {
                    //Reaches to the top
                }
                if (scrollY < (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    //Reaches to the bottom so it is time to updateRecyclerView!
                    updateRecyclerView();
                }
            }
        });
    }
}

如果您不是使用 nestedScrollView 的情况,则可以设置myRecyclerview.setNestedScrollingEnabled(false);以使其平滑滚动(如果整个活动或片段有多个回收器视图(。

希望这个答案对你有帮助,

快乐编码。巴基

最新更新