kotlin android中的setOnLongClickListener

时间:2017-08-20 14:54:31

标签: android listview kotlin listviewitem onlongclicklistener

如何在setOnClickListener的每个项目中使用ListView

我的xml:

<ListView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ListView>

2 个答案:

答案 0 :(得分:1)

在您的kotlin活动中:

override fun onCreate(savedInstanceState: Bundle?) {
  val listView: ListView = findViewById(R.id.yourListViewId)
  listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, i, l -> 
    //YOUR CODE HERE
  }
}

答案 1 :(得分:0)

没有什么晚参加聚会的。我们之所以发布此答案,是因为我们在RecyclerAdapter中设置OnLongClickListener感到费解,为什么原因是当您输入代码时,如果在打开语句和返回之间添加代码行之前未包含RETURN值,则编译器会抱怨,并且会认为它们只是错误,这里有一点代码希望它能帮助对OnLongClickListener陌生的任何人

class PersonRecyclerAdapter(contactList: List<Contact>, internal var context: Context) : RecyclerView.Adapter<PersonRecyclerAdapter.ViewHolder>() {

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

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

override fun getItemCount(): Int {
    return contactList.size
}

override fun onBindViewHolder(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)
    }

}

inner class ViewHolder(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 with

    fun bindCheckBox(){// 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"))
        }
    }
}

}

请注意在RecyclerAdapter中包含侦听器的不同方法