Home > Blockchain >  what's the simplest way to implement an item grid of two columns in android studio
what's the simplest way to implement an item grid of two columns in android studio

Time:12-26

some guides say recyclerview. some say gridview. some say recyclerview with grid adapter...

i have no idea what these methods have in differences as i'm still new to android development. i've tried them all but somehow always ended up messing things up. can someone explain a simple way please? (very preferably in kotlin) i know how to make an item but mostly struggle with the adapter (specially considering most guides for it are written in java and i can barely even read Kotlin). i've been trying to do it for a week so i would be really grateful.

CodePudding user response:

You will want to use a regular RecyclerView and any RecyclerView.Adapter will work. The key is the LayoutManager you give to the RecyclerView. If you give it a GridLayoutManager it will ask how many items per row, its actually fairly simple.

Here is a modified excerpt from the article below.

RecyclerView recyclerView = findViewById(R.id.recyclerView);
// set a GridLayoutManager with 2 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // set Horizontal Orientation
recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView

Here is a pretty in-depth article showing you how to do it. The article also has a simple implementation of an Adapter as-well. https://abhiandroid.com/materialdesign/recyclerview-gridview.html

A fair warning though the article uses some outdated libraries you should be using androidx for nearly everything. Luckily the implementation is not different.

CodePudding user response:

I decided to follow this extremely simple and relatively up-to-date tutorial: https://www.youtube.com/watch?v=HtwDXRWjMcU&ab_channel=PhilippLackner

it doesn't implement it as a grid but i can just do that by changing the grid layout in the very last step from LinearLayoutManager to GridLayoutManager (while filling the first parameter with context and the second with the number of columns i want)

i do have to design my item layout differently (he designed his for a linear layout) but that's easy too. just followed the layout in this tutorial https://www.youtube.com/watch?v=aRgSrJO40z8&t=395s&ab_channel=Foxandroid

  • Related