【问题标题】:How we can use the setOnClickListener inside of the adapter?我们如何在适配器内部使用 setOnClickListener?
【发布时间】: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


【解决方案1】:

也许你可以使用这个方法。因为您没有编写整个适配器,所以我发布了一个示例,所以也许它会对您有所帮助。(我注释了额外的代码)

class MyAdapter(val context: Context, val categories: List<Category>, val itemCliked:(Category) -> Unit) :
RecyclerView.Adapter<MyAdapter.RowHolder>() {
inner class RowHolder(itemView: View,val itemCliked: (Category) -> Unit) : RecyclerView.ViewHolder(itemView) {
     // val categoryImage = itemView.findViewById<ImageView>(R.id.services_layout_image)
     // val categoryName = itemView.findViewById<TextView>(R.id.services_layout_text)


    fun bindCategory(category: Category, context: Context) {
    // val resourceId =
    // context.resources.getIdentifier(category.image, "drawable", context.packageName)
    //  categoryImage.setImageResource(resourceId)
    //  categoryName.text = category.title


        itemView.setOnClickListener { itemCliked(category) }

    }
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.row_item, parent, false)

    return RowHolder(view,itemCliked)
}

override fun onBindViewHolder(holder: RowHolder, position: Int) {
    holder.bindCategory(categories[position], context)

}

override fun getItemCount(): Int {
    return categories.count()
}

}

在你的活动中:

lateinit var categoriesAdapter:MyAdapter

在 Oncreate 内部:

categoriesAdapter = MyAdapter(this,DataService.services){
        category ->        
       //what you want to do while user click on a row
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多