当前位置:网站首页>Slide the receleview horizontally to the far right to listen to the page loading function

Slide the receleview horizontally to the far right to listen to the page loading function

2022-06-11 05:36:00 Cockroach bully BBQ

Close test available

thank :https://www.jianshu.com/p/5eb2de368ea0 

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    //  Used to mark whether it is sliding to the left 
    private boolean isSlidingToLeft = false;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //  When not sliding 
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            //  Get the last fully displayed itemPosition
            int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
            int itemCount = manager.getItemCount();

            //  Judge if it's sliding to the last item, And it slides to the left 
            if (lastItemPosition == (itemCount - 1) && isSlidingToLeft) {
                //  Load more 
                onLoadMore();
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        // dx Greater than 0 Indicates that you are sliding to the left , Less than or equal to 0 Indicates sliding or stopping to the right 
        isSlidingToLeft = dx > 0;
    }

    /**
     *  Load more callbacks 
     */
    public abstract void onLoadMore();
}
原网站

版权声明
本文为[Cockroach bully BBQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020538031488.html