1

我正在使用 recyclerView 在主页片段中显示我的 listItems。但是我一直坚持在单击项目时如何打开不同的活动。我该如何做接下来的步骤?你能帮我解决这个问题吗?我将不胜感激。

HomeFragment.kt

class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

lateinit var recycle1:RecyclerView
private val list = ArrayList<Locations>()
private val adapter:Adapter = Adapter(list)

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    var v =inflater.inflate(R.layout.fragment_home, container, false)

    recycle1 = v.findViewById(R.id.rv_item)
    recycle1.layoutManager = LinearLayoutManager(activity)

    list.clear()
    testData()

    val adapterr = Adapter(list)
    recycle1.adapter = adapterr
    adapter.notifyDataSetChanged()
    recycle1.setHasFixedSize(true)
    return v
}

private fun testData(){
    list.add(Locations(R.drawable.book_cafe,"The Book Cafe","20 Martin Rd","Cafe"))
    list.add(Locations(R.drawable.citysprouts,"City Sprouts","102 Henderson Road","Cafe"))
    list.add(Locations(R.drawable.esplanade,"Library@esplande","8 Raffles Ave","Library"))
    list.add(Locations(R.drawable.hf,"Library@Harbourfront","1 HarbourFront Walk","Library"))
    list.add(Locations(R.drawable.mangawork,"MangaWork","291 Serangoon Rd","Cafe"))
    list.add(Locations(R.drawable.orchard,"Library@orchard","277 Orchard Road","Library"))
    list.add(Locations(R.drawable.rabbitandfox,"Rabbit&Fox","160 Orchard Rd","Cafe"))
    list.add(Locations(R.drawable.sixlettercoffee,"T6 Letter Coffee","259 Tanjong Katong Rd","Cafe"))

}

}

适配器.kt

class Adapter (val listItem:ArrayList<Locations>) : RecyclerView.Adapter<Adapter.RecycleViewHolder>(){
inner class RecycleViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    val itemImage: ShapeableImageView = itemView.findViewById(R.id.item_image)
    val heading: TextView = itemView.findViewById(R.id.item_title)
    val detail: TextView = itemView.findViewById(R.id.item_detail)
    val category: TextView = itemView.findViewById(R.id.item_categories)

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewHolder {
    val view:View = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
    return RecycleViewHolder(view)
}

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

override fun onBindViewHolder(holder: RecycleViewHolder, position: Int) {
    val item = listItem[position]
    holder.itemImage.setImageResource(item.itemImage)
    holder.heading.text = item.headings
    holder.detail.text = item.detail
    holder.category.text = item.category
}

地点。吨

data class Locations(var itemImage:Int,var headings :String,var detail :String,var category :String)
4

2 回答 2

1

在视图持有者的 init {} 块中设置一个 clickListener 例如。

inner class RecycleViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    val itemImage: ShapeableImageView = itemView.findViewById(R.id.item_image)
    val heading: TextView = itemView.findViewById(R.id.item_title)
    val detail: TextView = itemView.findViewById(R.id.item_detail)
    val category: TextView = itemView.findViewById(R.id.item_categories)
   
    init {
       itemView.setOnClickListener { // start your activity using view context  }
     }

}

永远不要在 onBindViewHolder() 方法中设置您的点击侦听器,因为点击侦听器将设置多次,因为每次在回收站视图中绑定项目时都会调用此方法方法。参考官方文档

于 2021-09-04T18:42:42.377 回答
1

您必须借助界面来导航:

首先创建一个界面,假设它命名为 Navigate


interface Navigate {


    fun onRecyclerViewItemClicked(location : Location)

}

然后您必须在适配器和片段中使用此接口: 在您的适配器中,您必须执行以下操作:

class Adapter (val listItem:ArrayList<Locations>) : RecyclerView.Adapter<Adapter.RecycleViewHolder>(){

//Declare listener object
    var listener: Navigate? = null


inner class RecycleViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
    val itemImage: ShapeableImageView = itemView.findViewById(R.id.item_image)
    val heading: TextView = itemView.findViewById(R.id.item_title)
    val detail: TextView = itemView.findViewById(R.id.item_detail)
    val category: TextView = itemView.findViewById(R.id.item_categories)

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewHolder {
    val view:View = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
    return RecycleViewHolder(view)
}

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

override fun onBindViewHolder(holder: RecycleViewHolder, position: Int) {
    val item = listItem[position]
    holder.itemImage.setImageResource(item.itemImage)
    holder.heading.text = item.headings
    holder.detail.text = item.detail
    holder.category.text = item.category

     // Now suppose you want to navigate to another fragment / Activity on click of the itemImage , then do the following 
   holder.itemImage.setOnClickListener{
            
     listener?.onRecyclerViewItemClicked(item)
    }
}

现在在您的片段中扩展 Navigate 类并实现覆盖方法:

class HomeFragment : Fragment() , Navigate {

// Rest of your code


override fun onRecyclerViewItemClicked(location : Location){

   // The argument location here is the information of the item that is clicked of 
   // the RecyclerView
  // Here write your code to navigate to the next fragment / activity based on what 
  //you are using NavController / Fragment Manager
}


}
于 2021-09-04T18:12:15.597 回答