As per Google Document RecyclerView is a more advanced and flexible version of
ListView
.
This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the
RecyclerView
widget when you have lists with elements that change dynamically.
The RecyclerView has been developed with extensibility in mind, so it is possible to create any kind of layout you can think of, but not without a little pain-in-the-ass dose. This is Android, so things are never easy.
If you want to use a RecyclerView, you will need to feel comfortable with three elements:
- RecyclerView.Adapter- LayoutManager
- ItemAnimator
- A layout manager for positioning items
- Default animations for common item operations
RecyclerView
is easy to use, because it provides:
You also have the flexibility to define custom layout managers and animations for this widget.
To use the
RecyclerView
widget, you have to specify an adapter and a layout manager. To create an adapter, you extend the RecyclerView.Adapter
class. The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.
We can use RecyclerView from Api version 7 in android .
Let me make my first list with RecyclerView and share my code with you all.
Below is my Activity class which use RecycleView to show list items.
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class RecycleActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private ListAdapter mListAdapter;
private ArrayList<String> mArrayList=new ArrayList<String>();
private LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
initView();
}
private void initView(){
mRecyclerView=(RecyclerView)findViewById(R.id.recycle_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mArrayList.clear();
for (int i=0;i<30;i++){
mArrayList.add("Listview Item at Position "+ i);
}
mListAdapter=new ListAdapter(mArrayList);
mRecyclerView.setAdapter(mListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recycle, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my Adapter Class to bind data in to RecycleView
import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListAdapter extends RecyclerView.Adapter<ViewHolder>{
private ArrayList<String> mArrayData;
public ListAdapter(ArrayList<String> mArrayListUpdate){
this.mArrayData=mArrayListUpdate;
}
public static class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{
public ViewHolder(View view) {
super(view);
// TODO Auto-generated constructor stub
mRlRowView=(RelativeLayout) view;
mTxtView=(TextView) mRlRowView.findViewById(R.id.txt_title);
mImgView=(ImageView)mRlRowView.findViewById(R.id.img_row);
mCheckBox=(CheckBox)mRlRowView.findViewById(R.id.checkbox_item);
}
CheckBox mCheckBox;
ImageView mImgView;
TextView mTxtView;
RelativeLayout mRlRowView;
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return mArrayData.size();
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(
android.support.v7.widget.RecyclerView.ViewHolder viewHolder, final int position) {
// TODO Auto-generated method stub
Log.i("LOG_TAG", "onBindViewHolder-->"+ position);
ViewHolder mViewHolder=((ViewHolder)viewHolder);
mViewHolder.mTxtView.setText(mArrayData.get(position));
mViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.w("LOG_TAG", "onCheckedChanged is"+ isChecked + "Position is"+ position);
}
});
}
@Override
public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(
ViewGroup viewGroup, int viewType) {
// TODO Auto-generated method stub
// create a new view
Log.i("LOG_TAG", "onCreateViewHolder-->");
View mView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row_item, viewGroup, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder mViewHolder=new ViewHolder(mView);
return mViewHolder;
}
}
Here is download link for my created Demo :
https://drive.google.com/file/d/0B-8An4Rd1nmKcU93VHU1Y1FWTG8/view?usp=sharing
It's Goog Drive link and from top there is download icons from there you can easily download whole code.
Below is my Activity class which use RecycleView to show list items.
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class RecycleActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private ListAdapter mListAdapter;
private ArrayList<String> mArrayList=new ArrayList<String>();
private LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle);
initView();
}
private void initView(){
mRecyclerView=(RecyclerView)findViewById(R.id.recycle_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mArrayList.clear();
for (int i=0;i<30;i++){
mArrayList.add("Listview Item at Position "+ i);
}
mListAdapter=new ListAdapter(mArrayList);
mRecyclerView.setAdapter(mListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recycle, 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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my Adapter Class to bind data in to RecycleView
import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListAdapter extends RecyclerView.Adapter<ViewHolder>{
private ArrayList<String> mArrayData;
public ListAdapter(ArrayList<String> mArrayListUpdate){
this.mArrayData=mArrayListUpdate;
}
public static class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{
public ViewHolder(View view) {
super(view);
// TODO Auto-generated constructor stub
mRlRowView=(RelativeLayout) view;
mTxtView=(TextView) mRlRowView.findViewById(R.id.txt_title);
mImgView=(ImageView)mRlRowView.findViewById(R.id.img_row);
mCheckBox=(CheckBox)mRlRowView.findViewById(R.id.checkbox_item);
}
CheckBox mCheckBox;
ImageView mImgView;
TextView mTxtView;
RelativeLayout mRlRowView;
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return mArrayData.size();
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(
android.support.v7.widget.RecyclerView.ViewHolder viewHolder, final int position) {
// TODO Auto-generated method stub
Log.i("LOG_TAG", "onBindViewHolder-->"+ position);
ViewHolder mViewHolder=((ViewHolder)viewHolder);
mViewHolder.mTxtView.setText(mArrayData.get(position));
mViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Log.w("LOG_TAG", "onCheckedChanged is"+ isChecked + "Position is"+ position);
}
});
}
@Override
public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(
ViewGroup viewGroup, int viewType) {
// TODO Auto-generated method stub
// create a new view
Log.i("LOG_TAG", "onCreateViewHolder-->");
View mView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row_item, viewGroup, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder mViewHolder=new ViewHolder(mView);
return mViewHolder;
}
}
Here is download link for my created Demo :
https://drive.google.com/file/d/0B-8An4Rd1nmKcU93VHU1Y1FWTG8/view?usp=sharing
It's Goog Drive link and from top there is download icons from there you can easily download whole code.