问题
我有一个带有不同片段的导航抽屉。每个片段
都应该使用一个默认的工具栏,除了需要一个折叠式工具栏
的一个片段
。
我的问题
如何在片段之间切换工具栏?
我有一个带有不同片段的导航抽屉。每个片段
都应该使用一个默认的工具栏,除了需要一个折叠式工具栏
的一个片段
。
如何在片段之间切换工具栏?
看起来你想实现这样的效果。
我使用了常规工具栏制作了一个活动。在切换到折叠工具栏片段时,我将工具栏设为透明,并让片段的工具栏接管。在切换到其他片段时,工具栏的颜色保持不变。
这允许您在xml中管理完整的折叠工具栏布局结构,而逻辑留在片段中。
希望这可以帮到你。请参考链接的gif。
private void collapseAppBar() {
// Collapse the AppBarLayout with animation
mAppBarLayout.setExpanded(false, true);
}
private void lockAppBar() {
/* Disable the nestedScrolling to disable expanding the
appBar with dragging the nestedScrollView below it */
ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
/* But still appBar is expandable with dragging the appBar itself
and below code disables that too
*/
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override
public boolean canDrag(AppBarLayout appBarLayout) {
return false;
}
});
}
private void unLockAppBar() {
ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override
public boolean canDrag(AppBarLayout appBarLayout) {
return true;
}
});
}
}
我会这样使用这些函数:
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.fragment1:
unLockAppBar();
fragmentClass = first_Fragment.class;
break;
case R.id.fragment2:
collapseAppBar();
lockAppBar();
fragmentClass = second_Fragment.class;
break;
case R.id.fragment3:
collapseAppBar();
lockAppBar();
fragmentClass = third_Fragment.class;
break;
您可以轻松地从Fragment
获取Toolbar
,然后修改或更改该Toolbar
的某些属性。
要从您的Activity
获取Toolbar
,您可以考虑使用以下内容。
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
onResume
函数中更改Toolbar
,然后在每次从Fragment
返回到onStop
函数时撤消更改。否则,在从导航抽屉切换到其他Fragment
时,Fragment
中所做的更改也会继续进行到其他片段。Fragment
都应该有自己的Toolbar
,这样它们就不会相互冲突,并且可以根据需要进行修改。是的,从您的Activity
删除Toolbar
。Fragment
的布局中添加Toolbar
。<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
Fragment
中找到它。@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
// Modify your Toolbar here.
// ...
// For example.
// toolbar.setBackground(R.color.red);
// Create home button
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
onOptionsItemSelected
函数。@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}
ActionBarDrawerToggle
对象时必须设置工具栏(toolbar)的事实,而这是使导航抽屉在用户点击汉堡图标时打开的重要部分。 - Franco工具栏
)。另一些是“特殊”Fragments,并具有自己的可折叠工具栏。// Handle toolbar changes in different Fragments
val navController = findNavController(R.id.nav_host_fragment)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.my_special_fragment_with_collapsible_toolbar -> {
binding.toolbarMain.visibility = View.GONE
}
else -> {
binding.toolbarMain.visibility = View.VISIBLE
}
}
}