RecyclerView 嵌套RecyclerView 或者 ScrollView当中嵌套RecyclerView ,子View会自动滚动到顶部

问题如图

此问题一开始我以为是我在fragment当中,返回和移动的时候调用了adpaternotifyItemChange方法情况和notifyItemChanged(int position) 引起的返回时RecyclerView页面不自然的跳动 一样,但是发现我并没有调用过此方法,所以我想是不是有可能是子 recyclerView 抢了焦点导致子 RecyclerView自动滚动到了第一行,所以我在子recyclerView设置

recycler_mine_partake_message.setFocusableInTouchMode(false); //设置不需要焦点
recycler_mine_partake_message.requestFocus(); //设置焦点不需要

同时我想到了在ScrollView当中同样会出现这个问题 ,解决思路:

    在代码里面 让处于ScrollView或者RecyclerView1 顶端的某个控件获得焦点即可
    比如顶部的一个textview
    tv_goodsName.setFocusableInTouchMode(true);  
    tv_goodsName.requestFocus(); 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
如果您想使用SmartRefreshLayout嵌套ScrollView嵌套多个RecyclerView,您需要在外部ScrollView和内部RecyclerView之间添加一个LinearLayout,然后将每个RecyclerView添加到LinearLayout中。接下来,您可以在LinearLayout中通过设置一个NestedScrollingChild来实现嵌套滚动。 以下是代码示例: ``` <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </androidx.core.widget.NestedScrollView> ``` 然后在代码中,您需要在RecyclerView上设置NestedScrollingEnabled为false,以防止RecyclerView自己处理滚动事件,而是让NestedScrollView来处理。 ``` recycler1.setNestedScrollingEnabled(false); recycler2.setNestedScrollingEnabled(false); recycler3.setNestedScrollingEnabled(false); ``` 最后,您可以在NestedScrollView上设置SmartRefreshLayout的布局参数,如下所示: ``` <com.scwang.smart.refresh.layout.SmartRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:enableLoadMore="true" app:enableRefresh="true"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </androidx.core.widget.NestedScrollView> </com.scwang.smart.refresh.layout.SmartRefreshLayout> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值