今天开发时,遇到了几个ScrollView与RecyclerView嵌套导致的问题。
1. 进入页面时,layout没有置顶
root cause: 由于在ScrollView内嵌套了RecyclerView,导致RecyclerView获取到了焦点
solution: 使ScrollView包裹的View获取到焦点
如下所示,在LinearLayout里增加 android:focusable="true"和 android:focusableInTouchMode="true"这两个属性
reference: https://blog.csdn.net/suwenlai/article/details/72902684
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="乘机人"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="15dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_flight_order_detail"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
2.RecyclerView显示不全
root cause: 可能是RecyclerView的onMeasure问题
solution1: 重写LinearLayoutManager,可能可以解决
solution2: 使用NestScrollView,而不是ScrollView
reference: https://blog.csdn.net/ThugKd/article/details/78196970
<?xml version="1.0" encoding="utf-8"?>
<!-- 使用NestedScrollView 而不是ScrollView, 解决ScrollView嵌套RecyclerView,导致RecyclerView显示不全问题 -->
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:text="乘机人"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="15dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_flight_order_detail"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
3.NestedScrollView与RecyclerView滑动冲突
root cause: NestedScrollView与RecyclerView嵌套
solution: recyclerView.setNestedScrollingEnabled(false);
reference: https://www.jianshu.com/p/791c0a4acc1c
@Override
protected void initView() {
mAdapter = new PassengerInfoAdapter();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_flight_order_detail);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new ListDivider(this, ListDivider.HORIZONTAL_LIST, R.drawable.passenger_info_divider));
// 修复NestedScrollView与RecyclerView滑动冲突
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(mAdapter);
}