【问题标题】:Horizontal RecyclerView inside vertical ScrollView垂直 ScrollView 内的水平 RecyclerView
【发布时间】:2015-11-30 09:06:34
【问题描述】:

所以我在垂直 ScrollView 中有一个水平 RecyclerView。我的布局中的所有内容都显示得很好,并且都按照我想要的方向滚动,并且运行顺畅。

我唯一的问题是,RecyclerView 位于 ScrollView 中的其他内容下方,当 RecyclerView 部分可见时,它会在启动时将 RecyclerView 的底部与屏幕底部对齐。这意味着 RecyclerView 上方的内容被推离了屏幕。

有谁知道为什么会发生这种情况,我该如何解决?

这是一个简单的布局,可以实现我刚才描述的功能。您甚至不需要填充 RecyclerView,它仍然会这样做。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:background="#fff"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#000"/>

    </LinearLayout>

</ScrollView>

【问题讨论】:

标签: android scrollview android-recyclerview


【解决方案1】:

原来这个问题在这里被报告给谷歌Issue - 81854

根据谷歌的说法,它正在按预期工作。问题在于 RecyclerView 将 focusableInTouchMode 设置为 true。为了解决这个问题,我在 ScrollView 的最顶层视图上将 focusableInTouchModefocusable 设置为 true。

以下是我在原始问题中提供的代码示例的修复:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:background="#fff"
            android:focusableInTouchMode="true"
            android:focusable="true"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="#000"/>

    </LinearLayout>

</ScrollView>

【讨论】:

  • 您说在 ScrollView 的最顶层视图上将 focusableInTouchModefocusable 设置为 true。但是在代码中我看到了一些不同的东西。请澄清。
  • @Parsi 对不起。由于 ScrollView 只能有一个孩子,我的意思是 ScrollView 孩子的最顶层视图。
  • 那我不明白。谢谢你。没提:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多