How can I fill RecyclerView with GridLayoutManager from right to left
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;
}
}
How to change order of RecyclerView from right to left?
override the isLayoutRTL method of GridLayoutManager Object,like this:
GridLayoutManager gl= new GridLayoutManager(context , 4)
{
@Override
protected boolean isLayoutRTL() {
return true;
}
};
RecyclerView Grow Element From Right to Left
it is pretty simple, just call setReverseLayout(true) for your LayoutManager :
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
layoutManager.setReverseLayout(true);
it is explained in its documentation :
/**
* Used to reverse item traversal and layout order.
* This behaves similar to the layout change for RTL views. When set to true, first item is
* laid out at the end of the UI, second item is laid out before it etc.
*
* For horizontal layouts, it depends on the layout direction.
* When set to true, If {@link android.support.v7.widget.RecyclerView} is LTR, than it will
* layout from RTL, if {@link android.support.v7.widget.RecyclerView}} is RTL, it will layout
* from LTR.
*
* If you are looking for the exact same behavior of
* {@link android.widget.AbsListView#setStackFromBottom(boolean)}, use
* {@link #setStackFromEnd(boolean)}
*/
public void setReverseLayout(boolean reverseLayout) {
assertNotInLayoutOrScroll(null);
if (reverseLayout == mReverseLayout) {
return;
}
mReverseLayout = reverseLayout;
requestLayout();
}
How to create rtl GridLayoutManager in Android?
You need to 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);
Android Recyclerview GridLayoutManager column spacing
RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can use
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildLayoutPosition(view) == 0) {
outRect.top = space;
} else {
outRect.top = 0;
}
}
}
Then add it via
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
mRecyclerView.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
Related Topics
Android Marshmallow: Changing Permissions at Run Time Crashes App
How to Display Map (Google) on a Phonegap Android Application
Configuration on Demand Is Not Supported by the Current Version of the Android Gradle Plugin
How to Apply Corner Radius to Linearlayout
Android - Copy Existing Project with a New Name in Eclipse
Android: Cloning a Drawable in Order to Make a Statelistdrawable with Filters
Defining a Percentage Width for a Linearlayout
Android Emulator VS Real Device
Android - Margins Specified in Custom Style Not Taking Effect
Android: Showing Wrong Screen Resolution
Android.Os.Networkonmainthreadexception . Need to Use Async Task
Batch Scripting - If Exist ./Sdcard/File.Any Using Adb
Ios/Android Cross Platform Development
Add a New Item to Recyclerview Programmatically
Connectivity_Action Intent Received Twice When Wifi Connected