Skip to content Skip to sidebar Skip to footer

Setonlongclicklistener In Kotlin Android

How can I use setOnClickListener in each item in my ListView? my xml : Copy

Solution 2:

Nothing like getting to the party late. We are posting this answer because we struggled with setting a OnLongClickListener in our RecyclerAdapter Why because as you enter the code if the RETURN value is not included before adding the lines of code between the opening statement and the return the compiler complains and one would think they are just wrong here is a little code hope it helps anyone new to OnLongClickListener

classPersonRecyclerAdapter(contactList: List<Contact>, internalvar context: Context) : RecyclerView.Adapter<PersonRecyclerAdapter.ViewHolder>() {

privatevar contactList: List<Contact> = ArrayList()
init { this.contactList = contactList }

overridefunonCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(context).inflate(R.layout.new_single_card,parent,false)
    return ViewHolder(view)
}

overridefungetItemCount(): Int {
    return contactList.size
}

overridefunonBindViewHolder(holder: ViewHolder, position: Int) {
    val items = contactList[position]
    holder.item.text = items.name

    holder.bindCheckBox()
    // This code calls the function below in the inner class// too much code manipulation

    holder.list_new_card.setOnLongClickListener { view ->
        holder.ckBox.isChecked = false
        holder.ckBox.isEnabled = true
        holder.item.text = items.name
        true

    }
    /*holder.list_new_card.setOnClickListener {

        holder.ckBox.isChecked = false
        holder.ckBox.isEnabled = true
        holder.item.text = items.name

        //val i = Intent(context, MainActivity::class.java)
        //i.putExtra("Mode", "E")
        //i.putExtra("Id", items.id)
        //i.putExtra("ET",items.name)
        //i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        //context.startActivity(i)
        // This code attaches a listener to the tvName in the new_single_card.xml
    }*/

    holder.editCLICK.setOnClickListener {
        val i = Intent(context, MainActivity::class.java)
        i.putExtra("FROM", "U")
        i.putExtra("MainActId",items.id)
        i.putExtra("ET",items.name)
        i.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(i)
    }

}

innerclassViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    var item: TextView = view.findViewById(R.id.tvName) as TextView
    var list_new_card: CardView = view.findViewById(R.id.list_new_card) as CardView
    var editCLICK: RelativeLayout = view.findViewById(R.id.editCLICK) as RelativeLayout
    var ckBox:CheckBox = view.findViewById(R.id.ckBox)as CheckBox
    // This is how you declare a instance of the Widiget you want to work withfunbindCheckBox(){// Create function and BIND it in the onBindViewHolder function

        ckBox.setOnCheckedChangeListener { view,isChecked ->
            if(ckBox.isChecked){
                item.visibility = View.VISIBLE
                item.setTextColor(Color.parseColor("#FF0000"))
                item.text = "Click & HOLD Me to View Item"
                ckBox.isEnabled = false

            }else
            item.setTextColor(Color.parseColor("#000000"))
        }
    }
}

}

Note different ways to include listeners in RecyclerAdapter

Post a Comment for "Setonlongclicklistener In Kotlin Android"