Since the introduction of RecyclerView in Android, it has gradually replaced the ListView and GridView. This article is very simple, written for record and memo. If I can help you, that would be great.
Control the width of RecyclerView item is not so clear, as you can see from the previous picture.
- The above is actually a Grid layout
- Each item in the first three lines is divided into the width of RecyclerView
- Others in the last line is about a third, Flipboard two-thirds.
The above picture and description is what we want to achieve today.
The method is 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; }}});Copy the code
- A spanCount is passed in the GridLayoutManager constructor, where it has a value of 3
- In the getSpanSize method, the last item occupies two spans and the others occupy one span
Full sample source code
- recyclerview_span_size