创建一个水平和垂直滚动的RecyclerView

问题描述 投票:0回答:3

在过去的几周里,我一直在学习使用

RecyclerView
。我需要实现一个水平列表,即通过将设备切换为横向模式,如下所示:

我找到了最好的解决方案(如何创建

RecyclerView
的水平位移,这里),但遇到了另一个问题。该项目
RecyclerView
大于设备的高度(横向、水平),因此我需要同时创建垂直和水平位移。

我查看了

LayoutManager
类的 Android 开发人员方法,但我的技能不够高,无法理解大多数方法。我还尝试将一个
RecyclerView
垂直放入另一个
RecyclerView
中,水平放置所有内容,但出现错误:

IllegalStateException:RecyclerView没有LayoutManager

为了解决这个问题,我从 XML 文件中删除了所有

<View ... />
元素,但这不会给出任何结果。

为了澄清我的问题:是否可以让我的布局水平和垂直滚动,以及如果您能解释我将如何欣赏它。

android android-layout android-studio scroll android-recyclerview
3个回答
22
投票

我对应用程序出现的所有问题感到非常愤怒,因为他们没有考虑最简单的解决方案。

RecyclerView
中由两个 XML 文件组成,主文件用于声明 RecyclerView,另一个包含内容。

最简单的解决方案是在

RecyclerView
中引入
ScrollView
。因此,借助
ScrollView
,我可以一次移动所有项目。在横向和横向模式下,我可以借助
RecyclerView
来移动项目。

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/cardIn_margin_ext">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbarStyle="outsideInset"
            android:scrollbars="horizontal" />

</ScrollView>

14
投票

接受的答案对我不起作用。我必须使用 HorizontalScrollView 而不是简单的 ScrollView。

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_margin="@dimen/cardIn_margin_ext">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarStyle="outsideInset"
        android:scrollbars="horizontal" />
</HorizontalScrollView >

0
投票
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="@dimen/cardIn_margin_ext">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbarStyle="outsideInset"
    android:scrollbars="horizontal" />
© www.soinside.com 2019 - 2024. All rights reserved.