答案 0 :(得分:0)
我会这样做。创建一个界面:
interface AdapterContract{
fun onListItemClick(id: String) //whatever the parameter is
}
在Fragment1
中实例化适配器时,可以实现此接口:
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
}
您的适配器构造函数应如下所示:MyAdapter(adapterContract: AdapterContract) : ListAdapter<MyAdapter.MyViewHolder>(Diff_Util_SOME_OBJECT)
比适配器的ViewHolder:
itemHolder?.setOnClickListener{
adapterContract.onListItemClick(someId)
}
现在在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
}
加载数据:)
我希望您正在使用数据库之类的东西。应该也可以使用硬编码的数据结构,但是为了安全起见:)。