【发布时间】:2021-12-10 14:20:21
【问题描述】:
我的项目中有底部表,我想将它用于 RecylerView 项目,我尝试在适配器内部使用下面的代码,不知道在哪里使用它,因为适配器中没有 onCreate 函数,有什么想法吗?
imageId.setOnClickListener{
val accountFragment = AccountFragment()
accountFragment.show(supportFragmentManager,"Tag")
}
适配器:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowsHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.row_item, parent, false)
imageId.setOnClickListener{
val accountFragment = AccountFragment()
accountFragment.show(supportFragmentManager,"Tag")
}
return RowsHolder(view)
}
row_item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imageId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="Title"
android:textColor="@android:color/black"
android:textFontWeight="700"
android:textSize="16sp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
这是 ViewHolders 中的一个按钮吗?如果是这样,您可以在创建视图之后并在
onCreateViewHolder()中返回之前设置它。如果按钮必须根据列表中的哪个项目执行不同的操作,那么您应该在onBindViewHolder()中执行。 -
不,它不仅仅是按钮,我想长按列表行并显示底部表格
-
也许您可以展示您的整个适配器,以便我们更好地了解您要做什么。
-
@Tenfour04,我还没有把它放在适配器里面,因为我仍然不知道如何在适配器里面实现它
-
@Tenfour,好的,让我更新问题
标签: android kotlin android-adapter