RecyclerView
The RecyclerView widget is a more advanced and
flexible version of ListView . This widget is a container for
displaying large data sets that can be scrolled very efficiently by
maintaining a limited number of views.
Will see simple example how to display country name and population using RecyclerView.
1. For recyclerview we need to add support library inside the
build.gradla(module:app),and then Syc project.
compile 'com.android.support:recyclerview-v7:24.1.1'
2.Add RecyclerView in the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.falgee.recycler.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</RelativeLayout>
3.Create java class with 2 variables country and population.
public class Country {
protected String name;
protected double population;
public Country(String name, double population) {
this.name = name;
this.population = population;
}
}
4.Create Adapter class for set the value to each item.
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.MyViewHolder> { private ArrayList<Country> countryList; /** * View holder class * */ public class MyViewHolder extends RecyclerView.ViewHolder { public TextView countryText; public TextView popText; public MyViewHolder(View view) { super(view); countryText = (TextView) view.findViewById(R.id.countryName); popText = (TextView) view.findViewById(R.id.pop); } } public CountryAdapter(ArrayList<Country> countryList) { this.countryList = countryList; } @Override public void onBindViewHolder(MyViewHolder holder, int position) { Country c = countryList.get(position); holder.countryText.setText(c.name); holder.popText.setText(String.valueOf(c.population)); } @Override public int getItemCount() { return countryList.size(); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.row,parent, false); return new MyViewHolder(v); } }
5.We need to create item layout with 2 TextView for Display name and population.
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countryName"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pop"/>
</LinearLayout>
6.Go to MainActivity and we have to initialize RecyclerView.
And set the value for country and population. And set value to recycleview.
import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; public class MainActivity extends Activity { ArrayList<Country> countryList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView rv = (RecyclerView) findViewById(R.id.recycler_view); Country c=new Country("India","1,210,193,422"); Country c1=new Country("China","1,354,040,000"); Country c2=new Country("United States","315,761,000"); Country c3=new Country("Indonesia","237,641,326"); countryList=new ArrayList<Country>(); countryList.add(c); countryList.add(c1); countryList.add(c2); countryList.add(c3); CountryAdapter ca = new CountryAdapter(countryList); rv.setAdapter(ca); LinearLayoutManager llm = new LinearLayoutManager(this); llm.setOrientation(LinearLayoutManager.VERTICAL); rv.setLayoutManager(llm); } }
7.Run and see the result in your Screen.
Thanks for this information on software testing training. Its very helpful and it cleared all my doubts on software testing training. Waiting for your next article.
ReplyDeletesoftware testing training
This comment has been removed by the author.
ReplyDeleteGreat post with wonderful piece of information. Keep us updated with more such posts.
ReplyDeleteVMware Training in Chennai
VMware Training in Velachery
Oracle Training in chennai
C C++ Training in Chennai
IoT Training in Chennai
Manual Testing Training in Chennai
It's a wonderful post and very helpful, thanks for all this information about android.You are including better information regarding this topic in an effective way.Thank you so much.
ReplyDeleteSelenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!
ReplyDeleteC and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery