我正在使用带有BottomNavigationView和单个活动的Android体系结构导航组件设计一个应用程序。 第一个选项卡是默认选项卡,应显示时间表。 要加载计划表,用户必须登录,因此我试图在MainActivity.java中使用以下代码实现条件导航:
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(android.R.style.Theme );
super.onCreate(savedInstanceState);
mContext = (Context) this;
setContentView(R.layout.activity_main);
mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
setupNavigation();
}
private void setupNavigation()
{
mToolbar = findViewById(R.id.toolbar);
mBottomTabs = findViewById(R.id.bottom_tabs);
setSupportActionBar(mToolbar);
mNavController = Navigation.findNavController(this, R.id.content);
NavigationUI.setupWithNavController(mBottomTabs, mNavController);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.schedFrag, R.id.studyPlanFragment).build();
NavigationUI.setupActionBarWithNavController(this, mNavController, appBarConfiguration);
mNavController.addOnDestinationChangedListener(
new NavController.OnDestinationChangedListener()
{
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
int id = destination.getId();
if (mViewModel.m_auth.isUserLoggedIn(mContext))
{
if(mViewModel.isDataLoaded(id))
mNavController.navigate(destination.getId());
else //data not loaded yet
{
mViewModel.loadData(id, MainActivity.this);
Bundle args = new Bundle();
args.putInt(MainViewModel.ARGS_NEXT_SCREEN, destination.getId());
mNavController.navigate(R.id.loadFrag, args);
}
}
else
{
mViewModel.m_LoginFragmentNextScreen = id;
int nextDestination =R.id.act_schFrag_to_logFrag;
mNavController.navigate(nextDestination);
}
}
});
}
主要活动布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
<fragment
android:id="@+id/content"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="@+id/bottom_tabs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.587"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_tabs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="@color/colorPrimary"
app:itemTextColor="@color/bottom_nav_color"
app:itemIconTint="@color/bottom_nav_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/tabs_navigation" />
导航图代码如下:
<?xml version="1.0" encoding="utf-8"?>
<fragment
android:id="@+id/logFrag"
android:name="com.myapp.Fragments.LoginFragment"
android:label="@string/title_login"
tools:layout="@layout/login_fragment" >
<action
android:id="@+id/act_logFrag_to_loadFrag"
app:destination="@id/loadFrag" />
</fragment>
<fragment
android:id="@+id/schedFrag"
android:name="com.myapp.Fragments.ScheduleFragment"
android:label="@string/hs_title_schedule"
tools:layout="@layout/fragment_schedule" >
<action
android:id="@+id/act_schFrag_to_logFrag"
app:destination="@id/logFrag"
app:popUpTo="@+id/schedFrag"
app:popUpToInclusive="true"
/>
<action
android:id="@+id/action_scheduleFragment_to_loadingFragment"
app:destination="@id/loadFrag" />
</fragment>
<fragment
android:id="@+id/loadFrag"
android:name="com.myapp.Fragments.LoadingFragment"
android:label="fragment_loading"
tools:layout="@layout/fragment_loading" >
<argument
android:name="FragmentType"
app:argType="integer" />
</fragment>
我的问题是:
我应该像现在那样使用专用片段来实现加载进度,还是应该以某种方式在SchedFragment中显示进度(可能是通过仅设置单个进度来设置单独的片段布局),然后通过切换使所有数据字段可见到包含数据字段的布局中?