In this post, You will learn about useful knowledge of RecycleView android studio. This implementation example of RecyclerView contains one image view with two textView.
The following picture gives the idea how the RecyclerView works. The RecycerView contain the layout_Manager. The adapter takes data from data source and maps the data into the Layout manager and RecyclerView is created.
Prerequisite
“It will good to cover my previous articles before this. Its help to learn more about Android development. In which I explained.”
- checkbox and spinner in android how created.
- Login Window using shared preference.
- Android Different Layout Example.
You will learn in this section
- How to set dependency
- Create a layout Manager
- Set the Adapter
- View holder pattern
RecyclerView Android
- The RecyclerView android is an advanced form of ListView.
- A RecylcerView is A ViewGrope.
- A flexible view providing a limited view into large datasets.
- Automatically scrolling the data when it moves to beyond the capacity
- When we have a very large data set and shown many UI elements then using listview is not a good idea. that why highly recommended to use RecyclerView.
- It gives an advanced form of listView.
- RecyclerView gives to maximum customization.
- You want to put inside the RecyclerView radio button checkbox many more you want.
- Does not create a new view for each row.
- If a row does not display any more it will be recycled and only content will be change.
The recycleView show the limited number of object in the screen. The fixed number of the object is a show on screen when you will scrolling up or scrolling down data are shown only the object that is on screen. It’s only depending on screen size.
Example: If a screen size displays fifteen objects on screen and recycleView contains one hundred or one thousand objects. The data will be shown on scrolling up and scrolling down on this fifteen objects on the screen.
This is a major difference between listview and recyclerView in term of performance is. The image that what time is required to load one hundred objects or what time is required to load fifteen objects.
How to do this work
- Add the supporting library into Grade file.
- Adding the RecyclerView in XML.
- Create a layout file for a single item inside the RecyclerView.
- Built an Adapter how to display a data for each item.
- Define the data source which you want to choose(Database, ArrayList , Jason or XML).
- After that use on CreateViewHolder link the adapter.
- OnBindViewHolder to show the data inside the adapter.
Dependencies section
The first work that is you doing to implementation RecyclerView Android Studio is that to set the supporting library. You can implement the RecycledView that can run in the older device by adding the following lines in Dependencies section your project built grade file.
1 2 | compile "com.android.support:appcompat-v7:21.0.+" compile 'com.android.support:recyclerview-v7:21.0.+ |
“Open <res> click on <values> than open <string.xml>”<Paste the following code> File:string.xml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string-array name="demo"> <item>A</item> <item>B</item> <item>C</item> <item>D</item> <item>E</item> </string-array> <string-array name="demo1"> <item>RED</item> <item>BLUE</item> <item>Black</item> <item>White</item> <item>Yellow</item> </string-array> </resources> |
Adding the RecyclerView into layout_Xml. After that set the layout width and height match parent. Define the specific _id for this RecyclerView.
“<Paste the following code> File:activity_main.xml.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:id="@+id/rrecycle" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </RelativeLayout> |
Next step adds all the images that are displayed in RecyclerView. Open <Resourse folder> inside the Resource folder paste all the image.
for a single_row a layout is created.
- In single_row layout add the image view and set the height and width.
- The main goal set all the properties like padding image _id margin left and margin right of the image.
- After that same thing is doing for the textView that is the font of the image view.
- One thing remains to add the horizontal line inside the image view and textViews.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="110dp" > <ImageView android:layout_width="100dp" android:layout_height="80dp" android:id="@+id/image" android:layout_alignParentLeft="true" android:src="@drawable/uyt" android:padding="4dp" /> <TextView android:layout_width="170dp" android:layout_height="100dp" android:text="Day" android:gravity="center" android:textSize="15dp" android:layout_toRightOf="@+id/image" android:textStyle="bold" android:id="@+id/text" /> <TextView android:layout_width="170dp" android:layout_height="100dp" android:text="Country" android:gravity="center" android:textSize="15dp" android:layout_toRightOf="@+id/text" android:textStyle="bold" android:id="@+id/text2" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#FF0000" android:layout_above="@+id/text2" android:layout_alignParentBottom="true" /> </RelativeLayout> |
“<Paste the following code on an activity that hosts the layout> File: DataProvider”
Define some variable and set the getter and setter method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class DataProvider { DataProvider(int ima,String text, String text2){ this.setIma(ima); this.setText(text); this.setText2(text2); } private int ima; private String text,text2; public int getIma() { return ima; } public void setIma(int ima) { this.ima = ima; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getText2() { return text2; } public void setText2(String text2) { this.text2 = text2; } } |
The implements the adapter of recyclerView android. Create a new class for that extent this class for the RecycleAdapter and implement all the method. The RecycleAdapter class contains three methods. for more details these methods OnCreateViewHolder.
- onCreateViewHolder
- onBindViewHolder
- getItemCount
“<Paste the following code on an activity that hosts the layout> File: RecycleAdapter”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.RecycleViewHolder> { private ArrayList<DataProvider> aarylist=new ArrayList<DataProvider>(); public RecycleAdapter(ArrayList<DataProvider> aarylis){ this.aarylist=aarylis; } @Override public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false); RecycleViewHolder recycleViewHolder= new RecycleViewHolder(view); return recycleViewHolder; } @Override public void onBindViewHolder(RecycleViewHolder holder, int position) { DataProvider dataProvider= aarylist.get(position); holder.imageView.setImageResource(dataProvider.getIma()); holder.text.setText(dataProvider.getText()); holder.text2.setText(dataProvider.getText2()); } @Override public int getItemCount() { return aarylist.size(); } public static class RecycleViewHolder extends RecyclerView.ViewHolder{ ImageView imageView; TextView text,text2; public RecycleViewHolder(View itemView) { super(itemView); imageView=(ImageView) itemView.findViewById(R.id.image); text=(TextView)itemView.findViewById(R.id.text); text2=(TextView)itemView.findViewById(R.id.text2); }}} |
“<Paste the following code on an activity that hosts the layout> File: MainActivity”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.widget.LinearLayout; import java.util.ArrayList; public class MainActivity extends Activity { RecyclerView recyclerView; RecyclerView.Adapter adapter; RecyclerView.LayoutManager manager; String[] de,d2; int [] imag_aa={R.drawable.aaa,R.drawable.ccc,R.drawable.zz,R.drawable.aaa,R.drawable.fff,}; ArrayList<DataProvider> arrayList=new ArrayList<DataProvider>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView=(RecyclerView)findViewById(R.id.rrecycle); de=getResources().getStringArray(R.array.demo); d2=getResources().getStringArray(R.array.demo1); int i=0; for ( String name : de ){ DataProvider dataProvider=new DataProvider(imag_aa[i],name,d2[i]); arrayList.add(dataProvider); i++; } adapter=new RecycleAdapter(arrayList); recyclerView.setHasFixedSize(true); manager=new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); recyclerView.setAdapter(adapter); }} |
Let run the application and see the output.
- Go to the toolbar, find the run button and click ok.
- The out contains the one image view and two text view.
- The inside these three component horizontal line.
- You will also customize this code by adding more textView and images according to your scenario.
Android RecyclerView is the propelled adaptation of ListView for more enhancements and highlights. You can make a connected rundown with ListView however with RecyclerView Reuses cells while looking over Invigorate to list things. Change the thing compartment Divider between two thingsNumerous views in a similar posting.
wrap up
I trust this article gave you great learning on nuts and bolts of RecyclerView. The subsequent stage is to pursue the underneath articles that clarifies. This is a great site that explores the different topic of android. In this post, I had explained the first challenge of RecycerView android dependency in Grade file. After that, I explained the RecycerView layout. I hope you got the idea. The next post will we describe the importance of card view. for more android RecylerView.