recyclerview with grid layout in android code example
Example 1: how to set recycler adapter in gridview
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
Example 2: android recyclerview how to combine row and grid
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a grid layout with two columns
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
// Create a custom SpanSizeLookup where the first item spans both columns
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new MyGridAdapter());
}