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.