【问题标题】:setOnLongClickListener in kotlin androidkotlin android中的setOnLongClickListener
【发布时间】:2018-01-28 17:20:13
【问题描述】:

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

我的 xml:

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

</ListView>

【问题讨论】:

    标签: android listview kotlin listviewitem onlongclicklistener


    【解决方案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
      }
    }
    

    【讨论】:

    • 他要了每一件物品
    • 我想要每个项目
    • onItemClickListener 是针对每个项目的。方法中的变量“i”是每个项目的位置。您只需使用 yourItemList.get(i) 来检索要使用的项目。这是推荐的方法。您也可以在适配器中使用 OnClickListener,但这会增加内存使用量。
    • 当我在适配器中使用此方法时,它不起作用。如果它在适配器中不起作用,我可以在哪里使用它??
    • 您必须在引用列表视图的活动中使用它。简而言之,您正在使用ListView listView=(ListView)findviewById(R.id.tv1)
    【解决方案2】:

    晚会比什么都好。我们发布此答案是因为我们在 RecyclerAdapter 中设置 OnLongClickListener 时遇到了困难,因为当您输入代码时,如果在开始语句和 return 之间添加代码行之前不包含 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 中包含侦听器的不同方法

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 2015-05-16
      • 2017-12-16
      相关资源
      最近更新 更多