美文网首页
加载更多流程实现

加载更多流程实现

作者: 王小贱_ww | 来源:发表于2017-07-17 17:05 被阅读16次

    mRecyclerView滑动到底部的监听

          mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    if (mCurrentState == ACTION_IDLE && isLoadMoreEnabled && checkIfLoadMore()) {
                        mCurrentState = ACTION_LOAD_MORE_REFRESH;
                        mSwipeRefreshLayout.setEnabled(false);
                        listener.onRefresh(ACTION_LOAD_MORE_REFRESH);
                    }
                }
            });
    

    checkIfLoadMore()

    重点看一下checkIfLoadMore()这个方法,这个方法用来判断界面上最后一个位置和itemcount的比较,因为RecyclerView的LayoutManager有三种,这个如果用If else来进行判断,过于冗余,我们这个采用策略模式来进行编写。

    public interface ILayoutManager {
    
        int findLastVisiblePosition();
    
        RecyclerView.LayoutManager getLayoutManager();
    }
    
    
    public class MyLinearLayoutManager extends LinearLayoutManager implements ILayoutManager {
        public MyLinearLayoutManager(Context context) {
            super(context);
        }
    
        public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        public MyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    
    
        @Override
        public RecyclerView.LayoutManager getLayoutManager() {
            return this;
        }
    
        @Override
        public int findLastVisiblePosition() {
            return findLastVisibleItemPosition();
        }
    
    }
    
    
     private boolean checkIfLoadMore() {
    
            int lastVisibleItemPosition = mLayoutManager.findLastVisiblePosition();
            int totalCount = mLayoutManager.getLayoutManager().getItemCount();
            return totalCount - lastVisibleItemPosition < 5;
        }
    

    加载完成后

    因为我们刷新和加载调用同一个接口,传递不同的参数,所以在操作完成后,我们需要进行判断,是哪种操作完成了
        public void onRefreshCompleted() {
            switch (mCurrentState) {
                case ACTION_PULL_TO_REFRESH:
                    mSwipeRefreshLayout.setRefreshing(false);
    
                    break;
                case ACTION_LOAD_MORE_REFRESH:
                    if (isPullToRefreshEnabled) {
                        mSwipeRefreshLayout.setEnabled(true);
                    }
    
                    break;
                default:
                    break;
            }
            mCurrentState = ACTION_IDLE;
        }
    

    相关文章

      网友评论

          本文标题:加载更多流程实现

          本文链接:https://www.haomeiwen.com/subject/nfzfkxtx.html