Â
Android RecyclerView with List Example –
First Create an Android Project with empty activity and open activiity_main.xml file in layout folder. After that add the RecyclerView widget.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""
  xmlns:app=""
  xmlns:tools=""
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Now we have to create a custom layout for listing items, So create list_layout.xml and add following code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android=""
  xmlns:app=""
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <androidx.cardview.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="15dp"
    android:padding="15dp"
    app:cardCornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    <androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <ImageView
        android:id="@+id/cardimage"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
      <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textColor="@color/black"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/cardimage"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
  </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
After creating list layout we need to create ListdataModel.java class. This class is used to sets the properties of the items.
package com.app.simplerecyclerview;
public class ListdataModel {
  private String Title;
  private int Image;
  public ListdataModel(String title, int image) {
    Title = title;
    Image = image;
 }
  public ListdataModel() {
 }
  public String getTitle() {
    return Title;
 }
  public void setTitle(String title) {
    Title = title;
 }
  public int getImage() {
    return Image;
 }
  public void setImage(int image) {
    Image = image;
 }
}
So now create ListAdapter.java class. This class extends RecyclerView.Adapter class and its methods like onCreateViewHolder() and onBindViewHolder(). onCreateViewHolder method inflates your list_layout.xml and the onBindViewHolder() method set each data items in list_layout.xml, also if you want to add a click event on any item then also you can do that in this method.
package com.app.simplerecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
​
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
  private ListdataModel[] listdataModels;
  public ListAdapter(ListdataModel[] listdataModels){
    this.listdataModels=listdataModels;
 }
  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater= LayoutInflater.from(parent.getContext());
    // inflate layout of list here
    View listitem = layoutInflater.inflate(R.layout.list_layout,parent,false);
    ViewHolder viewHolder= new ViewHolder(listitem);
    return viewHolder;
 }
  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final ListdataModel listdata = listdataModels[position];
    holder.textView.setText(listdataModels[position].getTitle());
    holder.imageView.setImageResource(listdataModels[position].getImage());
    // onclick click toast display
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(v.getContext(), listdata.getTitle(), Toast.LENGTH_SHORT).show();
     }
   });
 }
  @Override
  public int getItemCount() {
    return listdataModels.length;
 }
  public static class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public TextView textView;
    public ViewHolder(View itemView) {
      super(itemView);
      this.imageView = (ImageView) itemView.findViewById(R.id.cardimage);
      this.textView = (TextView) itemView.findViewById(R.id.textView);
   }
 }
}
Now it’s time to write some code in MainActivty.java class. In this class, we create an array of items for Listdatamodel class and set the adapter class to RecyclerView.
package com.app.simplerecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
​
public class MainActivity extends AppCompatActivity {
  RecyclerView recyclerView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Transparent Status bar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      Window w = getWindow();
      w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
   }
    // add data
    ListdataModel[] listdataModels= new ListdataModel[]{
        new ListdataModel("john",R.drawable.john),
        new ListdataModel("leo",R.drawable.leo),
        new ListdataModel("jeff",R.drawable.jeff),
        new ListdataModel("kober",R.drawable.kober),
        new ListdataModel("kelvin",R.drawable.kelvin),
        new ListdataModel("jems",R.drawable.jems),
   };
    recyclerView= findViewById(R.id.rv1);
    ListAdapter adapter = new ListAdapter(listdataModels);
    recyclerView.setHasFixedSize(true);
    // set recyclerview layout as linearlist and gird
    // for linear layout
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    //for grid layout
    //recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    recyclerView.setAdapter(adapter);
 }
}
0 Comments