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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章