android RecyclerView上的OnClickListener,以详细说明不工作的片段

z9smfwbn  于 2023-01-03  发布在  Android
关注(0)|答案(2)|浏览(138)

我尝试在RecyclerView上配置一个onClickListener来点击项目,这样每次点击项目都可以转到它的细节片段。我一直尝试使用方法onBindViewHolder,但没有运气。我不确定我是否可以使用接口或只是提到的方法。
适配器:

class TensesAdapter(private val sectionList : ArrayList<Tenses>) : RecyclerView.Adapter<TensesAdapter.MyViewHolder>() {
        
        
  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        
                val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item,
                parent, false)
                return MyViewHolder(itemView)
        
            }
        
            override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                val currentItem = sectionList[position]
                holder.sectionImage.setImageResource(currentItem.sectionImage)
                holder.tvHeading.text = currentItem.sectionHeading
                holder.itemView.setOnClickListener(object :View.OnClickListener {
        
        
                    }
                })
        
        
            }
        
            override fun getItemCount(): Int {
                return sectionList.size
            }
        
            class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
                val sectionImage : ImageView = itemView.findViewById(R.id.sectionImage)
                val tvHeading : TextView = itemView.findViewById(R.id.sectionHeading)
            
}
z18hc3ub

z18hc3ub1#

您可以使用接口

interface OnTensesClickListener {
    fun onItemClick(tenses : Tenses)
}

在你的TensesAdapter里,

class TensesAdapter(private val sectionList : ArrayList<Tenses>, private val onTensesClickListener: OnTensesClickListener): RecyclerView.Adapter<TensesAdapter.MyViewHolder>() {
    // Your Code...
}

onBindViewHolder中,

holder.itemView.setOnClickListener(object :View.OnClickListener {
    onTensesClickListener.onItemClick(currentItem)
})

最后,在初始化时的片段中...

val adapter = TensesAdapter(sectionList, object: OnTensesClickListener {
    fun onItemClick(currentItem) {
        // Navigate to other fragment
    }
})
xmjla07d

xmjla07d2#

我已经在适配器和片段上输入了下面的代码,但是我只是缺少在单击列表器时转到特定位置的代码。
“* 适配器 *”:

interface OnTensesClickListener {
    fun onItemClick(tenses : Tenses)
}

class TensesAdapter(private val sectionList : ArrayList<Tenses>,private val onTensesClickListener: OnTensesClickListener)  : RecyclerView.Adapter<TensesAdapter.MyViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item,
        parent, false)
        return MyViewHolder(itemView)

    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = sectionList[position]
        holder.sectionImage.setImageResource(currentItem.sectionImage)
        holder.tvHeading.text = currentItem.sectionHeading
        holder.itemView.setOnClickListener(object :View.OnClickListener {
            override fun onClick(p0: View?) {
                TODO("Not yet implemented")
            }

        })

    }

    override fun getItemCount(): Int {
        return sectionList.size
    }

    class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
        val sectionImage : ImageView = itemView.findViewById(R.id.sectionImage)
        val tvHeading : TextView = itemView.findViewById(R.id.sectionHeading)

    }

}

“* 片段 *”:

package com.baonumerique.loba

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

val adapter = TensesAdapter(sectionList, object: OnTensesClickListener {
    fun onItemClick(currentItem) {
        // Navigate to other fragment
    }
})

class PresentTenseFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_present_tense, container, false)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment PresentTenseFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            PresentTenseFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

相关问题