簡體   English   中英

滾動監聽器,用於嵌套的recyclerview

[英]Scroll listener for nested recyclerview

我正在開發一個應用程序,其中我的主頁中填充了具有一個水平Recyclerview和兩個垂直Recycler視圖的多種視圖類型的Recyclerview數據。 所以我在Recyclerview中有Recyclerview。 我想為第二個垂直Recyclerview添加滾動偵聽器,但無法添加,因為所有滾動都由主父Recyclerview處理。 我試圖在適配器內添加滾動器偵聽器,它對於水平的Recycler視圖正常工作,但對於垂直的視圖卻不工作。 如何使其運作?

如果您在nestedScrollView中使用垂直的recyclerview,則需要為nestedScrollView實現滾動偵聽器,否則不會觸發recyclerview的滾動偵聽器。 這是您基本上可以實現它的方法,並且可以捕獲在何處加載更多數據以實現分頁功能

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); 如果您對整個活動或片段有多個recyclerviews,則可以使其平滑滾動。

希望這個答案對您有幫助,

編碼愉快。 巴基

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM