加載更多流程實(shí)現(xiàn)

mRecyclerView滑動(dòng)到底部的監(jiān)聽(tīng)

      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()

重點(diǎn)看一下checkIfLoadMore()這個(gè)方法,這個(gè)方法用來(lái)判斷界面上最后一個(gè)位置和itemcount的比較,因?yàn)镽ecyclerView的LayoutManager有三種,這個(gè)如果用If else來(lái)進(jìn)行判斷,過(guò)于冗余,我們這個(gè)采用策略模式來(lái)進(jìn)行編寫(xiě)。

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;
    }

加載完成后

因?yàn)槲覀兯⑿潞图虞d調(diào)用同一個(gè)接口,傳遞不同的參數(shù),所以在操作完成后,我們需要進(jìn)行判斷,是哪種操作完成了
    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;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容