Android में RecyclerView: शुरुआती गाइड
RecyclerView, ListView का एक उन्नत संस्करण है और View Holder डिज़ाइन पैटर्न पर आधारित है। RecyclerView का उपयोग करके हम ग्रिड और साथ ही आइटम्स की सूची दिखा सकते हैं।
यह उदाहरण दर्शाता है कि RecyclerView को कैसे एकीकृत किया जाए, एक सुंदर छात्र रिकॉर्ड ऐप बनाकर जो छात्र का नाम और उम्र प्रदर्शित करता है।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2 - निर्भरताएं जोड़ें
build.gradle खोलें और RecyclerView लाइब्रेरी निर्भरता जोड़ें।
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.tutorialspoint"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
चरण 3 - activity_main.xml लेआउट
res/layout/activity_main.xml में निम्न कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
उपरोक्त कोड में, हमने RecyclerView को विंडो मैनेजर में RelativeLayout के रूप में पैरेंट लेआउट में जोड़ा है।
चरण 4 - MainActivity.java
src/MainActivity.java में निम्न कोड जोड़ें:
package com.example.andy.tutorialspoint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private StudentAdapter studentAdapter;
private List<StudentData> studentDataList = new ArrayList<>();
@TargetApi(Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
studentAdapter = new StudentAdapter(studentDataList);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(studentAdapter);
prepareStudentData();
}
private void prepareStudentData() {
StudentData data = new StudentData("साई", 25);
studentDataList.add(data);
data = new StudentData("साई", 25);
studentDataList.add(data);
data = new StudentData("रघु", 20);
studentDataList.add(data);
data = new StudentData("राज", 28);
studentDataList.add(data);
data = new StudentData("अमर", 15);
studentDataList.add(data);
data = new StudentData("बापू", 19);
studentDataList.add(data);
data = new StudentData("चंद्र", 52);
studentDataList.add(data);
data = new StudentData("देराज", 30);
studentDataList.add(data);
data = new StudentData("एशांत", 28);
studentDataList.add(data);
}
}
उपरोक्त कोड में, हमने एक RecyclerView और studentAdapter जोड़ा है। उस student adapter में, हमने studentDataList को ArrayList के रूप में पास किया है। Student data list में छात्र का नाम और उम्र होती है।
चरण 5 - StudentAdapter.java
src/StudentAdapter.java की सामग्री:
package com.example.andy.tutorialspoint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
List<StudentData> studentDataList;
public StudentAdapter(List<StudentData> studentDataList) {
this.studentDataList = studentDataList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.student_list_row, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int i) {
StudentData data = studentDataList.get(i);
viewHolder.name.setText(data.name);
viewHolder.age.setText(String.valueOf(data.age));
}
@Override
public int getItemCount() {
return studentDataList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name, age;
public MyViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
age = itemView.findViewById(R.id.age);
}
}
}
एडाप्टर क्लास में चार मुख्य विधियाँ हैं:
- onCreateViewHolder() - व्यू होल्डर बनाने और व्यू लौटाने के लिए उपयोग किया जाता है।
- onBindViewHolder() - बनाए गए व्यू होल्डर के साथ बाइंड करता है।
- getItemCount() - सूची का आकार रखता है।
- MyViewHolder क्लास - RecyclerView.ViewHolder द्वारा विस्तारित एक आंतरिक व्यू होल्डर क्लास।
चरण 6 - student_list_row.xml
res/layout/student_list_row.xml की संशोधित सामग्री:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:weightSum="1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textSize="15sp"
android:layout_height="100dp" />
<TextView
android:id="@+id/age"
android:layout_width="0dp"
android:layout_weight="0.5"
android:gravity="center"
android:textSize="15sp"
android:layout_height="100dp" />
</LinearLayout>
उपरोक्त लिस्ट आइटम व्यू में, हमने नाम और उम्र के लिए दो टेक्स्ट व्यू बनाए हैं।
चरण 7 - StudentData.java
src/StudentData.java की सामग्री:
package com.example.andy.tutorialspoint;
class StudentData {
String name;
int age;
public StudentData(String name, int age) {
this.name = name;
this.age = age;
}
}
उपरोक्त कोड StudentData ऑब्जेक्ट के बारे में बताता है। आइए अपने एप्लिकेशन को चलाने का प्रयास करें। मान लें कि आपने अपने वास्तविक Android मोबाइल डिवाइस को कंप्यूटर से जोड़ा है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। विकल्प के रूप में अपना मोबाइल डिवाइस चुनें और फिर अपने मोबाइल डिवाइस की जांच करें जो आपकी डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा।

अब नीचे स्क्रॉल करें। यह नीचे दिखाए अनुसार परिणाम दिखाता है:
