在CollapsingTollbarLayout(AppBarLayout)中菜单项不响应

时间:2016-06-30 09:41:15

标签: android android-toolbar android-menu android-collapsingtoolbarlayout android-touch-event

我有一个带有折叠工具栏的活动,除了通过菜单项和后退箭头按钮触摸事件外,一切正常。

如果工具栏(android.support.v7.widget.Toolbar)放在CollapsingTollbarLayout外面,一切正常。

据我所知,折叠是从工具栏中抓取触摸事件而无法到达,对吧? 我试过requestDisallowInterceptTouchEvent设置为false,但没有运气。

像这样:

mToolbar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(false);
                return false;
            }
        });

请帮助我理解我错过了什么。非常感谢!

这是我的布局:

<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_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/sub_goals_list_container"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:fitsSystemWindows="true"
        android:background="@null"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/sub_goals_collapsingToolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:contentScrim="?attr/colorPrimaryDark"
            app:titleEnabled="true"
            app:toolbarId="@+id/sub_goals_list_toolbar"
            app:expandedTitleTextAppearance="@style/TitleSubGoalsStyle"
            app:expandedTitleMarginStart="72dp"
            app:expandedTitleMarginBottom="115dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:elevation="3dp">

            <ImageView
                android:id="@+id/bg_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@drawable/bg_create_big_goal"/>

            <android.support.v7.widget.Toolbar
                android:id="@id/sub_goals_list_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways"
                app:layout_collapseMode="pin"/>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_anchor="@id/sub_goals_list_container"
                android:layout_marginLeft="72dp"
                android:layout_marginTop="50dp"
                android:paddingRight="8dp"
                app:layout_collapseMode="parallax"
                style="@style/DetailsOfTheBigGoal"
                >

                // Some TextView here

            </RelativeLayout>

            <com.daimajia.numberprogressbar.NumberProgressBar
                app:progress_reached_color="@color/darkBlueSea"
                app:progress_unreached_color="@color/brightBlue"
                app:progress_text_color="@color/darkBlueSea"
                app:progress_text_size="14sp"
                app:progress_unreached_bar_height="3dp"
                app:progress_reached_bar_height="3dp"
                android:id="@+id/big_goal_inner_progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:layout_marginLeft="56dp"
                android:layout_marginTop="94dp"/>

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

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/fragmentContainer"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_sub_goal_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_plus_fab"
        app:borderWidth="0dp"
        app:layout_anchor="@id/sub_goals_list_container"
        app:layout_anchorGravity="bottom|left|end" />

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

我的菜单代码如下:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()){
        case android.R.id.home:
            onBackPressed();
            return true;
        case R.id.delete_big_goal:
            try {
                IntentionDAOHelper.deleteIntention(mBigGoal, mBigGoalsDAO);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

我已在此处添加了另一个工具栏来解决此问题:

        <android.support.design.widget.FloatingActionButton
        android:id="@+id/add_sub_goal_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_plus_fab"
        app:borderWidth="0dp"
        app:layout_anchor="@id/sub_goals_list_container"
        app:layout_anchorGravity="bottom|left|end" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/sub_goals_list_toolbar_uncollapse"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        />

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

我敢打赌这不是最好的方式(远离它),但它运作良好。