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

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

ওভারভিউ

এই টিউটোরিয়ালটি দেখাবে কিভাবে Kotlin ব্যবহার করে Android RecyclerView-এ নির্বাচিত আইটেমগুলোকে সঠিকভাবে হাইলাইট করা যায়। আমরা একটি কাস্টম সিলেকশন মেকানিজম বাস্তবায়ন করব যা ব্যবহারকারীর ক্লিক অনুযায়ী আইটেমের পটভূমি রঙ পরিবর্তন করবে।

প্রয়োজনীয় ধাপসমূহ

ধাপ ১: নতুন প্রজেক্ট তৈরি

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

ধাপ ২: 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="4dp"
    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="8dp" />
</RelativeLayout>

ধাপ ৩: MainActivity.kt ফাইল বাস্তবায়ন

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "RecyclerView Selection Demo"
        
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.addItemDecoration(SimpleItemDecoration(this))
        recyclerView.layoutManager = LinearLayoutManager(this)
        
        val posts = returnListItems()
        val adapter = RecyclerViewAdapter(this, posts)
        recyclerView.adapter = adapter
    }
    
    private fun returnListItems(): List<ItemObject> {
        return listOf(
            ItemObject("Ballon d'or", "2007", "Ricardo KaKa"),
            ItemObject("Ballon d'or", "2008", "Cristiano Ronaldo"),
            ItemObject("Ballon d'or", "2009 - 2012, 2015", "Lionel Messi"),
            ItemObject("Ballon d'or", "2013, 2014, 2016, 2017", "Cristiano Ronaldo"),
            ItemObject("Ballon d'or", "2018", "Luka Modric"),
            ItemObject("Ballon d'or", "2019", "Lionel Messi")
        )
    }
}

ধাপ ৪: প্রয়োজনীয় Kotlin ক্লাস ফাইল তৈরি

ItemObject.kt

data class ItemObject(
    val awardTitle: String,
    val awardYear: String,
    val player: String
)

RecyclerViewAdapter.kt

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewAdapter(
    private val context: MainActivity,
    private val itemList: List<ItemObject>
) : RecyclerView.Adapter<RecyclerViewHolders>() {
    
    @NonNull
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolders {
        val layoutView = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
        return RecyclerViewHolders(layoutView)
    }
    
    override fun onBindViewHolder(holder: RecyclerViewHolders, position: Int) {
        val item = itemList[position]
        holder.awardTitle.text = "Award Title: ${item.awardTitle}"
        holder.awardYear.text = "Award Year: ${item.awardYear}"
        holder.player.text = "Player Name: ${item.player}"
    }
    
    override fun getItemCount() = itemList.size
}

RecyclerViewHolders.kt

import android.util.SparseBooleanArray
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class RecyclerViewHolders(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
    var awardTitle: TextView = itemView.findViewById(R.id.awardTitle)
    var awardYear: TextView = itemView.findViewById(R.id.awardYear)
    var player: TextView = itemView.findViewById(R.id.playerName)
    
    private val selectedItems = SparseBooleanArray()
    
    init {
        itemView.setOnClickListener(this)
    }
    
    override fun onClick(view: View) {
        val position = adapterPosition
        if (selectedItems.get(position, false)) {
            selectedItems.delete(position)
            view.isSelected = false
        } else {
            selectedItems.put(position, true)
            view.isSelected = true
        }
    }
}

SimpleItemDecoration.kt

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import androidx.annotation.NonNull
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

class SimpleItemDecoration(context: Context) : RecyclerView.ItemDecoration() {
    private val drawable: Drawable = ContextCompat.getDrawable(context, R.drawable.line_divider)!!
    
    override fun onDrawOver(
        @NonNull canvas: Canvas,
        parent: RecyclerView,
        @NonNull state: RecyclerView.State
    ) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + drawable.intrinsicHeight
            drawable.setBounds(left, top, right, bottom)
            drawable.draw(canvas)
        }
    }
}

ধাপ ৫: ড্রয়েবল রিসোর্স ফাইল তৈরি

background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/holo_green_light" 
          android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@android:color/holo_purple" 
          android:state_selected="false" />
</selector>

line_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="2dp" android:height="2dp" />
    <solid android:color="@color/colorPrimaryDark" />
</shape>

ধাপ ৬: list_layout.xml লেআউট ফাইল

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_selector"
    android:padding="16dp">
    <TextView
        android:id="@+id/awardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="New Text"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/awardYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/awardTitle"
        android:layout_alignStart="@id/awardTitle"
        android:layout_marginTop="20dp"
        android:text="New Text"
        android:textColor="@android:color/background_dark" />
    <TextView
        android:id="@+id/playerName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="10dp"
        android:text="New Text"
        android:textColor="@android:color/background_dark" />
</RelativeLayout>

ধাপ ৭: AndroidManifest.xml কনফিগারেশন

সাধারণ ম্যানিফেস্ট কনফিগারেশন ব্যবহার করুন। ইন্টারনেট পারমিশনের প্রয়োজন নেই।

  1. অ্যান্ড্রয়েড RecyclerView-এ সিলেক্টেড আইটেম সঠিকভাবে হাইলাইট করবেন কীভাবে?

  2. Kotlin ব্যবহার করে Android-এ স্ক্রলযোগ্য TextView তৈরি করা

  3. Kotlin ব্যবহার করে Android-এ JSON অবজেক্ট পার্স করা

  4. অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে GridView স্ক্রোলিং নিষ্ক্রিয় করার উপায়