简体   繁体   English

RecyclerView 项目单击并打开另一个 recyclerview

[英]RecyclerView item click and open another recyclerview

enter image description hereI have almost 250 patent item which i want to show in a parent recyclerview.在此处输入图像描述我有近 250 项专利项目,我想在父回收站视图中显示这些项目。 After item click of parent recyclerview it will show over hundreds of data under each parent item in another recycler view.在父回收者视图的项目单击后,它将在另一个回收者视图中的每个父项下显示数百个数据。 How can i do it like this picture.我怎么能像这张照片那样做。

在此处输入图片说明

I would do it this way.我会这样做。 Create an interface:创建接口:

interface AdapterContract{
  fun onListItemClick(id: String) //whatever the parameter is
}

When you instantiate the Adapter in your Fragment1 , you can implement this interface:当您在Fragment1实例化 Adapter 时,您可以实现此接口:

class Fragment1 : Fragment(), AdapterContract{
  ...
  //when instantiating the adapter:
  private val adapter: MyAdapter by lazy{
      MyAdapter(this //for the interface)
  }
  //override the method of Adapter contract
}

Your adapters constructor should look like this: MyAdapter(adapterContract: AdapterContract) : ListAdapter<MyAdapter.MyViewHolder>(Diff_Util_SOME_OBJECT)您的适配器构造函数应如下所示: MyAdapter(adapterContract: AdapterContract) : ListAdapter<MyAdapter.MyViewHolder>(Diff_Util_SOME_OBJECT)

Than in the adapters ViewHolder:比在适配器 ViewHolder 中:

itemHolder?.setOnClickListener{
 adapterContract.onListItemClick(someId)
}

Now in your override method in Fragment1 :现在在Fragment1的覆盖方法中:

override method onListItemClick(id: String){
  //pass this id to the next opening fragment (like Bundles or navargs)
 //init `Fragment2` which is going to have all items, that belong to `Fragment1` selected item
}

Load the data :)加载数据:)

I hope you are using a database or something.我希望您使用的是数据库或其他东西。 Should work with hard coded data structures as well, but just to be safe :).也应该使用硬编码的数据结构,但只是为了安全:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM