答案 0 :(得分:2)
当我阅读您的解释时,您知道ChildFragment的父级是NavHostFragment,而NavHostFragment的父级是ParentFragment。
NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
Fragment parent = (Fragment) navHostFragment.getParentFragment();
parent.getView().findViewById(R.id.element_id);
我尝试过的是演示项目,适合我的工作。
答案 1 :(得分:1)
使用活动:
val buttonView = (context as Activity).findViewById<View>(R.id.map_list_card_view)
使用片段:
val parent: Fragment? = (parentFragment as NavHostFragment).parentFragment
val buttonView = parent?.view?.findViewById<View>(R.id.map_list_card_view)
答案 2 :(得分:0)
尝试一下:
ParentFragment parent = (ParentFragment) getParentFragment();
parent.getView().findViewById(R.id.your_element);
答案 3 :(得分:0)
尝试一下:
public class Fragment2 extends Fragment {
View view;
...
...
public void setView(View view) {
this.view = view;
}
}
然后在调用片段事务之前调用mFragment.setView()
。我希望这会有所帮助!