垂直滚动视图中的水平RecyclerView

6
我有一个水平的RecyclerView放在一个垂直的ScrollView中。布局内的所有内容都可以正常显示,并且它们可以按照我想要的方向滑动,而且滑动起来非常流畅。
唯一的问题是,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>

请查看此链接 https://github.com/lucasr/twoway-view/。 - Raghunandan
根据我对TwoWayView的了解,它并不与我的问题相关,但还是谢谢。 - Daniel Julio
2个回答

7
事实证明,该问题已在此处向Google报告:Issue - 81854
根据 Google 的说法,这是预期的工作方式。问题在于 RecyclerView 设置了 focusableInTouchMode。要解决这个问题,我将 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。但是在代码中我看到了不同的东西。请澄清一下。 - Saiteja Parsi
@Parsi 抱歉。由于ScrollView只能有一个子视图,我指的是ScrollView子视图中最顶层的视图。 - Daniel Julio
我当时没听懂。谢谢。不用提 :) - Saiteja Parsi

1

我已经寻找了很长时间的解决方案。最终,我偶然间决定了。在您的活动中,找到ScrollView并为其添加触摸监听器。一切都将按照应有的方式工作。

ScrollView scroll = findViewById(R.id.scroll);
        scroll.setOnTouchListener((view, motionEvent) -> {
            return false;
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接