কম্পিউটার টিউটোরিয়াল

কোটলিন ব্যবহার করে অ্যান্ড্রয়েড অ্যাপে RecyclerView নিয়ে কাজ করার টিউটোরিয়াল

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েড অ্যাপে কোটলিন ব্যবহার করে RecyclerView নিয়ে কাজ করা যায়।

পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।

পদক্ষেপ ২: res/layout/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>

পদক্ষেপ ৩: build.gradle-এ ডিপেন্ডেন্সি যোগ করুন

dependencies {
    implementation "androidx.recyclerview:recyclerview:1.3.2"
    implementation "androidx.cardview:cardview:1.0.0"
}

পদক্ষেপ ৪: src/MainActivity.kt-এ কোড যোগ করুন

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    private val movieList = mutableListOf<MovieModel>()
    private lateinit var moviesAdapter: MoviesAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        moviesAdapter = MoviesAdapter(movieList)
        
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.adapter = moviesAdapter
        
        prepareMovieData()
    }

    private fun prepareMovieData() {
        val movies = listOf(
            MovieModel("Mad Max: Fury Road", "Action & Adventure", "2015"),
            MovieModel("Inside Out", "Animation, Kids & Family", "2015"),
            MovieModel("Star Wars: Episode VII - The Force Awakens", "Action", "2015"),
            MovieModel("Shaun the Sheep", "Animation", "2015"),
            MovieModel("The Martian", "Science Fiction & Fantasy", "2015"),
            MovieModel("Mission: Impossible Rogue Nation", "Action", "2015"),
            MovieModel("Up", "Animation", "2009"),
            MovieModel("Star Trek", "Science Fiction", "2009"),
            MovieModel("The LEGO Movie", "Animation", "2014"),
            MovieModel("Iron Man", "Action & Adventure", "2008"),
            MovieModel("Aliens", "Science Fiction", "1986"),
            MovieModel("Chicken Run", "Animation", "2000"),
            MovieModel("Back to the Future", "Science Fiction", "1985"),
            MovieModel("Raiders of the Lost Ark", "Action & Adventure", "1981"),
            MovieModel("Goldfinger", "Action & Adventure", "1965"),
            MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
        )
        movieList.addAll(movies)
        moviesAdapter.notifyDataSetChanged()
    }
}

পদক্ষেপ ৫: MovieModel.kt ডেটা ক্লাস তৈরি করুন

data class MovieModel(
    val title: String,
    val genre: String,
    val year: String
)

পদক্ষেপ ৬: MoviesAdapter.kt অ্যাডাপ্টার ক্লাস তৈরি করুন

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

class MoviesAdapter(private var moviesList: List<MovieModel>) : 
    RecyclerView.Adapter<MoviesAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val title: TextView = view.findViewById(R.id.title)
        val year: TextView = view.findViewById(R.id.year)
        val genre: TextView = view.findViewById(R.id.genre)
    }

    @NonNull
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_row, parent, false)
        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val movie = moviesList[position]
        holder.title.text = movie.title
        holder.genre.text = movie.genre
        holder.year.text = movie.year
    }

    override fun getItemCount(): Int = moviesList.size
}

পদক্ষেপ ৭: res/layout/item_row.xml লেআউট তৈরি করুন

<?xml version="1.0" encoding="utf-8"?> 
<androidx.cardview.widget.CardView xmlns:android="https://schemas.android.com/apk/res/android" 
    xmlns:app="https://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    app:cardBackgroundColor="@android:color/holo_red_dark"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp"> 
    <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:padding="16dp"> 
        <TextView 
            android:id="@+id/title" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_toStartOf="@+id/year" 
            android:textColor="@android:color/white" 
            android:textSize="18sp" 
            android:textStyle="bold" /> 
        <TextView 
            android:id="@+id/year" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentEnd="true" 
            android:textColor="@android:color/white" 
            android:textSize="14sp" /> 
        <TextView 
            android:id="@+id/genre" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:layout_below="@+id/title" 
            android:layout_marginTop="4dp"
            android:textColor="@android:color/white" 
            android:textSize="14sp" /> 
    </RelativeLayout> 
</androidx.cardview.widget.CardView>

পদক্ষেপ ৮: androidManifest.xml-এ কোড যোগ করুন

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.q11"> 
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme"> 
        <activity android:name=".MainActivity"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
</manifest>
আধুনিক উন্নতি: ListAdapter + DiffUtil ব্যবহার করে পারফরম্যান্স বাড়ানো যায়। ViewBinding ব্যবহার করুন findViewById-এর পরিবর্তে।

অ্যাপ্লিকেশন চালান

আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. RecyclerView ও GridLayoutManager ব্যবহার করে সিম্পল অ্যান্ড্রয়েড গ্রিড উদাহরণ

  2. Kotlin-এ Android-এ RecyclerView এবং ডেটাবেস (SQLite) একসাথে ব্যবহার করার পদ্ধতি

  3. Kotlin ব্যবহার করে Android RecyclerView-এ নির্বাচিত আইটেমগুলোকে সঠিকভাবে হাইলাইট করার পদ্ধতি

  4. Kotlin ব্যবহার করে Android-এ ফ্লিং জেসচার ডিটেকশন বাস্তবায়ন