折叠工具栏仅适用于导航视图中的一个片段

May*_*ies 9 java xml android android-layout android-fragments

问题

我有一个带有不同碎片的导航抽屉.每个Fragment应该使用的默认工具栏,除了Fragment需要折叠的工具栏Toolbar.

我的问题

如何在片段的工具栏之间切换?

Ank*_*ush 6

看来您想要实现这样的目标。

没有什么花哨

我使用通用工具栏进行了活动。当切换到折叠的工具栏片段时,我已经使工具栏透明,并且片段的工具栏接管了。切换到其他片段时,工具栏的颜色保持不变。

这使您可以在xml中管理完整的折叠式工具栏布局结构,而逻辑保留在Fragment中。

希望这会有所帮助。请参阅链接的gif。

gif的要点


Hab*_*emi 5

我发现的最佳解决方案是轻松折叠、锁定它(保持折叠模式)并解锁 collapsingToolbar。

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;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我以这种方式使用这些功能:

    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;
Run Code Online (Sandbox Code Playgroud)


Rea*_*hed 4

Toolbar您可以轻松地从您的文件中获取Fragment,然后修改或更改.ToolbarFragment

Toolbar要从您那里获取,Activity您可以考虑使用它。

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Run Code Online (Sandbox Code Playgroud)

Toolbar现在,您需要在函数中进行更改,然后每次从内部函数onResume返回时撤消更改。否则,当从导航抽屉切换到其他片段时,在 中所做的更改也将应用于其他片段。FragmentonStopFragmentFragment

但就你而言,我建议每个人都Fragment应该有自己的Toolbar,这样就不会相互冲突,并且可以根据需要进行修改。是的,Toolbar从您的Activity.

Toolbar所以在你的布局中添加Fragment这样的。

<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"/>
Run Code Online (Sandbox Code Playgroud)

然后在里面找到它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);
}
Run Code Online (Sandbox Code Playgroud)

并重写该onOptionsItemSelected函数。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            getActivity().onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)