Last Updated: February 25, 2016
·
1.174K
· wannabegeekboy

Recycler View

Last month in Google IO'14 5000 new APIs are released and in that one interesting thing is RecyclerView (widget).

RecyclerView : What the hell is that ?
The RecyclerView is a new ViewGroup that is prepared to render any adapter-based view, its very similar to a list view.

This helps in recycling a larger set of views and makes the scrolling smooth and efficient. To increase the performance in the ListView we use View holder pattern. RecyclerView enforces this pattern to the internally.

There is a support library release for backward compatibility, so we can make use of support-v7 version.

RecyclerView.Adapter
RecyclerView includes a new kind of adapter. It’s a similar approach to the ones you already used, but with some differences..

We have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view. The good thing about this is that first method is called only when we really need to create a new view.

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { private List<ViewModel> items; private int itemLayout; public MyRecyclerAdapter(List<ViewModel> items, int itemLayout) { this.items = items; this.itemLayout = itemLayout; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { ViewModel item = items.get(position); holder.text.setText(item.getText()); holder.image.setImageBitmap(null); Picasso.with(holder.image.getContext()).cancelRequest(holder.image); Picasso.with(holder.image.getContext()).load(item.getImage()).into(holder.image); holder.itemView.setTag(item); } @Override public int getItemCount() { return items.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public ImageView image; public TextView text; public ViewHolder(View itemView) { super(itemView); image = (ImageView) itemView.findViewById(Page on r.id.image); text = (TextView) itemView.findViewById(Page on r.id.text); } } }

LayoutManager
This is the class that will decide in which part of the screen the views are placed. But that’s only one of its many responsibilities. It must be able to manage scrolling and recycling among others.

ItemAnimator
ItemAnimator will animate ViewGroup modifications that are notified to adapter. Basically it will automatically animate adding and removing items. That’s not an easy class either, and we find a DefaultItemAnimator that works quite well.

Setting up and using a RecylerView

If we want to use recyclerView, we can use it as below,

RecyclerView recyclerView = (RecyclerView) findViewById(Page on r.id.list); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(new MyRecyclerAdapter(createMockList(), R.layout.item)); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setItemAnimator(new DefaultItemAnimator());