How to implement endless list with RecyclerView?

前端 未结 30 3073
无人及你
无人及你 2020-11-22 02:22

I would like to change ListView to RecyclerView. I want to use the onScroll of the OnScrollListener in RecyclerView to determine if a

30条回答
  •  萌比男神i
    2020-11-22 03:12

    My way to detect loading event is not to detect scrolling, but to listen whether the last view was attached. If the last view was attached, I regard it as timing to load more content.

    class MyListener implements RecyclerView.OnChildAttachStateChangeListener {
        RecyclerView mRecyclerView;
    
        MyListener(RecyclerView view) {
            mRecyclerView = view;
        }
    
        @Override
        public void onChildViewAttachedToWindow(View view) {
    
        RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
        RecyclerView.LayoutManager mgr = mRecyclerView.getLayoutManager();
        int adapterPosition = mgr.getPosition(view);
    
        if (adapterPosition == adapter.getItemCount() - 1) {
            // last view was attached
            loadMoreContent();
        }
    
        @Override
        public void onChildViewDetachedFromWindow(View view) {}
    }
    

提交回复
热议问题