如何使用Kotlin的自定义适配器为列表视图setOnItemClickListener

时间:2017-09-30 08:33:11

标签: android listview kotlin

我在我的Android应用程序上使用自定义适配器进行列表视图。我不知道如何设置项目点击的监听器。我使用Kotlin作为我的应用程序。

这是我的适配器布局。



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/cbx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:id="@+id/txtNameNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_toRightOf="@+id/cbx"
        />
    <TextView
        android:id="@+id/txtPercent"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="TextView"
        android:layout_toRightOf="@+id/cbx"
        android:layout_below="@+id/txtNameNote"
        />
    </RelativeLayout>
</LinearLayout>
&#13;
&#13;
&#13; 这是我的适配器代码

&#13;
&#13;
class CustomAdapter(internal var context: Context, resource: Int, internal var listNote: ArrayList<Note>) : ArrayAdapter<Note>(context, resource, listNote) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        // TODO Auto-generated method stub
        val inflater = (context as Activity).layoutInflater
        convertView = inflater.inflate(R.layout.rowlistitem, parent, false)
        val name = convertView!!.findViewById<View>(R.id.txtNameNote) as TextView
        val percent = convertView.findViewById<View>(R.id.txtPercent) as TextView
        val cb = convertView.findViewById<View>(R.id.cbx) as CheckBox
        name.text = listNote[position].name
        percent.text = "Percent: " + listNote[position].percent + "%"

        if (listNote[position].status == NoteStatus.Active.value)
            cb.isChecked = false
         else{
            name.paintFlags = (name.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG)
            cb.isChecked = true
        }
       }
      }
        return convertView
    }
}
&#13;
&#13;
&#13;

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

Kotlin的正确方法是创建一个回调。然后在适配器中创建一个单击侦听器,并将结果转发给您的回调。

class CustomAdapter(
      internal var context: Context, 
      resource: Int, 
      internal var listNote: ArrayList<Note>,
      val callback: (Note) -> Unit) : ArrayAdapter<Note>(context, resource, listNote) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        // .. do your bindings whatever 
        convertView.yourRootLayoutElement.setOnClickListener{ 
           if (callback != null) callback(listNote[position])
        }
        return convertView
    }
}

在您创建适配器的视图中

CustomAdapter(...., { info { "Clicked $it" } })

我没有测试过,因为我没有使用ListView。我建议你使用RecyclerView。 ListView 已弃用,由于多种原因不推荐使用。阅读一些here的原因。

编辑:在RecyclerView上使用ListView绝对有 无理由 。即使它有效,也不应该使用它。了解RecyclerView,因为ListView应不再使用。

相关问题