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
?
Create a class that extends
GridLayoutMAnager
,and override theisLayoutRTL()
method like this:The answer by Mohammad is true but you do not need to create a new class. you can simply override isLayoutRTL method.
Its very simple, just call method
setReverseLayout
as,On the face of it, GridLayouManager should fill rows from right when
getLayoutDirection()
givesViewCompat.LAYOUT_DIRECTION_RTL
. Normally, this direction will be inherited from theRecyclerView'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().There is a better way to do it
You can programmatically change the layout direction of the RecycleView