Adding a RecyclerView

suggest change

Add the dependency described in the Remarks section, then add a RecyclerView to your layout:

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Once you have added a RecyclerView widget to your layout, obtain a handle to the object, connect it to a layout manager and attach an adapter for the data to be displayed:

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// set a layout manager (LinearLayoutManager in this example)

mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);

// specify an adapter
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);

Or set the layout manager in XML by adding these lines to the RecyclerView:

xmlns:app="http://schemas.android.com/apk/res-auto"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"

If content changes will not change the RecyclerView’s own size, call setHasFixedSize(true). It can then skip requestLayout() on its parent when the children change. You can call this before setting the LayoutManager and the adapter:

mRecyclerView.setHasFixedSize(true);

RecyclerView provides these built-in layout managers to use. So you can create a list, a grid and a staggered grid using RecyclerView:

  1. LinearLayoutManager shows items in a vertical or horizontal scrolling list.
  2. GridLayoutManager shows items in a grid.
  3. StaggeredGridLayoutManager shows items in a staggered grid.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents