Android之如何实现阿拉伯版本(RTL)的recycleView的网格布局

1 问题

比如正常的recycleView的网格布局效果如下

 1 2 3
 4 5 6
 7 8

现在需要变成这样的效果

 3 2 1
 6 5 4
   8 7

2 思考过程和尝试解决方法

1)从recycleView上直接分析,看有没有相关的方法变成这个格式,网上百度了,基本上找不到

2)既然recycleView里面有常见的几种布局设置,我们采用了GridLayoutManager,然后recycleView设置进去了GridLayoutManager这个种布局,我们应该从GridLayoutManager进行思考怎么实现上面的效果。

3)从网上去百度了下有,给GridLayoutManager设置反转。

layoutManager.setReverseLayout(true);

然后再去 recycleView再设置GridLayoutManager,效果如下,很明显不行。

7 8
4 5 6
1 2 3

4)通过继承GridLayoutManager类,重写下面的方法

isLayoutRTL
    class RTLLayoutManager extends GridLayoutManager {

        public RTLLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        public RTLLayoutManager(Context context, int spanCount) {
            super(context, spanCount);
        }

        public RTLLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
            super(context, spanCount, orientation, reverseLayout);
        }

        @Override
        protected boolean isLayoutRTL() {
            //这里如果设置true,说明就是阿拉伯效果,如果设置成false,就是普通的网格效果
            return true;
        }
    }

 

 

3 最终解决办法

        RTLLayoutManager layoutManager = new RTLLayoutManager(this, 9);
        recycleView.setLayoutManager(layoutManager);
发布了1065 篇原创文章 · 获赞 670 · 访问量 300万+

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/104954722