Android 侧面折叠工具栏布局中ImageView的OnClickListener
我有一个Android 侧面折叠工具栏布局中ImageView的OnClickListener,android,android-layout,material-design,android-view,android-toolbar,Android,Android Layout,Material Design,Android View,Android Toolbar,我有一个图像视图在一个折叠工具栏布局中,我想附加一个视图。OnClickListerner。但是,单击似乎不会触发侦听器 <?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.androi
图像视图
在一个折叠工具栏布局
中,我想附加一个视图。OnClickListerner
。但是,单击似乎不会触发侦听器
<?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>
编辑:
这是我附加到折叠工具栏布局的OnTouchListener
(信用证:)
如果启用show layout bounds
,您会看到,ImageView
的顶部有一个布局,它实际上不会让您的单击事件到达ImageView
我最终得到的是:
等待折叠工具栏布局
完全展开
展开后,您可以获得ImageView
的Rect
(此Rect
包含视图层次结构中视图的坐标)
一旦你有了这个矩形,你应该在collappingtoolbarlayout
如果该触摸事件的x、y位置在上一步保存的Rect
内,则应截取该触摸事件并将该MotionEvent
委托给ImageView
作为一个实现,您可以看到这个项目mMapLogoRect
是ImageView
的Rect
,位于折叠工具栏布局中
您可以尝试以下两种方法吗:
1.从折叠工具栏布局中删除以下两项:
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
并将其放入工具栏:
app:layout_collapseMode="pin"
这样做:首先测试1,然后测试2,然后测试两者。
还要确保将ImageView的高度设置为更大的值,如200dp。如果有任何标准使它起作用,请让我知道。然后我会相应地编辑它。我发现了问题。工具栏
的高度被设置为包裹内容
,这使得它在展开时可以扩展到整个AppBarLayout
,并使用所有触摸事件。对于任何人来说,你有两个解决方案。您可以按照@azizbekian的建议进行操作,但将侦听器注册到工具栏,而不是折叠工具栏布局
,或者将工具栏的布局高度
属性更改为固定值(例如android:layout_height=“?attr/actionBarSize”
),但在这种情况下,它不会触发对图像视图
顶部的单击,而工具栏实际上位于该位置。出于某种原因,OnTouchListener
不会在折叠工具栏布局
上触发。我用完整的xml代码更新了anwser。
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_collapseMode="pin"