El *_*mer 13 android scrollview android-recyclerview
我有一个包含ImageView和RecyclerView的Scrollview.如果导航抽屉打开然后关闭RecyclerView自动滚动到顶部,如何停止此操作?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollViewMain"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView_main_icons_line"
android:src="@drawable/main_line" />
...
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_activity_main_passenger_log"
android:paddingBottom="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Rah*_*var 54
这个问题因为recyclerView有默认焦点.
解
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
Run Code Online (Sandbox Code Playgroud)
添加 android:descendantFocusability="blocksDescendants"到您的scrollView的直接布局
lan*_*ung 18
那是因为RecyclerView总是设置:
setFocusableInTouchMode(true);
Run Code Online (Sandbox Code Playgroud)
这是在其构造函数中硬编码的.
因此,如果您在XML中为RecyclerView应用这些属性
android:focusable="false"
android:focudeableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)
这将是无用的,你最终得到recycleView.isFocusable()== true ...
因此,最优雅的解决方案是禁用RecyclerView的Parent.
<LinearLayout
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
...
>
<android.support.v7.widget.RecyclerView
...
/>
/>
Run Code Online (Sandbox Code Playgroud)
或者你可以简单地setFocusable(false)
| 归档时间: |
|
| 查看次数: |
16194 次 |
| 最近记录: |