android onclick在recycleview上的listener到细节片段不工作

798qvoo8  于 2022-12-31  发布在  Android
关注(0)|答案(2)|浏览(97)

我尝试在recyclerView上配置onclick列表器来点击条目,这样每次点击条目都可以转到它的细节片段。我一直在尝试使用方法“onBindViewHolder”,但没有运气。我不确定我是否可以使用接口或只使用提到的方法。

========================================================================================
    
    

      The Adapter

    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)
    
    
        }
n53p2ov0

n53p2ov01#

您可以使用接口

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
    }
})
htrmnn0y

htrmnn0y2#

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

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)
                }
            }
    }
}

相关问题