Use of Recycler View in Android

Use of Recycler View in Android

Subscribe to my newsletter and never miss my upcoming articles

Step 1: Make an android project with empty Activity Step 2: Put the recyler view in the project. Step 3: Go to Layouts>make new layout as "sample_for_recylerview". Step 4: Completely design the sample in this layout. Step 5: Goto Java fils make two new packages one for "Models" and second for "Adapters". Step 6: Create a Java class for the model in model package.

Here is the Example of a model class

package com.example.myfoodapp.mymodels;

public class MainModel {
    int image;
    String priduct_Name;
    String price;
    String discription;

    public MainModel(int image, String priduct_Name, String price, String discription) {
        this.image = image;
        this.priduct_Name = priduct_Name;
        this.price = price;
        this.discription = discription;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getPriduct_Name() {
        return priduct_Name;
    }

    public void setPriduct_Name(String priduct_Name) {
        this.priduct_Name = priduct_Name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getDiscription() {
        return discription;
    }

    public void setDiscription(String discription) {
        this.discription = discription;
    }

}

Step 7: In the adapter package create an adapter class and a ViewHolder class in the adapter class which will be extended by the RecylerView.ViewHolder and use it to bind the front and views in the backend java.

Here is the example code of the Adapter class

package com.example.myfoodapp.myadapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myfoodapp.R;
import com.example.myfoodapp.mymodels.MainModel;

import java.util.ArrayList;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    ArrayList<MainModel> myList;
    Context context;

    public MainAdapter(ArrayList<MainModel> myList, Context context) {
        this.myList = myList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.sample_mainfoodpage,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            MainModel model = myList.get(position);
            holder.myImage.setImageResource(model.getImage());
            holder.myName.setText(model.getPriduct_Name());
            holder.myPrice.setText(model.getPrice());
            holder.MyDes.setText(model.getDiscription());
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    //View holding class. . .
    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView myImage;
        TextView myName, myPrice, MyDes;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            myImage = itemView.findViewById(R.id.my_imageview);
            myName = itemView.findViewById(R.id.lbl_itemname);
            myPrice = itemView.findViewById(R.id.lbl_price);
            MyDes = itemView.findViewById(R.id.lbl_discription);

        }
    }
}

Step 8: After making the adapter class lets go towards the mainactivity java file and program in the file like as shown in the example below:

Here is the MainActivity.Java

package com.example.myfoodapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.LinearLayout;

import com.example.myfoodapp.myadapters.MainAdapter;
import com.example.myfoodapp.mymodels.MainModel;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.myRecylerview);

        ArrayList<MainModel> list = new ArrayList<>();
        list.add(new MainModel(R.drawable.food1,"Burger","$2.5","Zinger burger with exta toping of chese"));
        list.add(new MainModel(R.drawable.food2,"Pizz","$3.5","Pizza with exta toping of chese"));

        MainAdapter adapter = new MainAdapter(list, this);
        recyclerView.setAdapter(adapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
    }
}
 
Share this