Create a RecyclerView with both horizontal and vertical scrolling

时间:2016-04-04 16:41:44

标签: android android-layout android-studio scroll android-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:

enter image description here

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.

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 has no LayoutManager

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.

2 个答案:

答案 0 :(得分:13)

我对那些没有考虑过最简单解决方案的应用所遇到的所有问题感到非常生气。

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>

答案 1 :(得分:6)

接受的答案对我不起作用。我不得不使用Horizo​​ntalScrollView而不是简单的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 >