I've got an ImageView
inside of a CollapsingToolbarLayout
and I'd like to attach a View.OnClickListerner
to it. However a click doesn't seem to trigger the listener.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_nade_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/view_nade_appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@null"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/view_nade_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/view_nade_backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
<android.support.v7.widget.Toolbar
android:id="@+id/view_nade_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin"
android:paddingTop="24dp">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/view_nade_fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_mode_edit_white_24dp"
app:fab_colorNormal="?android:attr/colorAccent"
app:fab_colorPressed="?android:attr/colorAccent"
app:layout_anchor="@id/view_nade_appbar"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
Here is the Activity:
@BindView(R.id.view_nade_backdrop) ImageView mBackdrop;
@BindView(R.id.view_nade_appbar) AppBarLayout mAppbar;
@BindView(R.id.view_nade_collapsing_toolbar) CollapsingToolbarLayout mCollapsingToolbar;
@BindView(R.id.view_nade_toolbar) Toolbar mToolbar;
private final View.OnClickListener mOnBackdropClickListener = (v) ->
{
final Intent intent = new Intent(ViewNadeActivity.this, GalleryActivity.class);
intent.putExtra(GalleryActivity.EXTRA_PHOTO_URIS, ListUtils.join(mUtilityData.getImgUris(this)));
startActivity(intent);
};
@Override
protected void onCreate(Bundle
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_nade);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
mBackdrop.setOnClickListener(mOnBackdropClickListener);
}
EDIT:
Here is the OnTouchListener
that I attach to the CollapsingToolbarLayout
(credit: @azizbekian)
private final View.OnTouchListener mOnToolbarTouchListener = new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
int action = motionEvent.getActionMasked();
if(mBackdropBounds != null && (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL))
{
if(mBackdropBounds.contains((int) motionEvent.getRawX(), (int) motionEvent.getRawY()))
{
if(mBackdrop != null)
{
mBackdrop.dispatchTouchEvent(motionEvent);
return true;
}
}
}
return false;
}
};
I found the problem. The Toolbar
's height was set to wrap_content
which made it extend across the whole AppBarLayout
when expanded and consume all touch events. For anyone that comes along, you have 2 solutions. You can either do what @azizbekian proposed, but register the listener to the Toolbar
and not the CollapsingToolbarLayout
or change the Toolbar
's layout_height
property to a fixed value (ex. android:layout_height="?attr/actionBarSize"
), but in that case it won't trigger the click on top of the ImageView
where the Toolbar
actually is.