对话框中的导航组件
我有一个对话框,应该根据菜单中的按钮单击在多个布局之间切换。这些不同的布局每个都有不同的逻辑(就像在多种形式之间切换一样),所以我尝试做的是在对话框内部使用自己的专用navgraph,将其隐藏在菜单中,直到用户从菜单中选择并膨胀它可以在按钮上填写对话框。
问题:第一次按预期工作 - 我将fragmentContainerview设置为始终可见,并且可以看到“家庭”屏幕,当我按下按钮时,片段会更改。 当我关闭并重新打开对话框时,fragmentContainer将什么都看不到(尽管称为fragment otCreateview,但看不到视图)。
自定义对话框XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:orientation="vertical">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layoutDescription="@xml/dialog_scene">
//... Some views and buttons
<androidx.fragment.app.FragmentContainerView
android:id="@+id/dialog_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@+id/frameLayout"
app:layout_constraintEnd_toEndOf="@+id/frameLayout"
app:layout_constraintStart_toStartOf="@+id/frameLayout"
app:layout_constraintTop_toTopOf="@+id/frameLayout"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/dialog_nav_graph" />
</androidx.constraintlayout.motion.widget.MotionLayout>
</RelativeLayout>
NAV图:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/filter_dialog_nav_graph"
app:startDestination="@id/realEstateFilter">
<fragment
android:id="@+id/dialog_home"
android:name="com.app.Fragments.dialog_home"
android:label="dialogHome" >
<action
android:id="@+id/action_dialogHome_to_otherScreen"
app:destination="@id/otherScreen" />
</fragment>
<fragment
android:id="@+id/otherScreen"
android:name="com.app.Fragments.other_screen"
android:label="otherScreen" />
</navigation>
在对话框内部处理导航的位置:
private void initializeViews(View view){
mNavHost = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.dialog_nav_fragment);
mNavController = mNavHost.getNavController();
mNavController.popBackStack(R.id.dialogHome, false); // I want the navigation default destination to be a certain form when the dialog is first opened
MotionLayout motionLayout = view.findViewById(R.id.motion_layout);
motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
@Override
public void onTransitionStarted(MotionLayout motionLayout, int startId, int endId) {
switch (endId){
case R.id.start: {
state = UiState.MENU;
break;
}
case R.id.transition: {
state = UiState.FILTER;
mNavController.navigate(R.id.action_dialogHome_to_otherScreen;
break;
}
case R.id.transition2:{
state = UiState.FILTER;
mNavController.popBackStack(R.id.dialogHome, false);
}
}
}
});
}
对问题有什么想法? 通常,这是在对话框内切换多个屏幕或导航的好方法吗?有什么一般建议吗?
I have a dialog that should switch between multiple layouts based on button clicks in a menu. These different layouts each has different logic accompanied to their UI (kind of like switching between multiple forms) so what I tried to do is having a FragmentContainer with its own dedicated NavGraph inside the dialog, hide it until the user chooses from the menu and inflate it to fill the dialog on button click.
The problem: The first time everything works as intended - I set the FragmentContainerView to always be visible and I can see the "home" screen, and when I press a button the fragment changes.
When I close and reopen the dialog the FragmentContainer will display nothing (no views are visible despite the fragment onCreateView being called).
Custom dialog XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:orientation="vertical">
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layoutDescription="@xml/dialog_scene">
//... Some views and buttons
<androidx.fragment.app.FragmentContainerView
android:id="@+id/dialog_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@+id/frameLayout"
app:layout_constraintEnd_toEndOf="@+id/frameLayout"
app:layout_constraintStart_toStartOf="@+id/frameLayout"
app:layout_constraintTop_toTopOf="@+id/frameLayout"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/dialog_nav_graph" />
</androidx.constraintlayout.motion.widget.MotionLayout>
</RelativeLayout>
Nav graph:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/filter_dialog_nav_graph"
app:startDestination="@id/realEstateFilter">
<fragment
android:id="@+id/dialog_home"
android:name="com.app.Fragments.dialog_home"
android:label="dialogHome" >
<action
android:id="@+id/action_dialogHome_to_otherScreen"
app:destination="@id/otherScreen" />
</fragment>
<fragment
android:id="@+id/otherScreen"
android:name="com.app.Fragments.other_screen"
android:label="otherScreen" />
</navigation>
Where I handle the navigation inside the dialog:
private void initializeViews(View view){
mNavHost = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.dialog_nav_fragment);
mNavController = mNavHost.getNavController();
mNavController.popBackStack(R.id.dialogHome, false); // I want the navigation default destination to be a certain form when the dialog is first opened
MotionLayout motionLayout = view.findViewById(R.id.motion_layout);
motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
@Override
public void onTransitionStarted(MotionLayout motionLayout, int startId, int endId) {
switch (endId){
case R.id.start: {
state = UiState.MENU;
break;
}
case R.id.transition: {
state = UiState.FILTER;
mNavController.navigate(R.id.action_dialogHome_to_otherScreen;
break;
}
case R.id.transition2:{
state = UiState.FILTER;
mNavController.popBackStack(R.id.dialogHome, false);
}
}
}
});
}
Any idea on what might be the problem?
In general is this a good approach for switching multiple screens or navigation inside a dialog doesn't work well together? Any general suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论