Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to change the color of the text and background of row clicked of my recyclerview in my recyclerview.

But my problem is after clicking for example on the 2th item,the 10st item also gets selected.Likewise after clicking my 5th item the 3nd item is selected.

How do i solve this?
in fact my question is that how to change background color of recyclerview item that click on it in Kotlin?
I also followed the instructions in this link. But it did not worked correctly!!

AllChanelAdapter.kt
Kotlin
class AllChanelAdapter(private val datalist:MutableList<AllChanelModel>, var clickListener: OnItemClickListener):RecyclerView.Adapter<AllChanelHolder>() {
    private lateinit var context:Context
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AllChanelHolder {
        context = parent.context
        return AllChanelHolder(LayoutInflater.from(context).inflate(R.layout.allchanel_singleitem,parent,false))
    }
    override fun getItemCount(): Int = datalist.size

    override fun onBindViewHolder(holder: AllChanelHolder, position: Int) {
        val data = datalist[position]
        val txt_title = holder.itemView.txt_title
        val txt_body = holder.itemView.txt_body
        val img_chanel = holder.itemView.img_chanel

        txt_title.setText(data.title)
        txt_body.setText(data.body)

        Glide
            .with(context)
            .load("...")
            .centerCrop()
            .into(img_chanel);
    }
    holder.initialize(datalist.get(position),clickListener)
}
interface OnItemClickListener {
    fun onItemClick(item: AllChanelModel, position: Int, view: View)
}


AllChanelHolder.kt

Kotlin
class AllChanelHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun initialize(item:AllChanelModel,action:OnItemClickListener){
        itemView.setOnClickListener {
            action.onItemClick(item,adapterPosition,itemView)
        }
    }
}


MainPageActivity.kt
Kotlin
class MainPageActivity : AppCompatActivity(),OnItemClickListener {

    private val datalist:MutableList<AllChanelModel> = mutableListOf()
    lateinit var allchaneladapter : AllChanelAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_page)
        
        send_request()

        allchaneladapter = AllChanelAdapter(datalist,this)
        all_chanel_recycler.layoutManager = LinearLayoutManager(this)
        all_chanel_recycler.adapter = allchaneladapter
    }

    private fun send_request(){
        val url = "http://10.0.2.2:8000/getsamplejson" // localhost api
        val que = Volley.newRequestQueue(this@MainPageActivity)
        val req = JsonArrayRequest(Request.Method.GET,url,null,
            Response.Listener {
                response->
                for(i in 0..response.length()-1){
                    var chanel_obj = response.getJSONObject(i)
                    datalist.add(
                        AllChanelModel(
                            chanel_obj.getString("body"),
                            chanel_obj.getString("title"),
                            chanel_obj.getString("userId")
                        )
                    )
                }
                allchaneladapter.notifyDataSetChanged()
            }, Response.ErrorListener {
                error->
                   Log.e("",error.message)
            })

            que.add(req)
    }
    override fun onItemClick(item: AllChanelModel, position: Int, view: View) {
        view.setBackgroundColor(Color.YELLOW)
    }
}


AllChanelModel.kt
Kotlin
data class AllChanelModel(
    @SerializedName("body")
    val body: String,
    @SerializedName("title")
    val title: String,
    @SerializedName("userId")
    val userId: String
    )


activity_main_page.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainPageActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/all_chanel_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


allchanel_singleitem.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/linear_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/txt_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="TextView"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/txt_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|right"
            android:text="TextView"
            android:textColor="#000000" />

    </LinearLayout>

    <ImageView
        android:id="@+id/img_chanel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        app:srcCompat="@mipmap/ic_launcher" />

</LinearLayout>


please help me
thank you

What I have tried:

I have explained in the problem section
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900