RecyclerView দিয়ে হরিজন্টাল লিস্ট তৈরি: আধুনিক পদ্ধতি
পুরানো HorizontalListView লাইব্রেরির পরিবর্তে আধুনিক অ্যান্ড্রয়েডে RecyclerView-কে Horizontal LayoutManager দিয়ে হরিজন্টাল লিস্ট তৈরি করা সেরা পদ্ধতি।
পদক্ষেপ ১: প্রজেক্ট তৈরি ও ডিপেন্ডেন্সি যোগ
build.gradle (Module: app) ফাইলে:
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'androidx.cardview:cardview:1.0.0'
}
পদক্ষেপ ২: লেআউট XML (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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
পদক্ষেপ ৩: আইটেম লেআউট (item_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:card_view="https://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="9dp"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="0.01dp">
<RelativeLayout
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="160dp">
<ImageView
android:id="@+id/imgThumbnail"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_above="@+id/textView"
android:layout_centerHorizontal="true"
android:scaleType="fitXY" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#FF444444"
android:gravity="center_vertical"
android:paddingStart="5dp"
android:paddingEnd="2dp"
android:text="Test"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
পদক্ষেপ ৪: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList numberName;
ArrayList numberImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ডেটা প্রস্তুতি
numberName = new ArrayList<>(Arrays.asList("Four", "Nine", "Seven", "Six", "Ten", "Three", "Two"));
numberImage = new ArrayList<>(Arrays.asList(
R.drawable.four, R.drawable.nine, R.drawable.seven,
R.drawable.six, R.drawable.ten, R.drawable.three, R.drawable.two
));
// RecyclerView সেটআপ
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
// হরিজন্টাল লেআউট ম্যানেজার - মূল কী
LinearLayoutManager layoutManager = new LinearLayoutManager(
this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
MyAdapter adapter = new MyAdapter(this, numberName, numberImage);
recyclerView.setAdapter(adapter);
}
}
পদক্ষেপ ৫: MyAdapter.java
import android.content.Context; import android.content.Intent; 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.recyclerview.widget.RecyclerView; import java.util.ArrayList; class MyAdapter extends RecyclerView.Adapter{ private ArrayList numberName; private ArrayList numberImage; private Context context; MyAdapter(Context context, ArrayList numberName, ArrayList numberImage) { this.context = context; this.numberName = numberName; this.numberImage = numberImage; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.textView.setText(numberName.get(position)); holder.imgThumbnail.setImageResource(numberImage.get(position)); holder.itemView.setOnClickListener(v -> Toast.makeText(context, "#" + position + " - " + numberName.get(position), Toast.LENGTH_SHORT).show() ); holder.itemView.setOnLongClickListener(v -> { Toast.makeText(context, "#" + position + " - " + numberName.get(position) + " (Long click)", Toast.LENGTH_SHORT).show(); return true; }); } @Override public int getItemCount() { return numberName.size(); } static class ViewHolder extends RecyclerView.ViewHolder { ImageView imgThumbnail; TextView textView; ViewHolder(View itemView) { super(itemView); imgThumbnail = itemView.findViewById(R.id.imgThumbnail); textView = itemView.findViewById(R.id.textView); } } }
গুরুত্বপূর্ণ উন্নতি
- Interface আলাদা ক্লাসে না করে ল্যাম্বডা ব্যবহার করা হয়েছে
- ViewHolder প্যাটার্ন সঠিকভাবে ইমপ্লিমেন্ট করা হয়েছে
- LayoutInflater-এ parent.getContext() ব্যবহার করা হয়েছে
- ড্রয়েবল রিসোর্স (R.drawable.*) অবশ্যই প্রজেক্টে থাকতে হবে