33

I'm trying to fill some data into a RecyclerView with GridLayoutManager:

GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);

This will fill the data into the grid from left to right. The first item will be put into top-left place, etc.

But the app I'm developing is designed for an RTL language, so I need to make it fill from right to left.

I tried to change the last argument to true. But it just scrolled the RecyclerView all the way down.

Also setting layoutDirection attribute for RecyclerView doesn't work (without considering that my min API is 16 and this attribute is added for API17+):

<android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layoutDirection="rtl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

How can I create a right to left filled grid using RecyclerView?

Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51

5 Answers5

77

Create a class that extends GridLayoutMAnager ,and override the isLayoutRTL() method like this:

public class RtlGridLayoutManager extends GridLayoutManager {

    public RtlGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public RtlGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public RtlGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }

    @Override
    protected boolean isLayoutRTL(){
        return true;
    }
}
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Mohammad Ranjbar Z
  • 1,487
  • 1
  • 10
  • 20
  • 5
    Thanks Mohammad! Solved the problem beautifully. – Ashkan Sarlak Oct 05 '15 at 12:42
  • Thanks! Actually, [`isLayoutRTL()`](https://developer.android.com/intl/zh-tw/reference/android/support/v7/widget/LinearLayoutManager.html#isLayoutRTL()) is a documented protected method of **GridLayoutManager**, therefore this solution is forward-compatible. – Alex Cohn Oct 06 '15 at 06:33
  • Thanks Mohammad, that was great. – Armin Ghoreishi Feb 12 '16 at 16:26
  • 1
    Consider to use RecyclerView 24.2.0 and upper for better results. According to [GridLayoutManager documentation] (https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.LayoutParams.html#getSpanIndex()) RTL handling is better than before. I had some issues before on calling `notifyDataSetChanged()`. – Navid Eivazzadeh Sep 27 '16 at 07:52
  • is it safe to use? Doesn't need API 17+ ? – Alireza Noorali Dec 12 '18 at 06:23
  • thanks it worked . if we have to write class so what's the job of android:layoutDirection="rtl"? – The MJ Jun 20 '21 at 11:42
12

The answer by Mohammad is true but you do not need to create a new class. you can simply override isLayoutRTL method.

GridLayoutManager lm = new GridLayoutManager(this, 2) {
    @Override
    protected boolean isLayoutRTL() {
        return true;
    }
};
Emad Razavi
  • 1,903
  • 2
  • 17
  • 24
5

There is a better way to do it

You can programmatically change the layout direction of the RecycleView

workHourRecycler = view.findViewById(R.id.market_hours_recycler);
workHourRecycler.setLayoutManager(new GridLayoutManager(getContext(),4));

//Programtically set the direction
workHourRecycler.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
  • It works fine, you juste have to call `ViewCompat.setLayoutDirection(workHourRecycler, View. LAYOUT_DIRECTION_RTL)` – Tijee Mar 27 '19 at 16:02
  • `ViewCompat.setLayoutDirection(View, int)` does nothing on api < 17. Using ViewCompat just makes it so it doesn't throw an exception on api levels that doesn't support it. – Subaru Tashiro Apr 23 '19 at 06:05
1

On the face of it, GridLayouManager should fill rows from right when getLayoutDirection() gives ViewCompat.LAYOUT_DIRECTION_RTL. Normally, this direction will be inherited from the RecyclerView's container.

But if this does not work as expected, or you need to push it down to API 16, or you some device with bad implementation of RTL support, you can simply pull the GridLayoutManager from github, and use a custom manager that you tune to your liking.

It may be enough to extend the stock GridLayoutManager and override layoutChunk().

Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
-6

Its very simple, just call method setReverseLayout as,

GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
layoutManager.setReverseLayout(true);
Eltaf Hussain
  • 3,071
  • 1
  • 14
  • 20