RecyclerView in Android:
In Android, RecyclerView is a more flexible and advanced version of ListView with some extra functionality. In RecyclerView different components work together to display different types of data. If you want to use ListView and RecyclerView together then any type of list and grid are created very easily.
RecyclerView holds many View holders to display a dynamic content as per user requirements. If the user scrolls the list then RecyclerView rebinds another data which is scrolling onto the screen.
Here, is the simple example how the RecyclerView Work.
If you are implementing RecyclerView in your program then all view holder objects are managed by an adapter, which is created by extending RecyclerView.Adapter class. The Adapter class creates view holder as per user requirements. And adapter also binds view holders with different types of data. For binding data, you can call onBindViewHolder () method.
Android RecyclerView Classes:
RecyclerView.ItemAnimator: By using this class we are easily animating the views, unlike the ListView.
RecyclerView.ItemDecorator: It provides better support for adding borders and dividers.
Android RecyclerView Example:
Add two dependency in Gradle script.
dependencies {
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#fff">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
card_layout.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="wrap_content"
android:orientation="vertical"
android:tag="cards main container">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_marginBottom="-5dp"
card_view:cardBackgroundColor="#fff"
card_view:cardCornerRadius="15dp"
card_view:cardElevation="15dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:tag="image_tag"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="@drawable/img1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/textViewVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Android Version"
android:textColor="#f00"
android:textStyle="bold"
android:textSize="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
layout/menu/ main_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu 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" tools:context=".MainActivity"> <item android:id="@+id/add_item" android:title="Add" android:orderInCategory="100" app:showAsAction="always"/> </menu>
MainActivity.java
package com.example.msclient009.recyclerviewexample; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private static RecyclerView.Adapter adapter; private RecyclerView.LayoutManager layoutManager; private static RecyclerView recyclerView; private static ArrayList<DataModel> data; static View.OnClickListener myOnClickListener; private static ArrayList<Integer> removedItems; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myOnClickListener = new MyOnClickListener(this); recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); data = new ArrayList<DataModel>(); for (int i = 0; i < MyData.nameArray.length; i++) { data.add(new DataModel( MyData.nameArray[i], MyData.versionArray[i], MyData.id_[i], MyData.drawableArray[i] )); } removedItems = new ArrayList<Integer>(); adapter = new AndroidVersionAdapter(data); recyclerView.setAdapter(adapter); } private static class MyOnClickListener implements View.OnClickListener { private final Context context; private MyOnClickListener(Context context) { this.context = context; } @Override public void onClick(View v) { removeItem(v); } private void removeItem(View v) { int selectedItemPosition = recyclerView.getChildPosition(v); RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForPosition(selectedItemPosition); TextView textViewName = (TextView) viewHolder.itemView.findViewById(R.id.textViewName); String selectedName = (String) textViewName.getText(); int selectedItemId = -1; for (int i = 0; i < MyData.nameArray.length; i++) { if (selectedName.equals(MyData.nameArray[i])) { selectedItemId = MyData.id_[i]; } } removedItems.add(selectedItemId); data.remove(selectedItemPosition); adapter.notifyItemRemoved(selectedItemPosition); } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); if (item.getItemId() == R.id.add_item) { //check if any items to add if (removedItems.size() != 0) { addRemovedItemToList(); } else { Toast.makeText(this, "Nothing to add", Toast.LENGTH_SHORT).show(); } } return true; } private void addRemovedItemToList() { int addItemAtListPosition = 3; data.add(addItemAtListPosition, new DataModel( MyData.nameArray[removedItems.get(0)], MyData.versionArray[removedItems.get(0)], MyData.id_[removedItems.get(0)], MyData.drawableArray[removedItems.get(0)] )); adapter.notifyItemInserted(addItemAtListPosition); removedItems.remove(0); } }
AndroidVersionAdapter.java
package com.example.msclient009.recyclerviewexample; 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; /** * Created by msclient009 on 5/19/2018. */ public class AndroidVersionAdapter extends RecyclerView.Adapter<AndroidVersionAdapter.MyViewHolder> { private ArrayList<DataModel> dataSet; public static class MyViewHolder extends RecyclerView.ViewHolder { TextView textViewName; TextView textViewVersion; ImageView imageViewIcon; public MyViewHolder(View itemView) { super(itemView); this.textViewName = (TextView) itemView.findViewById(R.id.textViewName); this.textViewVersion = (TextView) itemView.findViewById(R.id.textViewVersion); this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView); } } public AndroidVersionAdapter(ArrayList<DataModel> data) { this.dataSet = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.card_layout, parent, false); view.setOnClickListener(MainActivity.myOnClickListener); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(final MyViewHolder holder, final int listPosition) { TextView textViewName = holder.textViewName; TextView textViewVersion = holder.textViewVersion; ImageView imageView = holder.imageViewIcon; textViewName.setText(dataSet.get(listPosition).getName()); textViewVersion.setText(dataSet.get(listPosition).getVersion()); imageView.setImageResource(dataSet.get(listPosition).getImage()); } @Override public int getItemCount() { return dataSet.size(); } }
DataMadel.xml
package com.example.msclient009.recyclerviewexample;
public class DataModel { String name; String version; int id_; int image; public DataModel(String name, String version, int id_, int image) { this.name = name; this.version = version; this.id_ = id_; this.image=image; } public String getName() { return name; } public String getVersion() { return version; } public int getImage() { return image; } public int getId() { return id_; } }
MyData.java
package com.example.msclient009.recyclerviewexample;
public class MyData { static String[] nameArray = {"Android P","Oreo","Nougat","Marshmallow", "Lollipop","Kitkat", "JellyBean", "Ice Cream Sandwich", "Honeycomb", "Gingerbread", "Froyo", "Eclair","Donut", "Cupcake", }; static String[] versionArray = {"9", "8.0 – 8.1", "7.0 – 7.1.2", "6.0 – 6.0.1", "5.0 – 5.1.1", "4.4 – 4.4.4", "4.1 – 4.3.1","4.0 – 4.0.4", "3.0 – 3.2.6", "2.3 – 2.3.7","2.2 – 2.2.3","2.0 – 2.1","1.6","1.5"}; static Integer[] drawableArray = {R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1, R.drawable.img1,R.drawable.img1, R.drawable.img1}; static Integer[] id_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13}; }
output: