recyclerView的學(xué)習(xí)(四)

男人要么穿上軍裝保家衛(wèi)國,要么穿上西裝運(yùn)籌帷幄
最近看戰(zhàn)狼二,看的是激動不已,今天也趁熱打鐵總結(jié)下recyclerVeiw中一些常見的小知識點(diǎn)。

如果在開發(fā)中要嵌套scrollview怎么辦?

其實(shí)在開發(fā)中嵌套scrollview網(wǎng)上有的說重寫LayoutManager等等,效果不是很明顯,你可以去搜搜。現(xiàn)在給出的解決方案是
NestedScrollView 包裹RecyclerView。媽媽再也不用擔(dān)心嵌套滑動的問題。然后在代碼中加上:

my_recyler.setNestedScrollingEnabled(false); 

這就是解決嵌套recyclerVeiw的終極解決方法。

RecyclerView.OnScrollListener

(1)滾動事件分類

列表的滾動一般分為兩種:

1.手指按下 -> 手指拖拽列表移動 -> 手指停止拖拽 -> 抬起手指
2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表繼續(xù)滾動 -> 停止?jié)L動
上面的過程的狀態(tài)變化如下:

1.靜止 -> 被迫拖拽移動 -> 靜止
2.靜止 -> 被迫拖拽移動 -> 自己滾動 -> 靜止
OnScrollListener

/**
 * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
 * has occurred on that RecyclerView.
 * <p>
 * @see RecyclerView#addOnScrollListener(OnScrollListener)
 * @see RecyclerView#clearOnChildAttachStateChangeListeners()
 *
 */
public abstract static class OnScrollListener {
    /**
     * Callback method to be invoked when RecyclerView's scroll state changes.
     *
     * @param recyclerView The RecyclerView whose scroll state has changed.
     * @param newState     The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
     *                     {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
     */
    public void onScrollStateChanged(RecyclerView recyclerView, int newState){}

    /**
     * Callback method to be invoked when the RecyclerView has been scrolled. This will be
     * called after the scroll has completed.
     * <p>
     * This callback will also be called if visible item range changes after a layout
     * calculation. In that case, dx and dy will be 0.
     *
     * @param recyclerView The RecyclerView which scrolled.
     * @param dx The amount of horizontal scroll.
     * @param dy The amount of vertical scroll.
     */
    public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}

onScrollStateChanged

回調(diào)的兩個(gè)變量的含義:

  • recyclerView: 當(dāng)前在滾動的RecyclerView
  • newState: 當(dāng)前滾動狀態(tài)
    newState 狀態(tài)碼
/**
 * The RecyclerView is not currently scrolling.(靜止沒有滾動)
 */
public static final int SCROLL_STATE_IDLE = 0;

/**
 * The RecyclerView is currently being dragged by outside input such as user touch input.
 *(正在被外部拖拽,一般為用戶正在用手指滾動)
 */
public static final int SCROLL_STATE_DRAGGING = 1;

/**
 * The RecyclerView is currently animating to a final position while not under outside control.
 *(自動滾動)
 */
public static final int SCROLL_STATE_SETTLING = 2;


狀態(tài)發(fā)生改變的時(shí)候會調(diào)用。接下來針對兩種滑動過程具體onScrollStateChanged 是怎么打印信息的

  • 1.靜止 -> 被迫拖拽移動 -> 靜止
image.png
  • 2.靜止 -> 被迫拖拽移動 -> 自己滾動 -> 靜止
image.png
onScrolled()

回調(diào)的三個(gè)變量含義:
recyclerView : 當(dāng)前滾動的view
dx : 水平滾動距離
dy : 垂直滾動距離

dx > 0 時(shí)為手指向左滾動,列表滾動顯示右面的內(nèi)容
dx < 0 時(shí)為手指向右滾動,列表滾動顯示左面的內(nèi)容
dy > 0 時(shí)為手指向上滾動,列表滾動顯示下面的內(nèi)容
dy < 0 時(shí)為手指向下滾動,列表滾動顯示上面的內(nèi)容
具體的日志信息就不打印了。
不過這個(gè)方法值判斷recyclerVeiw有么有滑動到底部的方法

判斷滑動到底部的方法

方法一:第一個(gè)可見item的位置 + 當(dāng)前可見的item個(gè)數(shù) >= item的總個(gè)數(shù)

loadingMoreListener = 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 (dy > 0) //向下滾動
            {
                int visibleItemCount = mLinearLayoutManager.getChildCount();//該方法是得到當(dāng)前可見的子視圖的個(gè)數(shù)
                int totalItemCount = mLinearLayoutManager.getItemCount();//返回的是當(dāng)前所有的item的個(gè)數(shù)
                int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();//返回的是從頂部開始劃出屏幕是個(gè)數(shù),即已經(jīng)可見的視圖的個(gè)數(shù)

                if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    loading = true;
                    loadMoreDate();
                }
            }
        }
};


image.png

方法二:通過canScrollVertically 來判斷

loadingMoreListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if(!loading && !recyclerView.canScrollVertically(1)){
            loading = true;
            loadMoreDate();
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

//                if (dy > 0) //向下滾動
//                {
//                    int visibleItemCount = mLinearLayoutManager.getChildCount();
//                    int totalItemCount = mLinearLayoutManager.getItemCount();
//                    int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
//                    if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                        loading = true;
//                        loadMoreDate();
//                    }
//                }
    }
};

方法三:

 RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.i("jimmy","onScrollStateChanged"+"滑動狀態(tài)"+newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
//            Log.i("jimmy","onScrolled"+"滑動dx的值="+dx+"滑動dy的值"+dy);

            if (dy > 0) //向下滾動
            {
//                int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                int totalItemCount = recyclerView.getLayoutManager().getItemCount();
//                int pastVisiblesItems = manager.findFirstVisibleItemPosition();
                int lastvisibleitemcount = manager.findLastVisibleItemPosition();
//                Log.i("jimmy","onScrolled==visibleItemCount的值=="+visibleItemCount+"totalItemCount的值為==="+totalItemCount+"pastVisiblesItems的值為==="+pastVisiblesItems);
                Log.i("jimmy","onScrolled===lastvisibleitemcount的值=="+lastvisibleitemcount);

//                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                    Log.i("jimmy","onScrolled滑動到底部了");
//                }
                if ((lastvisibleitemcount + 1) >= totalItemCount) {
                    Log.i("jimmy","onScrolled滑動到底部了");
                }
            }


        }
    };
image.png

最后還要說說adapter的封裝問題,水平有限

推薦個(gè)學(xué)習(xí)鏈接:https://github.com/CymChad/BaseRecyclerViewAdapterHelper

http://www.itdecent.cn/p/ce347cf991db

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評論 25 709
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,160評論 6 472
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,149評論 22 665
  • 簡介: 提供一個(gè)讓有限的窗口變成一個(gè)大數(shù)據(jù)集的靈活視圖。 術(shù)語表: Adapter:RecyclerView的子類...
    酷泡泡閱讀 5,360評論 0 16
  • 學(xué)姐說,最近想回家了。 在北京,每天都有很多人拉著行李箱準(zhǔn)備離開,每天又有很多人牽著女朋友的手準(zhǔn)備留下來。 學(xué)姐掙...
    圍城不是城閱讀 248評論 2 0

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