I would like the gridview to look like this
The way I recommend it is to use the RecyclerView
along with StaggeredGridLayoutManager
.
You will have something like:
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager staggeredLayoutManager =
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recycler.setLayoutManager(staggeredLayoutManager);
Where the first parameter of StaggeredGridLayoutManager
is the number of columns.
Of course, you can configure the strategy to fill in gaps.
Then you can use the setGapStrategy
method with one of these constants:
GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS - Não tem documentação, mas ele provavelmente move os itens para ocupar os espaços
GAP_HANDLING_LAZY - Só ocupa os gaps quando houver scroll
GAP_HANDLING_NONE - Não faz nada, deixa o gap
Another question is spans
, in LayoutParams
of the list item, you can define whether the item uses all spans
or not, that is, whether it occupies the entire row or not.
Then just use it inside your Adapter
o LayoutParams.setFullSpan(boolean)
.
Of course you will have to implement a RecyclerView.Adapter
, which is slightly different from implementing BaseAdapter
for example.
For more details I recommend these sources: