build.gradle
compile 'com.android.support:recyclerview-v7:25.3.1'
Country.java
public class Country { public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCapital() { return capital; } public void setCapital(String capital) { this.capital = capital; } private String name; private String capital; }
country_row.xml
<?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="wrap_content"> <TextView android:id="@+id/textViewCountryName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginEnd="50dp" android:layout_marginLeft="10dp" android:layout_marginRight="50dp" android:layout_marginStart="10dp" android:gravity="center" android:text="Country Name" android:textStyle="bold" android:textAppearance="@android:style/TextAppearance.Large" /> <TextView android:id="@+id/textViewCountryCapital" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toEndOf="@+id/textViewCountryName" android:layout_toRightOf="@+id/textViewCountryName" android:gravity="center" android:text="Capital Name" android:textAppearance="@android:style/TextAppearance.Large" /> </RelativeLayout>
CountryAdapter.java
public class CountryAdapter extends RecyclerView.Adapter<CountryAdapter.CountryViewHolder> { private List<Country> countryList; public CountryAdapter(List<Country> countries) { this.countryList=countries; } @Override public CountryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { Context context = parent.getContext(); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.country_row, parent, false); CountryViewHolder viewHolder=new CountryViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(CountryViewHolder holder, int position) { Country country=countryList.get(position); holder.textViewCountryName.setText(country.getName()); holder.textViewCountryCapital.setText(country.getCapital()); } @Override public int getItemCount() { return this.countryList.size(); } public class CountryViewHolder extends RecyclerView.ViewHolder{ public TextView textViewCountryName; public TextView textViewCountryCapital; public CountryViewHolder(View itemView) { super(itemView); textViewCountryName= (TextView) itemView.findViewById(R.id.textViewCountryName); textViewCountryCapital= (TextView) itemView.findViewById(R.id.textViewCountryCapital); } } }
main_activity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ir.mhdr.a082.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { List<Country> countryList=new ArrayList<>(); private RecyclerView recyclerView; private RecyclerView.Adapter adapter; private RecyclerView.LayoutManager layoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); generateCountries(); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView recyclerView.setHasFixedSize(true); // use a linear layout manager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); // animate items with DefaultItemAnimator recyclerView.setItemAnimator(new DefaultItemAnimator()); // specify an adapter (see also next example) adapter = new CountryAdapter(countryList); recyclerView.setAdapter(adapter); } private void generateCountries() { Country country1=new Country(); country1.setName("Iran"); country1.setCapital("Tehran"); Country country2=new Country(); country2.setName("Netherlands"); country2.setCapital("Amsterdam"); Country country3=new Country(); country3.setName("Turkey"); country3.setCapital("Ankara"); Country country4=new Country(); country4.setName("Ukraine"); country4.setCapital("Kiev"); Country country5=new Country(); country5.setName("Russia"); country5.setCapital("Moscow"); Country country6=new Country(); country6.setName("Chile"); country6.setCapital("Santiago"); Country country7=new Country(); country7.setName("United States"); country7.setCapital("Washington, D.C."); countryList.add(country1); countryList.add(country2); countryList.add(country3); countryList.add(country4); countryList.add(country5); countryList.add(country6); countryList.add(country7); } }
References
https://github.com/mhdr/AndroidSamples/tree/master/082
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/guide/topics/ui/layout/recyclerview.html
https://guides.codepath.com/android/using-the-recyclerview
http://www.androidhive.info/2016/01/android-working-with-recycler-view/