如何使用 GridLayoutManager 从右到左填充 RecyclerView [英] How can I fill RecyclerView with GridLayoutManager from right to left

查看:28
本文介绍了如何使用 GridLayoutManager 从右到左填充 RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GridLayoutManager 将一些数据填充到 RecyclerView 中:

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.

但是我正在开发的应用程序是为 RTL 语言设计的,所以我需要让它从从右到左填充.

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

我试图将最后一个参数更改为 true.但它只是将 RecyclerView 一直向下滚动.

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

还为 RecyclerView 设置 layoutDirection 属性不起作用(不考虑我的最小 API 是 16 并且为 API17+ 添加了此属性):

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"/>

如何使用 RecyclerView 创建从右到左填充的网格?

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

推荐答案

创建一个扩展 GridLayoutMAnager 的类,并像这样覆盖 isLayoutRTL() 方法:

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;
    }
}

这篇关于如何使用 GridLayoutManager 从右到左填充 RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆