【问题标题】:nested scrollview + recyclerview, strange autoscroll behaviour [duplicate]嵌套的scrollview + recyclerview,奇怪的自动滚动行为[重复]
【发布时间】:2016-10-24 09:27:39
【问题描述】:

在视图寻呼机中,我有几个片段,其中一个使用带有标题和 recyclerview 的嵌套滚动视图:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.m360.android.fragment.Members.MemberDetailsFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="20dp">

        <header/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="0dp" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

标签“header”代表了一个复杂的布局,我不想在这里发布,因为它过多地延伸了代码。

当我在选项卡之间切换时,它会滚动到回收站视图。标题已隐藏,我必须向上滚动才能看到它。

关于是什么原因的任何想法?如果可以避免的话,我不想在适配器中使用类型。

【问题讨论】:

    标签: android android-recyclerview android-nestedscrollview


    【解决方案1】:

    我们也有类似的问题。我们有一个垂直的RecyclerView。这个垂直的RecyclerView 的每个项目都包含一个水平的RecyclerView,就像在 Android TV 应用中一样。

    当我们将支持库从 23.4.0 升级到 24.0.0 时,自动滚动突然出现。特别是,当我们打开一个Activity 然后返回时,垂直的RecyclerView 向上滚动,这样当前水平的RecyclerView 行就不会被剪切,而是完全显示该行。

    添加android:descendantFocusability="blocksDescendants" 可解决此问题。

    但是,我找到了another solution,它也可以。在我们的例子中,垂直的RecyclerView 包含在FrameLayout 中。如果我将android:focusableInTouchMode="true" 添加到此FrameLayout,问题就会消失。

    甚至还有一个third solution mentioned here,它基本上包括在子/内部RecyclerViews 上调用setFocusable(false)。这个我没试过。

    对了,有一个open issue on the AOSP

    【讨论】:

    • 您选择了第二种解决方案吗?为什么你认为它更好?谢谢
    • @RenaudFavier 我选择了android:descendantFocusability="blocksDescendants" 选项。我认为提出的 3 个解决方案做了类似的事情(基本上防止子/内部RecyclerView 获得焦点,从而防止自动滚动)。但是,take a look at this 可以更好地理解这个问题。
    • @RenaudFavier 我可以使用android:focusableInTouchMode="true" 技巧来窃取焦点,因为我将外部RecyclerView 包裹在FrameLayout 中。如果外部 RecyclerView 是您的布局的根,我不确定您是否可以这样做。
    • 好的,我明白了。谢谢分享!
    • 好的,这解决了我的问题。但是为什么这会成功,我不明白这一点。如果有人能解释一下。谢谢!
    【解决方案2】:

    NestedScrollView的子布局设置android:focusableInTouchMode="true"(可能是LinearLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多