我刚刚开始在Kotlin上使用Android工作室。 我使用自定义适配器实现了listView,但是当我点击此列表视图中的某个项目时,我不明白如何检测。
这是我的班级CalAdapter.kt
class CalAdapter(context: Context,al_session:ArrayList<activity_session>) : BaseAdapter(){
private val mInflator: LayoutInflater
private val al_session:ArrayList<activity_session>
init {
this.mInflator = LayoutInflater.from(context)
this.al_session=al_session
}
override fun getCount(): Int {
return al_session.size
}
override fun getItem(position: Int): Any {
return al_session.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
@SuppressLint("ResourceAsColor", "PrivateResource")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
val view: View?
val vh: ListRowHolder
if (convertView == null) {
view = this.mInflator.inflate(R.layout.calendar_adapter, parent, false)
vh = ListRowHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ListRowHolder
}
vh.label.text = al_session[position].id.toString()
vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
when (al_session[position].provided_sport)
{
"swimming" ->
{
//vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
vh.ivImage.setImageResource(R.drawable.ic_swiming)
}
"running" ->
{
//vh.ivImage.setBackgroundColor(R.color.white)
vh.ivImage.setImageResource(R.drawable.ic_running)
}
"cycling" ->
{
//vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
vh.ivImage.setImageResource(R.drawable.ic_bicycle)
}
}
return view
}
}
private class ListRowHolder(row: View?) {
val label: TextView = row?.findViewById(R.id.TWActivityID) as TextView
val ivImage: ImageButton = row?.findViewById(R.id.CalActivityButton) as ImageButton
}
这是我实现listview的功能。
lv是我的列表视图。
fun createActivities()
{
val lv = dynamicList
val test=ArrayList<activity_session>()
var i : Int = 0
while (i < 5) {
if (ActivityList.activityList[i] != null) {
test.add(ActivityList.activityList[i])
}
i += 1
}
val obj_adapter: CalAdapter
obj_adapter = CalAdapter(this.view!!.dynamicList.context, test)
lv.adapter=obj_adapter
}
我创建了一个代表我的listview的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<LinearLayout
android:id="@+id/AdapterLayout"
android:layout_width="215dp"
android:layout_height="140dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="@+id/CalActivityButton"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_info_black_24dp" />
<TextView
android:id="@+id/TWActivityID"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView"
android:visibility="invisible"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="230dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
当有人点击我刚刚创建的列表视图中的项目时,有人可以解释我如何检测吗?
编辑:嗨,我再次理解如何做到这一点,我在我的CalAdapter.kt类中的imagebutton中添加了一个setOnClickListener方法
现在看起来像是:https://imgur.com/a/Vw9SD
答案 0 :(得分:0)
在您的活动中,您应该向列表视图添加一个监听器。
list.setOnItemClickListener { parent, view, position, id ->
val myItem = parent.getItemAtPosition(position) as MyDataObj
}
您还应该像在自己的适配器上一样正确填充getItem()方法。
试试吧!
希望它有所帮助!