Android: A basic RecyclerView

The RecyclerView is a more efficient version of GridView and ListView. With animated bells and whistles. And it works with CoordinatorLayout. You'll want to use it.

Although it's a large topic, here's a basic RecyclerView implementation. First the initialisation code and the XML:

RecyclerView rc = (RecyclerView) layout.findViewById(;
rc.setAdapter(new MyRecycler());
LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);

The initialisation code is very similar to ListView's. But we have to set the LayoutManager.


The XML is just about the same as for a ListView.

The adapter is different, however:

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

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;

    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText("Stuff, innit. " + position);

    public int getItemCount() {
        return 40;

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        public ViewHolder(View itemView) {
            textView = (TextView) itemView.findViewById(;

The first difference is it forces the ViewHolder pattern. The inner class is the generic class which extends the Adapter. It fetched View's from a passed-in View and makes such available as public fields.

Then onCreateViewHolder inflates a layout, and sets the ViewHolder with it. onBindViewHolder sets the holder with values, mostly likely values passed in to the adapter in a real world situation.

android android-recyclerview

Edit on github
comments powered by Disqus
Click me