Search code examples
androidlayout-managergridlayoutmanagerstaggeredgridlayout

GridLayoutManager Customization


i want to know if there is way that i can customize gridlayoutmanager in android to have horizontal layout like this sketch: enter image description here

i tried some layout manager that i found in Github but non of them that help me to implement this kind of layout manager.

something like this:

public class CustomLayoutManager extends StaggeredGridLayoutManager {
public CustomLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public CustomLayoutManager(int spanCount, int orientation) {
    super(spanCount, orientation);
}

@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
    super.onMeasure(recycler, state, widthSpec, heightSpec);
}

@Override
public void setSpanCount(int spanCount) {
    super.setSpanCount(spanCount);
}}

any help will be appreciated thanks


Solution

  • I don't think you need the CustomLayoutManager. Do this to your RecyclerView:

    GridLayoutManager layoutManager = new GridLayoutManager(itemView.getContext(), 2, GridLayoutManager.HORIZONTAL, false);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return position == 0 ? 2 : 1;//put your condition here
        }
    });
    recyclerView.setLayoutManager(layoutManager);