RecyclerView: আধুনিক লিস্ট/গ্রিড ভিউ ইমপ্লিমেন্টেশন
RecyclerView হলো ListView-এর উন্নত সংস্করণ যা ভিউ রিসাইক্লিং, লেআউট ম্যানেজার, আইটেম অ্যানিমেশন এবং ডেকোরেশন সাপোর্ট করে।
প্রজেক্ট সেটআপ
dependencies-এ নিশ্চিত করুন:
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'androidx.cardview:cardview:1.0.0'
}
ধাপ ১: লেআউট (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ধাপ ২: মডেল ক্লাস (MovieModel.java)
package com.example.recyclerviewdemo;
public class MovieModel {
private String title, genre, year;
public MovieModel() {}
public MovieModel(String title, String genre, String year) {
this.title = title;
this.genre = genre;
this.year = year;
}
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getGenre() { return genre; }
public void setGenre(String genre) { this.genre = genre; }
public String getYear() { return year; }
public void setYear(String year) { this.year = year; }
}
ধাপ ৩: আইটেম লেআউট (res/layout/movie_list.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/year"
android:textColor="#222222"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textColor="#999999" />
<TextView
android:id="@+id/genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
ধাপ ৪: Adapter (MoviesAdapter.java)
package com.example.recyclerviewdemo; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class MoviesAdapter extends RecyclerView.Adapter{ private List moviesList; static class MyViewHolder extends RecyclerView.ViewHolder { TextView title, year, genre; MyViewHolder(View view) { super(view); title = view.findViewById(R.id.title); genre = view.findViewById(R.id.genre); year = view.findViewById(R.id.year); } } public MoviesAdapter(List moviesList) { this.moviesList = moviesList; } @NonNull @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.movie_list, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { MovieModel movie = moviesList.get(position); holder.title.setText(movie.getTitle()); holder.genre.setText(movie.getGenre()); holder.year.setText(movie.getYear()); } @Override public int getItemCount() { return moviesList.size(); } }
ধাপ ৫: MainActivity.java
package com.example.recyclerviewdemo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List movieList = new ArrayList<>();
private MoviesAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
mAdapter = new MoviesAdapter(movieList);
// LinearLayoutManager (ভার্টিক্যাল লিস্ট)
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
// অ্যানিমেশন
recyclerView.setItemAnimator(new DefaultItemAnimator());
// অ্যাডাপ্টার সেট
recyclerView.setAdapter(mAdapter);
// ডেটাเตম্প
prepareMovieData();
}
private void prepareMovieData() {
String[][] movies = {
{"Mad Max: Fury Road", "Action & Adventure", "2015"},
{"Inside Out", "Animation, Kids & Family", "2015"},
{"Star Wars: Episode VII", "Action", "2015"},
{"The Martian", "Science Fiction & Fantasy", "2015"},
{"Mission: Impossible Rogue Nation", "Action", "2015"},
// আরও ডেটা যোগ করুন...
};
for (String[] m : movies) {
movieList.add(new MovieModel(m[0], m[1], m[2]));
}
mAdapter.notifyDataSetChanged();
}
}
LayoutManager ধরন
| Manager | ব্যবহার |
|---|---|
| LinearLayoutManager | ভার্টিক্যাল/হরিজোন্টাল লিস্ট |
| GridLayoutManager | গ্রিড (গ্যালারি, ফটো অ্যাপ) |
| StaggeredGridLayoutManager | মেসনারি/পিন্টারেস্ট স্টাইল |