android RecyclerView 嵌套 RecycleView 导致位置不正确问题

项目中用到一个下拉列表功能,手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也遇到这种问题 ,同样保证父布局是有焦点就没问题

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值