Android has introduced a new widget for displaying data collections which is a more advanced and flexible version of ListView. RecyclerView incorporates ListView and ViewHolder reusability concept very efficiently.
RecyclerView is very useful with large data collections whose content changes at runtime.
RecyclerView includes:
- Layout managers for positioning items.
- Default animatios for common item operations , such as removal or addition of items.
Layout Manager:
- LinearLayoutManager shows items in a vertical or horizontal scrolling list.
- GridLayoutManage shows items in a grid.
- StaggeredGridLayoutManager shows items in a staggered grid.
For custom layout manager, extend the RecyclerView.LayoutManager class.
Animation:
- Animations for adding and removing items are enabled by default.
- For custom animation, extend the RecyclerView.ItemAnimator class and use the RecyclerView.setItemAnimator() method.
RecyclerView Demo:
- Create a new Android project.
- Add the v7 support library, for eclipse follow the link and for gradle add:
dependencies{
compile ‘com.android.support:recyclerview-v7:21.0.+’
}3. Add RecyclerView in xml:
<android.support.v7.widget.RecyclerView android:id="@+id/recylcer_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" />
4. Create Adapter file MyRecyclerViewAdapter.java by extending RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{ private String[] mData; public MyRecyclerViewAdapter(String[] mData) { this.mData = mData; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.list_item_layout, viewGroup, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.mTextView.setText(mData[i]); } @Override public int getItemCount() { if (mData != null) return mData.length; return 0; } public static class ViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case public TextView mTextView; public ViewHolder(View itemView) { super(itemView); mTextView = (TextView)itemView.findViewById(R.id.txt);; } } }
5. Create Activity MyActivity.java
public class MyActivity extends Activity implements View.OnClickListener{ private RecyclerView mRecyclerView; private MyRecyclerViewAdapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; private GridLayoutManager mGridLayoutManager; private StaggeredGridLayoutManager mStaggeredGridLayoutManager; private String[] mSampleData = {"What is Android Lollipop?", "What is Android 5.0?", "What is RecyclerView?", "What is difference between RecyclerView and ListView?", "How does this demo app help?"};@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mRecyclerView = (RecyclerView) findViewById(R.id.recylcer_view); mRecyclerView.setHasFixedSize(true); mAdapter = new MyRecyclerViewAdapter(mSampleData); mRecyclerView.setAdapter(mAdapter); applyLinearLayoutManager(); } private void applyLinearLayoutManager(){ mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); } private void applyGridLayoutManager(){ mGridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(mGridLayoutManager); } private void applyStaggeredGridLayoutManager(){ mStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(mStaggeredGridLayoutManager); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_linear) applyLinearLayoutManager(); else if (id == R.id.action_grid) applyGridLayoutManager(); else if (id == R.id.action_staggered) applyStaggeredGridLayoutManager(); return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { int pos = mRecyclerView.getChildPosition(v); Toast.makeText(getApplicationContext(), mSampleData[pos], Toast.LENGTH_LONG).show(); } }