Android Studio Recyclerview:
The Recyclerview is an advanced version of listview, In the Recyclerview, the single layout has been set and the remaining data has passed through it. When an item scrolls off the screen, Recyclerview does not destroy its view. Rather, Recyclerview reuses the view for new particulars that have scrolled onscreen. This exercise extensively improves performance, perfecting your app’s responsiveness and reducing power consumption.
Classes in Recyclerview:
Several different classes of Recyclerview work together to make your dynamic list.
Let’s start with a Recyclerview
First, create a layout.xml by which you can set the layout of the recycler view, how you want to set image and text in the layout.
list_item_layout.xml in Recyclerview
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="match_parent" android:padding="8dp" android:orientation="horizontal"> <ImageView android:id="@+id/imgIcon" android:layout_width="fill_parent" android:layout_height="100dp" android:layout_marginHorizontal="@android:dimen/app_icon_size" android:layout_marginTop="-10dp" android:src="@drawable/java" /> <TextView android:id="@+id/textTitle" android:layout_width="20dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="-190dp" android:layout_marginRight="70dp" android:layout_marginTop="80dp" android:layout_marginBottom="40dp" android:layout_weight="1" android:text="@string/app_name" android:textColor="#000" android:textSize="20dp" /> </LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/programminglist" > </androidx.recyclerview.widget.RecyclerView>
MainActivity.java
package com.example.recyclerapp; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.Toast; import static android.widget.AdapterView.*; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); RecyclerView programminglist = findViewById( R.id.programminglist ); programminglist.setLayoutManager( new LinearLayoutManager( this ) ); final String[] languages = {"Java", "javascript", "C#", "php", "C", "C++", "Python"}; final int[] img = {R.drawable.java, R.drawable.javascript, R.drawable.csharp, R.drawable.php, R.drawable.c, R.drawable.cplus, R.drawable.python}; programminglist.setAdapter(new ProgrammingAdapte(this,languages,img)); } }
programmingadapter.java
Create an adapter that is used to hold the layout content right click on the java files and create a java class by the name of the programming adapter i.e..,
package com.example.recyclerapp; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import org.w3c.dom.Text; class ProgrammingAdapter extends RecyclerView.Adapter<ProgrammingAdapter.ProgrammingViewHolder>{ int [] image; String [] languages; public ProgrammingAdapter (Context context, String[] languages,int [] image) { this.image=image; this.languages=languages; } @NonNull @Override public ProgrammingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(R.layout.list_item_layout,parent,false); return new ProgrammingViewHolder(view); } @Override public void onBindViewHolder(@NonNull ProgrammingViewHolder holder, int position) { //String title = data[position]; holder.txtTitle.setText(languages[position]); holder.imgIcon.setImageResource(image[position]); } @Override public int getItemCount() { return image.length; } public class ProgrammingViewHolder extends RecyclerView.ViewHolder { ImageView imgIcon; TextView txtTitle; public ProgrammingViewHolder(@NonNull View itemView) { super(itemView); imgIcon = (ImageView)itemView.findViewById(R.id.imgIcon); txtTitle = (TextView)itemView.findViewById(R.id.textTitle); } } }