Jua*_*ini 2 android toolbar android-fragments android-jetpack-navigation
我已经开始使用 Android Jetpack 的导航库。到目前为止,我觉得它很容易使用。我面临的问题是工具栏。
我想要实现的是:
我尝试了很多事情,但没有成功。前两次尝试使用具有NoActionBar样式的活动。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
android:background="@color/white"
tools:context=".ResetPasswordFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar_resetPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:liftOnScroll="true"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_resetPassword"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView_resetPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Linear layout -->
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这个尝试奏效了。它在我的活动中添加了一个工具栏,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:background="@color/colorAccent"
android:orientation="vertical"
tools:context=".LoginActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout >
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我添加了这个:
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
Toolbar toolbar = findViewById(R.id.toolbar);
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
Run Code Online (Sandbox Code Playgroud)
这里的问题是工具栏显示在我的第一个片段中,我不想要那样。我尝试从我的第一个片段中删除它并且它起作用了,但是当我尝试在我的第二个片段中再次显示它时,由于某种原因findViewById无法识别我的工具栏。所以我最终在我的第一个片段代码中再次显示了工具栏,就在第二个片段显示之前。它起作用了,但是当工具栏显示时您会注意到动画并且它看起来不太好,而且我知道直接从我的片段隐藏/显示工具栏是不行的。
那么,我需要做什么才能正确实现这一目标?将来,我也会有一些其他片段的活动,我将需要一个与我之前写的类似的行为。我想如果我从现在开始做这件事会更好。
谢谢!
您可以监听导航事件使用OnDestinationChangedListener。例如 :
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(NavController controller,
NavDestination destination, Bundle arguments) {
// getting the current fragment id.
int currentFragmentID = destination.getId();
if(currentFragmentID == R.id.fragment_with_toolbar){
// showing the toolbar
toolbar.setVisibility(View.VISIBLE);
} else if(currentFragmentID == R.id.fragment_without_toolbar){
// hiding the toolbar
toolbar.setVisibility(View.GONE);
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |