Dmitry Sokolov Dmitry Sokolov - 4 months ago 13
Android Question

Menu items don't respond in CollapsingTollbarLayout (AppBarLayout)

I have an Activity with collapsing Toolbar and everything works fine except touch events with menu items and back arrow button.

If Toolbar (android.support.v7.widget.Toolbar) puts outside CollapsingTollbarLayout everything works well.

As i understand, collapsing is grabbing touch event from the toolbar and it can't be reached, right?
I'm tried requestDisallowInterceptTouchEvent set to false, but no luck.

Like this:

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


Please help me understand what did i miss. Thanks a lot!

Here is my layout:

<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>


My menu code looks like this:

@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);
}
}

Answer

I have solved this issue by adding another one Toolbar here:

        <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>

I bet it's not the best way (far far away from it) to do it, but it works fine.

Comments