我正在尝试用GridLayoutManager
将一些数据填充到GridLayoutManager
中
GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
这将使数据从从左到右的填充到网格中。第一件物品将放在左上角等地方.
但是我正在开发的应用程序是为一种RTL语言设计的,所以我需要让它从从右到左填充。
我试图将最后一个参数更改为true
。但它只是把RecyclerView
一直往下滚动。
另外,为layoutDirection
设置RecyclerView
属性也不起作用(不考虑我的min为16,该属性是为API17+添加的):
<android.support.v7.widget.RecyclerView
android:id="@+id/grid"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
如何使用RecyclerView
创建从右到左填充的网格
发布于 2015-10-05 11:13:22
创建扩展GridLayoutMAnager
的类,并覆盖如下所示的isLayoutRTL()
方法:
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;
}
}
发布于 2018-09-14 16:28:34
穆罕默德的回答是正确的,但你不需要创建一个新的类。您可以简单地重写isLayoutRTL方法。
GridLayoutManager lm = new GridLayoutManager(this, 2) {
@Override
protected boolean isLayoutRTL() {
return true;
}
};
发布于 2018-04-19 13:19:30
有一个更好的方法
可以以编程方式更改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);
https://stackoverflow.com/questions/32920867
复制相似问题