Android 中关于 NavController 的理解题
Comprehension question about the NavController in Android
我刚开始在 Android 中使用 Jetpack Navigation,我有一个问题。我在实现 View.OnClickListener
的 Fragment 中有以下方法
public void onClick(View view) {
if(view.getId() == R.id.imageButton_Softdrinks_en) {
int amount = 1;
Menu_FragmentDirections.ActionMenuFragmentToSoftdrinks action = Menu_FragmentDirections
.actionMenuFragmentToSoftdrinks(amount);
Navigation.findNavController(view).navigate(action);
}
}
基本上这个方法是有效的,但我不太明白为什么。方法声明中的视图应该是 ImageButtons。使用命令时:"Navigation.findNavController(view)" Android 搜索视图的 Navcontroller。鉴于该视图是一个 ImageButtom,我不明白为什么导航有效。 ImageButtoms 没有 NavController 或 NavHost。谁介意向我解释一下。我会感谢每一条评论,非常感谢你的帮助。
您可以在 View
层次结构中的任何 View
上调用 Navigation.findNavController(View)
。然后系统会向上遍历层级,直到找到父Fragment
(NavHostFragment
),然后它会找到NavHostFragment
.
的NavController。
我刚开始在 Android 中使用 Jetpack Navigation,我有一个问题。我在实现 View.OnClickListener
的 Fragment 中有以下方法public void onClick(View view) {
if(view.getId() == R.id.imageButton_Softdrinks_en) {
int amount = 1;
Menu_FragmentDirections.ActionMenuFragmentToSoftdrinks action = Menu_FragmentDirections
.actionMenuFragmentToSoftdrinks(amount);
Navigation.findNavController(view).navigate(action);
}
}
基本上这个方法是有效的,但我不太明白为什么。方法声明中的视图应该是 ImageButtons。使用命令时:"Navigation.findNavController(view)" Android 搜索视图的 Navcontroller。鉴于该视图是一个 ImageButtom,我不明白为什么导航有效。 ImageButtoms 没有 NavController 或 NavHost。谁介意向我解释一下。我会感谢每一条评论,非常感谢你的帮助。
您可以在 View
层次结构中的任何 View
上调用 Navigation.findNavController(View)
。然后系统会向上遍历层级,直到找到父Fragment
(NavHostFragment
),然后它会找到NavHostFragment
.