How to implement endless list with RecyclerView?

前端 未结 30 2690
无人及你
无人及你 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条回答
  •  醉酒成梦
    2020-11-22 03:15

    I have created LoadMoreRecyclerView using Abdulaziz Noor Answer

    LoadMoreRecyclerView

    public class LoadMoreRecyclerView extends RecyclerView  {
    
        private boolean loading = true;
        int pastVisiblesItems, visibleItemCount, totalItemCount;
        //WrapperLinearLayout is for handling crash in RecyclerView
        private WrapperLinearLayout mLayoutManager;
        private Context mContext;
        private OnLoadMoreListener onLoadMoreListener;
    
        public LoadMoreRecyclerView(Context context) {
            super(context);
            mContext = context;
            init();
        }
    
        public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            init();
        }
    
        public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            mContext = context;
            init();
        }
    
        private void init(){
            mLayoutManager = new WrapperLinearLayout(mContext,LinearLayoutManager.VERTICAL,false);
            this.setLayoutManager(mLayoutManager);
            this.setItemAnimator(new DefaultItemAnimator());
            this.setHasFixedSize(true);
        }
    
        @Override
        public void onScrolled(int dx, int dy) {
            super.onScrolled(dx, dy);
    
            if(dy > 0) //check for scroll down
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
    
                if (loading)
                {
                    if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                    {
                        loading = false;
                        Log.v("...", "Call Load More !");
                        if(onLoadMoreListener != null){
                            onLoadMoreListener.onLoadMore();
                        }
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
        }
    
        @Override
        public void onScrollStateChanged(int state) {
            super.onScrollStateChanged(state);
        }
    
        public void onLoadMoreCompleted(){
            loading = true;
        }
    
        public void setMoreLoading(boolean moreLoading){
            loading = moreLoading;
        }
    
        public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
            this.onLoadMoreListener = onLoadMoreListener;
        }
    }
    

    WrapperLinearLayout

    public class WrapperLinearLayout extends LinearLayoutManager
    {
        public WrapperLinearLayout(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException e) {
                Log.e("probe", "meet a IOOBE in RecyclerView");
            }
        }
    }
    

    //Add it in xml like

    
    
    

    OnCreate or onViewCreated

    mLoadMoreRecyclerView = (LoadMoreRecyclerView) view.findViewById(R.id.recycler_view);
    mLoadMoreRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    callYourService(StartIndex);
                }
            });
    

    callYourService

    private void callYourService(){
        //callyour Service and get response in any List
    
        List newDataFromServer = getDataFromServerService();
        //Enable Load More
        mLoadMoreRecyclerView.onLoadMoreCompleted();
    
        if(newDataFromServer != null && newDataFromServer.size() > 0){
                StartIndex += newDataFromServer.size();
    
                if (newDataFromServer.size() < Integer.valueOf(MAX_ROWS)) {
                        //StopLoading..
                       mLoadMoreRecyclerView.setMoreLoading(false);
                }
        }
        else{
                mLoadMoreRecyclerView.setMoreLoading(false);
                mAdapter.notifyDataSetChanged();
        }
    }
    

提交回复
热议问题