Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)

autofit gridlayoutmanager recyclerview android
simple recyclerview android example
recyclerview gridview android example androidhive
custom recyclerview android example
android recyclerview grid spacing
recyclerview gridlayoutmanager autofit
android recyclerview gridlayoutmanager column width
androidx.recyclerview.widget.recyclerview example

I know that RecyclerView has replaced the functionality of the old ListView and GridView. I am looking for a very basic example that shows a minimal grid setup using RecyclerView. I am not looking for long tutorial style explanations, just a minimal example. I imagine the simplest grid that mimics the old GridView would consist of the following features:

  • multiple cells per row
  • single view in each cell
  • responds to click events

Android GridLayoutManager Example, example using RecyclerView with GridLayoutManager (like the old GridView) I am looking for a very basic example that shows a minimal grid setup using  I know that RecyclerView has replaced the functionality of the old ListView and GridView. I am looking for a very basic example that shows a minimal grid setup using RecyclerView. I am not looking for long tutorial style explanations, just a minimal example. I imagine the simplest grid that mimics the old GridView would consist of the following features: multiple cells […]

Although I do like and appreciate Suragch's answer, I would like to leave a note because I found that coding the Adapter (MyRecyclerViewAdapter) to define and expose the Listener method onItemClick isn't the best way to do it, due to not using class encapsulation correctly. So my suggestion is to let the Adapter handle the Listening operations solely (that's his purpose!) and separate those from the Activity that uses the Adapter (MainActivity). So this is how I would set the Adapter class:

MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private String[] mData = new String[0];
    private LayoutInflater mInflater;

    // Data is passed into the constructor
    public MyRecyclerViewAdapter(Context context, String[] data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // Inflates the cell layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    // Binds the data to the textview in each cell
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData[position];
        holder.myTextView.setText(animal);
    }

    // Total number of cells
    @Override
    public int getItemCount() {
        return mData.length;
    }

    // Stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView myTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            myTextView = (TextView) itemView.findViewById(R.id.info_text);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            onItemClick(view, getAdapterPosition());
        }
    }

    // Convenience method for getting data at click position
    public String getItem(int id) {
        return mData[id];
    }

    // Method that executes your code for the action received
    public void onItemClick(View view, int position) {
        Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position);
    }
}

Please note the onItemClick method now defined in MyRecyclerViewAdapter that is the place where you would want to code your tasks for the event/action received.

There is only a small change to be done in order to complete this transformation: the Activity doesn't need to implement MyRecyclerViewAdapter.ItemClickListener anymore, because now that is done completely by the Adapter. This would then be the final modification:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // data to populate the RecyclerView with
        String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"};

        // set up the RecyclerView
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
        int numberOfColumns = 6;
        recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
        adapter = new MyRecyclerViewAdapter(this, data);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);
    }
}

Simple Android grid example using RecyclerView , Android GridLayoutManager example tutorial, android RecyclerView Now let's use a GridLayoutManager to layout the RecyclerView as a grid. setLayoutManager(layoutManager);*/ /** Simple GridLayoutManager that spans two columns **/ GridLayoutManager I am having trouble starting an activity from the gridview. Android RecyclerView with GridView GridLayoutManager example tutorial Juned Mughal June 17, 2016 June 17, 2016 Android Examples Tutorials How to create RecyclerView layout with CardView and TextView inside each gridview item one by one Material Design look.

Yo Have to Set Your recyclerview layoutmanager to Gridlayout Mode , For Do This Just change your code when you Want Set your RecyclerView LayoutManager :

Note : replace Your Columns Count That you want with ###HELP###

   recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),###HELP###));

Using the RecyclerView, Before getting into grid Layout manager for recycler view example, we should know what is Recycler view in android. Recycler In the above code we have added recycler view to window manager as relative parent layout. RecyclerView with GridLayoutManager Android Tutorial with Example Vishal Shrestha 6:08 AM 60 Minute Projects , Android Today we will be making a Grid View like list using a RecyclerView with GridLayoutManager as shown in the Video below, but let's first talk a little bit about recyclerView; as you might already know a RecyclerView is more

RecyclerView Using GridLayoutManager With Example In Android , It is supposed to be the successor of ListView and GridView. One of the If you want to use a RecyclerView , you will need to work with the following: GridLayoutManager shows items in a grid. Use this Migration guide to ensure compatibility with the rest of the examples. Callback that accepts the old and new list: Step 6 : Now open app -> java -> package -> MainActivity.java and add the below code. In this step firstly we get the reference of RecyclerView. After that we creates two ArrayList’s for Person Names and Images. After that we set a GridLayoutManager and finally we set the Adapter to show the grid items in RecyclerView.

Simple Android grid example using RecyclerView with , In this example we are using GridLayoutManager with we add the code for creating custom grid items. created using RecyclerView as GridView. Let’s implement RecyclerView using a GridLayoutManager in a new Android Studio project. Android GridLayoutManager Example Project Structure. The project consists of a single Activity : MainActivity.java, an adapter class : RecyclerViewAdapter.java, a DataModel.java class and a custom GridLayoutManager class AutoFitGridLayoutManager.java.

Simple Android grid example using RecyclerView with , For those who are already familiar with setting up a RecyclerView to make a list, the good news is that making a grid is largely the same. Android GridView with Recyclerview. A Recyclerview is an advanced and flexible version of a listview. RecyclerView ViewGroup is a container for larger data set of views that can be recycled and scrolled very efficiently. For using RecyclerView you need to specify an adapter and layout manager also.

Comments
  • @MarianPaździoch, Yes, I just made this as a minimal example. It could definitely use some beautification work. I'll try to update this answer some time in the future.
  • I logged in just to point that people like you have kept this portal alive and kicking .i was stuck on this for two days before seeing this solution. thanks a lot
  • @androiddeveloper, The grid items get laid out left to right, top to bottom. Scrolling is vertical when there are more items than can fit on the screen.
  • Future readers, let me save you some time, key thing is recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
  • @daka, good point. I edited my answer to include this in the beginning.
  • What if the activity does need to listen to the click events? e.g. passing data to presenter, doing some logic based on item clicked, tracking, etc.
  • I agree that Adapter should handle click events, since it has the items with the data in it. @AhmadFadli if you need to do some work in the adapter's host (a Fragment or Activity) you should create a callback interface with methods you need. Your host implements this interface. And then you pass an instance of your host into Adapter's constructor. Having the instance of the host you can call it's methods when you need from your Adapter. And your host we get callbacks called. This is often used when you need to work with ActionMode. When you longClick to select items and use ActionBar buttons.
  • I disagree and think that click events should be processed in the hosting Activity. Because only it's click listener can know about the Activity views and other Fragments, Activities, etc. The adapter can only send click events to upper level. It should have the interface ItemClickListener with so many events, as many events adapter's views can produce. This solution was written even earlier: stackoverflow.com/a/40563598/2914140.