In Android there is the need when you need to display a list of data or simply data that get generated at run-time. Rather than creating your own layout inflator. Android provide Recycler View, there is also a listview which was provided to show data.
After after many experiments with Recycler View here is the tutorial for it. Hope it will be use full to someone.I assume if you are reading this then you know how to add activity in the android studio.
I have divided the article into several parts. Here is the First part.
Open your build.gradle file for (module:app) level
add line compile 'com.android.support:recyclerview-v7:25.3.1' in the dependencies section.
this will download recycler view dependencies in the project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dependencies{compilefileTree(dir:'libs',include:['*.jar'])androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{excludegroup:'com.android.support',module:'support-annotations'})compile'com.android.support:appcompat-v7:25.3.1'compile'com.android.support.constraint:constraint-layout:1.0.2'//Recycler view library -- its version should be similar as appcompact.// It can also be used as//compile 'com.android.support:recyclerview-v7:25.+'compile'com.android.support:recyclerview-v7:25.3.1'testCompile'junit:junit:4.12'}
importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.support.v7.widget.LinearLayoutManager;importandroid.support.v7.widget.RecyclerView;importskd.app.androidfeatures.R;publicclassRecylerActivityextendsAppCompatActivity{//Below are the componets which are required by the Recycler viewRecyclerViewrecyclerView;//this will hold the recycler view from the layoutRecyclerView.AdaptermAdapter;//this will hold the adapter for the recycler viewRecyclerView.LayoutManagermLayoutmanager;//holds the layout managerprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_recyler);//layout which contain the recycler view//Find the Recycler viewrecyclerView=(RecyclerView)findViewById(R.id.my_recycler_view);//got the recycler view from the layout//set the layout manager for the recycler view//standard layout managers( LinearLayoutManager or GridLayoutManager) can be used, or implement your own.//Layout Manager tells how the item are shown in your Recycler ViewmLayoutmanager=newLinearLayoutManager(this);recyclerView.setLayoutManager(mLayoutmanager);//set the data adapter//Adapter contain the Data Which need to be shown in the viewmAdapter=newRvDataAdpt();recyclerView.setAdapter(mAdapter);}}
#Recycler View Item
Now we need to create and recycler item , i.e the layout which can be shown by the recycler view.
Add layout recycler_item.xml
The data Adapter is the component which feeds to the recycler view. In the post I will describe how to set up the basic adapter class to able to show the view. listening events from the view will be decribed in a later post :).
As per Android doc your adapter class must extent RecyclerView.Adapter class from the support library.
importandroid.support.v7.widget.RecyclerView;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importskd.app.androidfeatures.R;/** * THis class will be the data adapter for the recycler view * This class must exxtend the Recycler view adapter. */publicclassRvDataAdptextendsRecyclerView.Adapter{@OverridepublicRecyclerView.ViewHolderonCreateViewHolder(ViewGroupparent,intviewType){returnnull;}@OverridepublicvoidonBindViewHolder(RecyclerView.ViewHolderholder,intposition){}@OverridepublicintgetItemCount(){return0;}}
These methods are need overridden by the base class.
onCreateViewHolder : This method used to construct the view holder and set the view it uses
to display its contents. Typically, it would set the view by inflating an XML layout file.
onBindViewHolder : Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.
getItemCount : Returns the total number of items in the data set held by the adapter.
At this point if you run the application. Recycler view will be visible however with no item.
Now lets try to display our item layout in the recycler.
To display the item in the recycler view we need to inflate our item in the onCreateViewHolder method however. However this method return data of RecyclerView.ViewHolder , so we first need to convert or return our view as ViewHolder type. By creating instance of ViewHolder Class.
To set the count of the item displayed. getItemCount should return value greater than 0.
importandroid.support.v7.widget.RecyclerView;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importskd.app.androidfeatures.R;/** * THis class will be the data adapter for the recycler view * This class must exxtend the Recycler view adapter. */publicclassRvDataAdptextendsRecyclerView.Adapter{@OverridepublicRecyclerView.ViewHolderonCreateViewHolder(ViewGroupparent,intviewType){//lets populate our recyler view with the item created;//get the view from the layout inflator// third parameter is set to false to prevent viewgroup to attach to rootViewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);// as this method need to return the viewHolder type// need to convert our view to the view holderRecyclerView.ViewHolderviewHolder=newRecyclerView.ViewHolder(view){@OverridepublicStringtoString(){returnsuper.toString();}};returnviewHolder;}@OverridepublicvoidonBindViewHolder(RecyclerView.ViewHolderholder,intposition){}@OverridepublicintgetItemCount(){//this defined how many items need to be displayedreturn3000;// to display the 3000 items}}
Now After compiling and running the application you will get something like this.
In the next post I will describe how to use Recycler view with proper items ,with data model and listing to the events of the items.
Comments
Post a comment