1. 程式人生 > >NestedScrollView+RecyclerView 滑動卡頓簡單解決方案

NestedScrollView+RecyclerView 滑動卡頓簡單解決方案

這個是在工作中發現的問題

以下xml是當前佈局: <code> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"

<LinearLayout android:id="@+id/linerLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"

<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </code>

NestedScrollView中包含了LinearLayout,LinearLayout包含了一系列的元件,其中包括RecyclerView,RecyclerView和NestedScrollView都有滾動事件,這種情況下進行滑動操作,fling的操作體驗很差,幾乎就是手指離開的時候,滑動停止.

以下xml是改動後的佈局: <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:nestedScrollingEnabled="false" /> android:nestedScrollingEnabled="false" 官方文件: Enable or disable nested scrolling for this view. If this property is set to true the view will be permitted to initiate nested scrolling operations with a compatible parent view in the current hierarchy. If this view does not implement nested scrolling this will have no effect. Disabling nested scrolling while a nested scroll is in progress has the effect of stopping the nested scroll. 這裡設定為false,放棄自己的滑動,交給外部的NestedScrollView處理,就沒有出現卡頓的現象了,並且有fling的效果