How to implement setOnScrollListener in RecyclerView

后端 未结 7 942
遇见更好的自我
遇见更好的自我 2020-11-29 02:09

How Do I show progress bar at bottom when user reached to items those are visible in a list.

I have written a code in which i am getting data using web service, now

相关标签:
7条回答
  • 2020-11-29 03:09

    Now accepted answer as @SilentKnight suggested will not work as setOnscroll is deprecated by Android SDK.

    You should use

    recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int ydy = 0;
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
    
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int offset = dy - ydy;
                ydy = dy;
                boolean shouldRefresh = (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0)
                        && (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) && offset > 30;
                if (shouldRefresh) {
                    //swipeRefreshLayout.setRefreshing(true);
                    //Refresh to load data here.
                    return;
                }
                boolean shouldPullUpRefresh = linearLayoutManager.findLastCompletelyVisibleItemPosition() == linearLayoutManager.getChildCount() - 1
                        && recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING && offset < -30;
                if (shouldPullUpRefresh) {
                    //swipeRefreshLayout.setRefreshing(true);
                    //refresh to load data here.
                    return;
                }
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    
    0 讨论(0)
提交回复
热议问题