Make GridLayoutManager VERTICAL orientation with horizontal scrolling
Asked Answered
L

2

6

I need the RecyclerView Gridlayout Manager with horizontal scrollable behavior. It must add child views like it does when it has VERTICAL orientation (column wise index for e.g. (row 1, col 1) = index 0, (row 1, col 2) = index 1 and so on).

Currently VERTICAL Orientation does not allow to scroll horizontally and fits child view with in device width.

Edit:

I have 100 rows and 150 columns in RecyclerView GridLayoutManager and need horizontal scrolling of view. I am hiding and showing of rows, which is only possible with VERTICAL Orientation.

Langille answered 20/12, 2017 at 16:18 Comment(4)
When you create a new Resource Layout you get qualifiers such as language, orientation, screen size. In your case orientation is one of these. In the manifest you can force orientation developer.android.com/guide/topics/manifest/…Responser
Orientation of activity in manifest file is "landscape" mode. Problem is with RecyclerView GridLayoutManager orientation behavior.Langille
@Langille Did you got any potential solution with RecyclerView & GridLayout Manager.?Fiden
@Langille have you find a solution?Gusher
B
3

You can try to put your Recyclerview inside a HorizontalScrollingView and disable the vertical scrolling for your Recyclerview:

Your .xml file may look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <HorizontalScrollView
        android:id="@+id/myHorizontalScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

    </HorizontalScrollView >

</LinearLayout >

In your code you have to override canScrollVertically of your GridLayoutManager:

Java:

GridLayoutManager mLayoutManager = new GridLayoutManager(this, 100) {
    public boolean canScrollVertically() {
        return false;
    }
};

Kotlin:

val mLayoutManager = object : GridLayoutManager(this, 100) {
    override fun canScrollVertically() : Boolean {
        return false
    }
}

It is possible that the 100 rows are too much to display them at once on your screen. To solve this, just don't deactivate the vertical scrolling in the GridLayoutManager.

Briefs answered 9/5, 2019 at 14:33 Comment(0)
G
1

Try

new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false);

Here 1 counts as number of rows. use getActvity() if you work with fragment, and MainActivity.this for example if you re calling it in the activity

Guildsman answered 20/12, 2017 at 18:23 Comment(2)
Rainmaker, Thanks for answer, Above code will allow horizontal scrolling but I have many rows in layout and assign index vertically(Row wise). I need index horizontally.Langille
Maybe i don't understand what you mean, put the number of rows u need (5,10)....you cannot scroll endlessly vertically and horizontally at the same timeGuildsman

© 2022 - 2024 — McMap. All rights reserved.