在主机和子碎片之间进行通信

时间:2020-07-24 18:26:48

标签: android android-fragments mvvm viewmodel android-livedata

我有以下情况:

Fragment (A) - Contains 3 Tabs. 
           Inside each Tab [Layout] , another Fragment (B) (using FragmentStateAdapter).

Fragment (B) - Contains a RecyclerView. 
           Inside the RecyclerView some actionable Tiles.

        Tile - Contains some (textual) information and a Button (for navigation) 

Fragment (C) - Contains detailed information about selected Tile.

总而言之,每个选项卡都将以recyclerview形式包含元素列表,这些选项卡之间唯一的不同是一些杂项过滤器,这些过滤器将为用户整理列表。

我想从Tile中导航,一直回到片段(A),然后进入片段(C)。我目前的尝试涉及使用SharedViewModel将选定的Tile存储为MutableLiveData,并在Fragment(A)中进行观察。它永远不会进入观察者块。第二种方法涉及一个自定义侦听器接口,该接口在片段(B)中声明,并由片段(A)实现。它永远不会到达片段(A)中的替代方法。

我应该只能观察LiveData,有人知道我为什么不能这样做吗?

0 个答案:

没有答案
相关问题