Ash*_*lak 27 android right-to-left gridlayoutmanager android-recyclerview
我试图填补一些数据到一个RecyclerView与GridLayoutManager:
GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
Run Code Online (Sandbox Code Playgroud)
这将从左到右将数据填充到网格中.第一项将被放入左上角等地方.
但我正在开发的应用程序是专为RTL语言设计的,所以我需要从右到左填充它.
我试图改变最后一个参数true.但它只是RecyclerView一直向下滚动.
设置layoutDirection属性RecyclerView也不起作用(不考虑我的min API为16,并且为API17 +添加了此属性):
<android.support.v7.widget.RecyclerView
android:id="@+id/grid"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)
如何使用RecyclerView?创建一个右到左填充网格?
Moh*_*r Z 66
创建一个扩展的类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;
}
}
Run Code Online (Sandbox Code Playgroud)
Emi*_*Raz 11
Mohammad 的答案是正确的,但您不需要创建新类。您可以简单地覆盖 isLayoutRTL 方法。
GridLayoutManager lm = new GridLayoutManager(this, 2) {
@Override
protected boolean isLayoutRTL() {
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
有更好的方法来做到这一点
您可以以编程方式更改 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5703 次 |
| 最近记录: |