今天開(kāi)發(fā)時(shí)变秦,遇到了幾個(gè)ScrollView與RecyclerView嵌套導(dǎo)致的問(wèn)題坦喘。
1. 進(jìn)入頁(yè)面時(shí),layout沒(méi)有置頂
root cause: 由于在ScrollView內(nèi)嵌套了RecyclerView婉商,導(dǎo)致RecyclerView獲取到了焦點(diǎn)
solution: 使ScrollView包裹的View獲取到焦點(diǎn)
如下所示坟桅,在LinearLayout里增加 android:focusable="true"和 android:focusableInTouchMode="true"這兩個(gè)屬性
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="乘機(jī)人"/>
<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問(wèn)題
solution1: 重寫(xiě)LinearLayoutManager璃氢,可能可以解決
solution2: 使用NestScrollView哟玷,而不是ScrollView
reference: https://blog.csdn.net/ThugKd/article/details/78196970
<?xml version="1.0" encoding="utf-8"?>
<!-- 使用NestedScrollView 而不是ScrollView, 解決ScrollView嵌套R(shí)ecyclerView,導(dǎo)致RecyclerView顯示不全問(wèn)題 -->
<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="乘機(jī)人"/>
<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滑動(dòng)沖突
root cause: NestedScrollView與RecyclerView嵌套
solution: recyclerView.setNestedScrollingEnabled(false);
reference: http://www.reibang.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));
// 修復(fù)NestedScrollView與RecyclerView滑動(dòng)沖突
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(mAdapter);
}