Create a RecyclerView with both horizontal and vertical scrolling
Asked Answered
B

3

24

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.

Bugbear answered 4/4, 2016 at 16:41 Comment(1)
how about 2-dimensional gallery as: #3754945Numbersnumbfish
B
22

I was so angry about all the problems that had tended with the application that had not thought about the easiest solution.

In a RecyclerView consists of two XML files, the main one where the RecyclerView is declared and another with content.

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>
Bugbear answered 5/4, 2016 at 8:47 Comment(6)
Simple and genius!Trever
if a parent is a scrollview, the child cannot be a scrollview too.. this results in ambiguity wen rendered. Niiice attempt to trouble android OSHammered
Congrats on your solution! But if you want to have a LOT of both vertical and horizontal scrolling, you need the RecyclerView to handle both directions (like in my case, sort of a spreadsheet). Anyone out there with a solution that actually uses only RecyclerViews?Gambrinus
We need to add NestedScrollView instead of ScrollviewCatechetical
This is not working .. you have to use FixedGridLayoutManager for that.Frozen
The problem here is that ScrollView isn't aware of what's currently visible and can't recycle views. You'll end up rendering everything in the RecyclerView, even the non visible rows.Byran
A
15

The accepted answer did'nt work for me. I had to use the HorizontalScrollView instead of simple 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 >
Artair answered 17/1, 2020 at 12:23 Comment(0)
D
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" />
Demarcusdemaria answered 30/10, 2023 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.