Recylcer view is an updated version of ListView.
Recylcer View can also be used to create Horizontal ListView by setting LinearLayoutManager's Orientation to HORIZONTAL, it can be done either by setting it in a method or while creating an instance.
Initilize Recycler View
fragment_main.xml
Set LayoutManager that Recycler View will use. This has lot of other benifits which will be cover later.
Using this we can set the RecyclerView to Horizontal or Vertical.
Sets the ItemAnimator that will handle animations involving changes to the items in this RecyclerView.
Hence,
Creating an adapter for RecyclerView. This is bit different from before.
Recycler view's adapter accepts
Extend your ListAdapterHolder with RecylcerView.Adapter and pass your ViewHolder extending RecyclerView's ViewHolder.
Add Unimplemented Methods and a constructor.
Inflate the view. To know which overloaded method to be used while inflating, do have a look at the best article.
That's it.
Recylcer View can also be used to create Horizontal ListView by setting LinearLayoutManager's Orientation to HORIZONTAL, it can be done either by setting it in a method or while creating an instance.
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
or
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
Initilize Recycler View
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="vee.android.sample.recycler.view.MainActivity$PlaceholderFragment" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#FFE5E5E5"
android:scrollbarStyle="insideOverlay" />
</RelativeLayout>
Setting hasFixedSize to true will increase the performance of Recylcer, but one has to careful while using it, It has to be set to true only if the size of the adapter doesn't change.mRecyclerView.setHasFixedSize(true);
Set LayoutManager that Recycler View will use. This has lot of other benifits which will be cover later.
Using this we can set the RecyclerView to Horizontal or Vertical.
mRecyclerView.setLayoutManager(mListLayoutManager);
Sets the ItemAnimator that will handle animations involving changes to the items in this RecyclerView.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
Hence,
mRecyclerView.setHasFixedSize(true);
mListLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mListLayoutManager);
ListAdapterHolder adapter = new ListAdapterHolder(mActivity);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
Creating an adapter for RecyclerView. This is bit different from before.
Recycler view's adapter accepts
RecyclerView.Adapter<VH extends ViewHolder>
onlyListAdapterHolder
public class ListAdapterHolder extends RecyclerView.Adapter<ListAdapterHolder.ViewHolder>{
public class ViewHolder extends RecyclerView.ViewHolder {
}
}
Extend your ListAdapterHolder with RecylcerView.Adapter and pass your ViewHolder extending RecyclerView's ViewHolder.
Add Unimplemented Methods and a constructor.
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
public class ListAdapterHolder extends RecyclerView.Adapter<ListAdapterHolder.ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
return null;
}
@Override
public void onBindViewHolder(ViewHolder holder , int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
}
Return the number of list Items "getItemCount"
@Override
public int getItemCount() {
return count;
}
Inflate your view "onCreateViewHolder"
Inflate the view. To know which overloaded method to be used while inflating, do have a look at the best article.
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
final View sView = mInflater.inflate(R.layout.single_list_item, parent, false);
return new ViewHolder(sView);
}
Declare and intiate views in "ViewHolder"
public ViewHolder(View view) {
super(view);
vName = (TextView) view.findViewById(R.id.list_name);
vSex = (TextView) view.findViewById(R.id.list_sex);
vId = (TextView) view.findViewById(R.id.list_id);
vAge = (TextView) view.findViewById(R.id.list_age);
}
Layout for ListView/RecyclerView
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="4" >
<TextView
android:id="@+id/list_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/list_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/list_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Add data to your view "onBindViewHolder"
@Override
public void onBindViewHolder(ViewHolder holder , int position) {
holder.vId.setText("ID: " + mUserDetails.get(position).getId());
holder.vName.setText("Name: " + mUserDetails.get(position).getName());
holder.vSex.setText("Sex: " + mUserDetails.get(position).getSex());
holder.vAge.setText("Age: " + mUserDetails.get(position).getAge());
}
That's it.