如何在片段中的 recyclerView 上打开不同的活动?

Posted

技术标签:

【中文标题】如何在片段中的 recyclerView 上打开不同的活动?【英文标题】:How to open a different activity on recyclerView in fragment? 【发布时间】:2021-11-02 11:35:30 【问题描述】:

我正在使用 recyclerView 在 home 片段中显示我的 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)

【问题讨论】:

【参考方案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




【讨论】:

【参考方案2】:

在视图持有者的 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() 方法中设置点击监听器,因为点击监听器会设置多次,因为每次在回收站视图中绑定项目时都会调用此方法方法。 参考官方doc

【讨论】:

以上是关于如何在片段中的 recyclerView 上打开不同的活动?的主要内容,如果未能解决你的问题,请参考以下文章

如何在recyclerview不同的项目点击上打开几个不同的片段?

片段中的 RecyclerView OnclickListener

当另一个片段中的数据发生更改时,如何刷新一个片段中的 RecyclerView

如何从 RecyclerView.Adapter<CardAdapter.ViewHolder> 打开片段

Android:RecyclerView 不显示片段中的列表项

将数据从 RecyclerView.Adapter 传递到片段 onClick