创建一个具有水平和垂直滚动的 RecyclerView [英] Create a RecyclerView with both horizontal and vertical scrolling
问题描述
在过去的几周里,我一直在学习使用 RecyclerView
.我需要实现一个水平列表,即通过将设备切换为横向模式,如下所示:
Over the past few weeks I've been learning to use the RecyclerView
. I need to implement a horizontal list, ie, that by turning the device in landscape mode like so:
我找到了最好的解决方案(如何创建 RecyclerView
的水平位移,此处),但遇到了另一个问题.项目 RecyclerView
大于设备的高度(横向,水平),所以我需要同时创建垂直和水平位移.
I found the best solution for this (how to create the horizontal displacement of RecyclerView
, here), but encountered another problem. The item RecyclerView
was larger than the height of the device (in landscape, horizontal), so I need to create a vertical and horizontal displacement, simultaneously.
我查看了 LayoutManager
类的 Android Developer 方法,但我的技能不够高,无法理解大多数方法.我还尝试将 RecyclerView
垂直放置在另一个 RecyclerView
中,并与所有内容水平放置,但出现错误:
I looked at the Android Developer methods for the LayoutManager
class, but my skills are not high enough to understand most of the methods. I also tried putting a RecyclerView
vertically inside another RecyclerView
horizontally with all the content, but I get error:
IllegalStateException: RecyclerView 没有 LayoutManager
IllegalStateException: RecyclerView has no LayoutManager
为了解决这个问题,我从 XML 文件中删除了所有 <View .../>
元素,但这没有给出任何结果.
To rememedy this I removed all <View ... />
elements from the XML file, but this does not give any results.
澄清我的问题:是否可以让我的布局水平和垂直滚动,如果你能解释一下我会很感激.
To clarify what I am asking: is it possible to have my layout scroll both horizontally and vertically, and if you could explain how I would appreciate it.
推荐答案
我对应用程序所出现的所有问题感到非常生气,而这些问题没有考虑过最简单的解决方案.
I was so angry about all the problems that had tended with the application that had not thought about the easiest solution.
在一个 RecyclerView
中由两个 XML 文件组成,主要的一个是声明 RecyclerView 的文件,另一个包含内容.
In a RecyclerView
consists of two XML files, the main one where the RecyclerView is declared and another with content.
最简单的解决方案是在 ScrollView
中引入 RecyclerView
.由于 ScrollView
,我可以一次移动所有项目 由于 RecyclerView
在横向模式下,我可以移动项目.
The simplest solution was to introduce the RecyclerView
within a ScrollView
. So I can move all items at a time thanks to ScrollView
vertically and horizontally I can move the items thanks to RecyclerView
in landscape mode.
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>
这篇关于创建一个具有水平和垂直滚动的 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!