最近有个项目首页采用的是阿里的vlayout开发的,其中有一个item是横向滚动的recyclerview,发现有个比较诡异的现象,当页面来回切换的时候,列表老是会自动滚动到item是横向RecyclerView处。
通过查看源码以及参考网上的分析,原来这就是所谓的RecyclerView抢夺焦点现象,进而导致界面自动滚动了。解决方法就是在RecyclerView的父布局上增加“ android:focusable=“true” android:focusableInTouchMode=“true””这2个配置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent_layout"
android:background="@color/bg_color"
android:focusable="true"
android:focusableInTouchMode="true">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>