RecyclerView GridLayoutManager controls the width of Item

The above picture and description are the effects we want to achieve today.

The method is very simple, mainly using the setSpanSizeLookup method of GridLayoutManager

mLayoutManager = new GridLayoutManager(this, 3);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if (position == mAdapter.getItemCount() - 1) {
            return 2;
        } else {
            return 1;
        }
    }
});
  • A spanCount is passed in the GridLayoutManager constructor, and the value here is 3
  • In the getSpanSize method, the last item occupies 2 spans, and the others occupy one span.

Guess you like

Origin blog.csdn.net/chengzhenjia/article/details/132183904