编辑图像按钮上的onclick函数不工作[In coordinatorLayout….Im正在将图像按钮插入collapsingToolbar标题的右侧,setOnClickListener不工作于该图像按钮……您可以在下面找到代码和图像。如有任何帮助,我们将不胜感激2
<android.support.design.widget.AppBarLayout
android:id="@+id/MyAppbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:gravity="center_vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapse_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">
<ImageButton
android:id="@+id/bgheader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nature"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/MyToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="25dp"
android:layout_marginRight="20dp"
android:gravity="bottom|end"
android:orientation="horizontal"
app:layout_collapseMode="pin">
<ImageButton
android:id="@+id/txt_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/edit"
android:onClick="dd"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relative1"
android:padding="8dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
private View mGroupName;
mGroupName = (View) findViewById(R.id.txt_group_name);
mGroupName.setOnClickListener(this);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txt_group_name: {
final EditText txtUrl = new EditText(GroupInformationActivity.this);
txtUrl.setText(group.getName());
new AlertDialog.Builder(GroupInformationActivity.this)
.setTitle("New name for the group")
.setView(txtUrl)
.setPositiveButton("Change", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
String name = txtUrl.getText().toString();
if (!group.getName().equals(name)) {
collapsingToolbar.setTitle(name);
new UpdateGroupTask().execute();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
}
.......
这可能有点晚了,但我遇到了同样的问题,原因是Toolbar
正在消耗点击事件,所以它没有传递到CollapsingToolbarLayout
中的ImageButton
。我最终只是把ImageButton
移到了Toolbar
里面,然后一切都很好。
您可以创建新的CustomerToolbar扩展Toolbar,并使CustomerToolbar方法:onTouchEvent和onInterceptTouchEvent返回false
就我而言:
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
if (alpha >= 1) {
return false
} else {
return super.onInterceptTouchEvent(ev)
}
}
override fun onTouchEvent(ev: MotionEvent?): Boolean {
if (alpha >= 1) {
return false
} else {
return super.onInterceptTouchEvent(ev)
}
}
看看我的工具栏代码:
Toolbar toolbar = (Toolbar
) findViewById(R.id.account_toolbar);
setSupportActionBar(toolbar);
btnEdit = toolbar.findViewById(R.id.btn_edit);
btnEdit.setVisibility(View.VISIBLE);
当您的按钮在工具栏布局中时,您必须首先初始化工具栏,然后通过引用工具栏可以访问其中的所有组件并执行操作。