Android 處理RecycleView使用中遇到的問題

直接上代碼:

recyclerView.setLayoutManager(new LinearLayoutManager(this){
      @Override
      public boolean canScrollVertically() {
         //解決ScrollView里存在多個RecyclerView時滑動卡頓的問題
         //如果你的RecyclerView是水平滑動的話可以重寫canScrollHorizontally方法
         return false;
      }
});
//解決數(shù)據(jù)加載不完的問題
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
//解決數(shù)據(jù)加載完成后, 沒有停留在頂部的問題
recyclerView.setFocusable(false);

. 切記,切記,切記, 重要的事情說三遍, 還解決不了的時候看這里

關(guān)于嵌套后滑動卡頓或者焦點之類的問題
使用了上面的方法還無法解決就把布局中的RecyclerView外層的
ScrollView換成NestedScrollView就可以解決了

大概就改成這樣

<android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                    <android.support.v7.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
</android.support.v4.widget.NestedScrollView>

綜合解決方案

若是需要綜合解決上述三個問題,則可以采用如下幾種方式

插入LinearLayout/RelativeLayout

在原有布局中插入一層LinearLayout/RelativeLayout,形成如下布局


image.png

重寫LayoutManager

該方法的核心思想在于通過重寫LayoutManager中的onMeasure()方法,即

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
    super.onMeasure(recycler, state, widthSpec, heightSpec);
}

重新實現(xiàn)RecyclerView高度的計算,使得其能夠在ScrollView中表現(xiàn)出正確的高度,具體重寫方式可參考這篇文章

http://www.cnblogs.com/tianzh...

重寫ScrollView

該方法的核心思想在于通過重寫ScrollView的onInterceptTouchEvent(MotionEvent ev)方法,攔截滑動事件,使得滑動事件能夠直接傳遞給RecyclerView,具體重寫方式可參考如下

/**
 * Created by YH on 2017/10/10.
 */

public class RecyclerScrollView extends ScrollView {
    private int slop;
    private int touch;

    public RecyclerScrollView(Context context) {
        super(context);
        setSlop(context);
    }

    public RecyclerScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setSlop(context);
    }

    public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSlop(context);
    }

    /**
     * 是否intercept當前的觸摸事件
     * @param ev 觸摸事件
     * @return true:調(diào)用onMotionEvent()方法,并完成滑動操作
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //  保存當前touch的縱坐標值
                touch = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //  滑動距離大于slop值時,返回true
                if (Math.abs((int) ev.getRawY() - touch) > slop) return true;
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 獲取相應(yīng)context的touch slop值(即在用戶滑動之前,能夠滑動的以像素為單位的距離)
     * @param context ScrollView對應(yīng)的context
     */
    private void setSlop(Context context) {
        slop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
}

事實上,盡管我們能夠采用多種方式解決ScrollView嵌套RecyclerView所產(chǎn)生的一系列問題,但由于上述解決方式均會使得RecyclerView在頁面加載過程中一次性顯示所有內(nèi)容,因此當RecyclerView下的條目過多時,將會對影響整個應(yīng)用的運行效率?;诖?,在這種情況下我們應(yīng)當盡量避免采用ScrollView嵌套RecyclerView的布局方式

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

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

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