关于含RecyclerView的fragment来回切换时页面自动滑动到底部的解决方法

本文介绍了一个常见问题:在Fragment间切换时,RecyclerView由于默认的focusableOnTouchMode属性导致自动滚动到底部。提供了解决方案,即通过在RecyclerView上方添加一个不影响布局的View并设置其focusableInTouchMode为true来避免此现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原因:

在fragment中来回切换时RecyclerView获得了焦点,而RecyclerView的 focusableOnTouchMode属性默认是true,所以在切换时RecyclerView自动获得焦点就滚动到了底部。

解决办法:

将RecyclerView上面的控件的设置android:focusableInTouchMode="true"(若无上方控件建议加一个不影响布局的View),如:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6     <View
 7         android:layout_width="match_parent"
 8         android:layout_height="2px"
 9         android:background="#bfbfbf"
10         android:focusable="true"
11         android:focusableInTouchMode="true"/>
12     <android.support.v7.widget.RecyclerView
13         android:layout_width="match_parent"
14         android:layout_height="match_parent"
15         android:paddingRight="8dp"
16         android:paddingLeft="8dp"
17         android:fadingEdge="none">
18     </android.support.v7.widget.RecyclerView>
19 </LinearLayout>

 

转载于:https://www.cnblogs.com/yang12318/p/9091531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值