实际开发过程中,经常会使用Recyclerview嵌套 RecyclerView 或者 ScrollView 中嵌套Recyclerview。然而通过这种嵌套方式会导致 Recyclerview 显示不全、滑动卡顿等问题,下面我们就来提出解决以上问题的方案。
嵌套滑动解决方案
第一种方案 : 将Recyclerview 放置于 LinearLayout 或 RelateveLayout 中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="none" />
</LinearLayout>
第二种方案 :用 NestedScrollView 来替换 ScrollView
<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:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
但是第二种方案还是会出现滑动卡顿问题,这是只需要禁止 RecyclerView 的滑动即可,通过在 xml 中给 RecyclerView 添加 android:nestedScrollingEnabled=“false” 或者 直接在代码中设置 recyclerView.setNestedScrollingEnabled(false); 也可结合方案三使用
第三种方案:重写LinearLayoutManager的 canScrollVertically 方法从而禁止滑动 ,代码如下
recyclerView.setLayoutManager(new LinearLayoutManager(this){
@Override
public boolean canScrollVertically() {
return false;
}
});