在片段替换后滚动FrameLayout时,折叠工具栏不会折叠

时间:2015-09-02 15:01:18

标签: android android-fragments android-toolbar android-collapsingtoolbarlayout

所以我正在使用Navigation View实现一个带有5个抽屉(片段)的应用程序,每次点击抽屉时,它会加载/替换content某个layout选定的片段抽屉:

MainActivity.Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    setupToolbar();
    setupCollapsingToolbar();
    setupNavigationDrawer();
}

public void setupToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

public void setupCollapsingToolbar() {
    collapsing_toolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsing_toolbar.setTitle(toolbar.getTitle());
}

public void setupNavigationDrawer() {
    drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
    navigation_view = (NavigationView) findViewById(R.id.navigation_view);

    navigation_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            fragment_manager = getSupportFragmentManager();
            fragment_transaction = fragment_manager.beginTransaction();
            //fragment_transaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

            menuItem.setChecked(true);

            switch (menuItem.getItemId()) {
                case R.id.cor:
                    fragment_transaction.replace(R.id.content, cor_fragment);
                    break;
                case R.id.sched:
                    fragment_transaction.replace(R.id.content, sched_fragment);
                    break;
                case R.id.eval:
                    fragment_transaction.replace(R.id.content, eval_fragment);
                    break;
                case R.id.inc:
                    fragment_transaction.replace(R.id.content, inc_fragment);
                    break;
                case R.id.clear:
                    fragment_transaction.replace(R.id.content, clear_fragment);
                    break;
            }

            mHandler = new Handler();
            mHandler.removeCallbacksAndMessages(null);
            int delay = 400;
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fragment_transaction.addToBackStack(null);
                    fragment_transaction.commit();
                }
            }, delay);
            // The millisecond delay is arbitrary and was arrived at through trial and error

            drawer_layout.closeDrawers();

            return true;
        }
    });

    drawer_toggle = new ActionBarDrawerToggle(this, drawer_layout, toolbar, R.string.open_drawer, R.string.close_drawer) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    drawer_layout.setDrawerListener(drawer_toggle);
    drawer_toggle.syncState();

    drawer_layout.openDrawer(GravityCompat.START);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_toolbar, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawer_layout.openDrawer(GravityCompat.START);
            return true;
        case R.id.action_sign_out:
            onSignOutClicked();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

因此,我尝试合并<CollapsingToolbarLayout>,当我尝试将其添加到fragment_main.xml时:

activity_main.xml中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/fragment_main"/>

    <include layout="@layout/navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

fragment_main.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginBottom="32dp"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/cover_nav_header"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:background="@color/primary_dark"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ActionBarPopupThemeOverlay"
                app:menu="@menu/menu_toolbar"
                app:layout_collapseMode="pin"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <!-- <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"> -->

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:orientation="vertical" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <!-- </android.support.v4.widget.NestedScrollView> -->

</android.support.design.widget.CoordinatorLayout>

然后, <FrameLayout> 会滚动!它根本不会影响Collasping Toolbar

我尝试选择<NestedScrollView>(您可以从我评论的内容中看到)而不是<FrameLayout><LinearLayout>,但是内容赢了“ t被片段的布局取代,只能看到一个白色的空白区域。

我也试过嵌套它,<NestedScrollView>作为父项,<FrameLayout>作为孩子,但无济于事。

为什么Collapsing Toolbar不会崩溃有什么特别的原因吗?我错过了Collapsing Toolbar指南的内容吗?因为据我所知,所有这些都在.xml而且没有那么多Java干扰,只在.java文件中设置标题。

这是否与我 MainActivity.java 中的 fragment_transaction.replace() 有关?这只是我的推测。

0 个答案:

没有答案