How can I fill RecyclerView with GridLayoutManager from right to left

前端 未结 5 556
别那么骄傲
别那么骄傲 2020-11-29 13:06

I\'m trying to fill some data into a RecyclerView with GridLayoutManager:

GridLayoutManager layoutManager = new GridLayoutManager(t         


        
相关标签:
5条回答
  • 2020-11-29 13:35

    On the face of it, GridLayouManager should fill rows from right when getLayoutDirection() gives ViewCompat.LAYOUT_DIRECTION_RTL. Normally, this direction will be inherited from the RecyclerView's container.

    But if this does not work as expected, or you need to push it down to API 16, or you some device with bad implementation of RTL support, you can simply pull the GridLayoutManager from github, and use a custom manager that you tune to your liking.

    It may be enough to extend the stock GridLayoutManager and override layoutChunk().

    0 讨论(0)
  • 2020-11-29 13:37

    The answer by Mohammad is true but you do not need to create a new class. you can simply override isLayoutRTL method.

    GridLayoutManager lm = new GridLayoutManager(this, 2) {
        @Override
        protected boolean isLayoutRTL() {
            return true;
        }
    };
    
    0 讨论(0)
  • 2020-11-29 13:38

    Its very simple, just call method setReverseLayout as,

    GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
    layoutManager.setReverseLayout(true);
    
    0 讨论(0)
  • 2020-11-29 13:45

    There is a better way to do it

    You can programmatically change the layout direction of the 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);
    
    0 讨论(0)
  • 2020-11-29 13:51

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题