今天,简单讲讲Android里使用SrcollView嵌套recyclerView需要注意的地方。
不废话了直接上代码,在使用时加上下面的代码就可以
recyclerView.setLayoutManager(new LinearLayoutManager(this){
@Override
public boolean canScrollVertically() {
//解决ScrollView里存在多个RecyclerView时滑动卡顿的问题
//如果你的RecyclerView是水平滑动的话可以重写canScrollHorizontally方法
return false;
}
});
//解决数据加载不完的问题
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
//解决数据加载完成后, 没有停留在顶部的问题
recyclerView.setFocusable(false);
- 切记,切记,切记, 重要的事情说三遍, 还解决不了的时候看这里
关于嵌套后滑动卡顿或者焦点之类的问题
使用了上面的方法还无法解决就把布局中的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>
Android SrcollView嵌套recyclerView的使用就讲完了。
就这么简单。