项目中用到一个下拉列表功能,手lu了一个RecyclerView 嵌套RecyclerView的垂直下拉列表,本来挺不错,结果遇到个问题,每次点击父RecyclerView靠后面的几项Item展开子RecyclerView,结果父RecyclerView每次都自动滑动到最上面。
研究了一下,其实就是子RecyclerView抢占了父RecyclerView的焦点导致的。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_info_recycler_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<View
android:id="@+id/item_line_up"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="@color/study_time_line_color"
android:onClick="@{()->adapter.setUpOrDown(beenToCatalogBean)}"/>
<View
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:layout_below="@+id/item_line_up"
android:background="@color/my_info_gray_line"
android:visibility="@{beenToCatalogBean.isShow?View.INVISIBLE:View.VISIBLE}"/>
<LinearLayout
android:id="@+id/rl_catalog"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:background="#42ebff"
android:onClick="@{()->adapter.setUpOrDown(beenToCatalogBean)}"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp">
<TextView
android:id="@+id/tv_catalog_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="@{()->adapter.setUpOrDown(beenToCatalogBean)}"
android:padding="5dp"
android:text="@{beenToCatalogBean.CatalogName}"
android:textColor="#364243"
android:textSize="20dp"/>
<ImageButton
android:id="@+id/btn_catalog_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_catalog_name"
android:background="@{MathApp.instance.getResources().getDrawable(beenToCatalogBean.drawableId)}"
android:onClick="@{()->adapter.setUpOrDown(beenToCatalogBean)}"/>
</RelativeLayout>
</RelativeLayout>
<!--<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />-->
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_my_info_test_point"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl_catalog"
android:focusableInTouchMode="false"
android:nestedScrollingEnabled="false"/>
</RelativeLayout>
关键就是这一句,在子RecyclerView中加上
android:focusableInTouchMode=“false”
同时在父布局上加上
android:focusable=“true”
android:focusableInTouchMode=“true”
只要保证不要让子布局抢占了RecyclerView的焦点
如果嵌套了ScrollView也遇到这种问题 ,同样保证父布局是有焦点就没问题