In Android, Recyclerview is an advanced and flexible version of ListView and GridView. It is a container used for displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views. Recyclerview was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop).
Implementation: To implement a basic RecyclerView three sub-parts are needed to be constructed which offer the users the degree of control they require in making varying designs of their choice.
- The Card Layout: The card layout is an XML layout which will be treated as an item for the list created by the RecyclerView.
- The Adaptor(ViewHolder): The ViewHolder is a java class that stores the reference to the card layout views that have to be dynamically modified during the execution of the program by a list of data obtained either by online databases or added in some other way.
- The Model Class: The Data class is a custom java class that acts as a structure for holding the information for every item of the RecyclerView.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio click File->New->New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add
the below code to that file. Below is the code for the
activity_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 3: Now create to Model class
Go to the app name then Right click->New->java Class->Model file.
Below is the code for the Model.java file. Comments are added inside the code to understand the code in more detail.
package com.example.recyclerview;
public class Model {
String name;
int image;
public Model(String name, int image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
Step 4: Now create to the Adaptor class
Go to the app name then Right click->New->java Class->Adaptor file.
Below is the code for the Model.java file. Comments are added inside the code to understand the code in more detail.
package com.example.recyclerview;
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 java.util.ArrayList;
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
ArrayList<Model> list;
Context context;
public Adaptor(ArrayList<Model> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_file,parent,false);
return new Adaptor.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Model model = list.get(position);
//add variables
holder.name.setText(model.getName());
holder.imageView.setImageResource(model.getImage());
}
@Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
imageView = itemView.findViewById(R.id.image);
}
}
}
Step 5: Working with the MainActivity.java file
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//create a variables..
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hook's
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
ArrayList<Model> list = new ArrayList<>();
list.add(new Model("Mamta", R.drawable.pic1));
list.add(new Model("Sunita", R.drawable.pic2));
list.add(new Model("Sweety", R.drawable.pic3));
list.add(new Model("Neeta", R.drawable.pic4));
list.add(new Model("Meenal", R.drawable.pic5));
list.add(new Model("Poja", R.drawable.pic6));
list.add(new Model("Neelam", R.drawable.pic7));
list.add(new Model("Kamla", R.drawable.pic8));
list.add(new Model("Baby", R.drawable.pic9));
list.add(new Model("Babu", R.drawable.pic10));
list.add(new Model("Khusbu", R.drawable.pic11));
list.add(new Model("Kawita", R.drawable.pic12));
Adaptor adaptor = new Adaptor(list,this);
recyclerView.setAdapter(adaptor);
//now we set different different layout i will give some example
// LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
//Stagle layout
// StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL);
//Grid Layout
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(gridLayoutManager);
//thank's in the next tutorials we learn recyclerview in the API real data......thanks....
}
}
0 Comments