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.

Gaurav
  • 61
  • 1
  • 3
  • 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 https://developer.android.com/guide/topics/manifest/activity-element.html – Pomagranite Dec 20 '17 at 16:28
  • Orientation of activity in manifest file is "landscape" mode. Problem is with RecyclerView GridLayoutManager orientation behavior. – Gaurav Dec 21 '17 at 06:05
  • @Gaurav Did you got any potential solution with RecyclerView & GridLayout Manager.? – Rehan Sarwar Sep 10 '20 at 12:18
  • @Gaurav have you find a solution? – LunaVulpo Dec 28 '20 at 18:44

2 Answers2

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.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Shorxy
  • 598
  • 5
  • 12
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

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • 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. – Gaurav Dec 21 '17 at 06:10
  • 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 time – Rainmaker Dec 21 '17 at 07:48