覆盖在 GridView 上的按钮不响应 OnClick 事件
Button overlayed on a GridView does not respond to OnClick events
我的应用程序中有一个屏幕,它在 GridView 中为用户显示一些类别 select,如下所示:
当用户点击一个类别时,会出现一个按钮供用户继续,如下所示:
问题是,出现的绿色按钮没有响应@OnClick(我正在使用 ButterKnife 进行查看)。
布局如下xml:
activity_select_categories.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/g"
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/h"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="22dp"
android:layout_marginBottom="22dp"
android:textSize="23sp"
android:text="CATEGORIES" />
<GridView
android:id="@+id/gridView_selectCategoriesLayout_categories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:clickable="true"
android:id="@+id/button_selectCategoriesLayout_continue"
android:alpha="0.8">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#22c064"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:background="#22c064"
android:src="@drawable/checkmark"
/>
</RelativeLayout>
这是自定义网格视图元素:
grid_tem.xml
<com.entu.artapp.utils.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/root"
android:gravity="center_horizontal">
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_squareImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="invisible"
/>
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_selectedColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e85349"
android:visibility="invisible"
android:alpha="0.45"
android:scaleType="centerCrop"/>
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_constantGrey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:alpha="0.45"
android:scaleType="centerCrop"/>
<com.entu.artapp.utils.RectangularLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/s"
android:orientation="vertical">
<ImageView
android:id="@+id/gridItem_selectedCheckMark"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/checkmark"
android:visibility="invisible"
/>
</com.entu.artapp.utils.RectangularLinearLayout>
<ProgressBar
android:id="@+id/gridItem_progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:visibility="visible"/>
<TextView
android:id="@+id/gridItem_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="15sp"
android:textAllCaps="true"
android:textStyle="bold"
android:visibility="invisible"/>
下面是我为按钮设置 OnClick 的方式:
@OnClick(R.id.button_selectCategoriesLayout_continue)
public void setContinueButton () {
ParseUser.getCurrentUser().put("followedCategories", categoriesSelectedList);
ParseUser.getCurrentUser().put("followedCategoriesNames", categoriesNamesSelectedList);
ParseUser.getCurrentUser().saveInBackground();
}
谁能帮我解决为什么按钮不响应 OnClick?
ButterKnife.inject(this) 已被调用。
额外:我在按钮上有一个 alpha 动画。虽然我是调用.setVisibility(VIEW.GONE),但是按钮消失了,但是按钮is/was的地方是无法点击的。我已经阅读了关于动画和 VIEW.GONE 冲突的类似主题,它可能是一个错误,并建议使用 .clearAnimation()。但是,如果我调用它,alpha 动画会重置,它会弄乱我的 UI.
谁能帮我解决这些问题?
干杯!
编辑 #1: 为按钮设置动画的代码:
@InjectView(R.id.button_selectCategoriesLayout_continue)
RelativeLayout continueButton;
categoriesGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
[...]
if (categoriesNamesSelectedList.size() == 0) {
deselectedAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
continueButton.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
continueButton.startAnimation(deselectedAnimation);
}
}
RelativeLayout 中的按钮窃取了布局中的点击事件。您应该从 RelativeLayout 中删除 Button(因为无论如何您都不会使用它)并设置 RelativeLayout 的背景而不是按钮。
我的应用程序中有一个屏幕,它在 GridView 中为用户显示一些类别 select,如下所示:
当用户点击一个类别时,会出现一个按钮供用户继续,如下所示:
问题是,出现的绿色按钮没有响应@OnClick(我正在使用 ButterKnife 进行查看)。
布局如下xml:
activity_select_categories.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/g"
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/h"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="22dp"
android:layout_marginBottom="22dp"
android:textSize="23sp"
android:text="CATEGORIES" />
<GridView
android:id="@+id/gridView_selectCategoriesLayout_categories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="2dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:clickable="true"
android:id="@+id/button_selectCategoriesLayout_continue"
android:alpha="0.8">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#22c064"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:background="#22c064"
android:src="@drawable/checkmark"
/>
</RelativeLayout>
这是自定义网格视图元素:
grid_tem.xml
<com.entu.artapp.utils.SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/root"
android:gravity="center_horizontal">
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_squareImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="invisible"
/>
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_selectedColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e85349"
android:visibility="invisible"
android:alpha="0.45"
android:scaleType="centerCrop"/>
<com.entu.artapp.utils.SquareImageView
android:id="@+id/gridItem_constantGrey"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:alpha="0.45"
android:scaleType="centerCrop"/>
<com.entu.artapp.utils.RectangularLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/s"
android:orientation="vertical">
<ImageView
android:id="@+id/gridItem_selectedCheckMark"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/checkmark"
android:visibility="invisible"
/>
</com.entu.artapp.utils.RectangularLinearLayout>
<ProgressBar
android:id="@+id/gridItem_progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:visibility="visible"/>
<TextView
android:id="@+id/gridItem_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="15sp"
android:textAllCaps="true"
android:textStyle="bold"
android:visibility="invisible"/>
下面是我为按钮设置 OnClick 的方式:
@OnClick(R.id.button_selectCategoriesLayout_continue)
public void setContinueButton () {
ParseUser.getCurrentUser().put("followedCategories", categoriesSelectedList);
ParseUser.getCurrentUser().put("followedCategoriesNames", categoriesNamesSelectedList);
ParseUser.getCurrentUser().saveInBackground();
}
谁能帮我解决为什么按钮不响应 OnClick? ButterKnife.inject(this) 已被调用。
额外:我在按钮上有一个 alpha 动画。虽然我是调用.setVisibility(VIEW.GONE),但是按钮消失了,但是按钮is/was的地方是无法点击的。我已经阅读了关于动画和 VIEW.GONE 冲突的类似主题,它可能是一个错误,并建议使用 .clearAnimation()。但是,如果我调用它,alpha 动画会重置,它会弄乱我的 UI.
谁能帮我解决这些问题?
干杯!
编辑 #1: 为按钮设置动画的代码:
@InjectView(R.id.button_selectCategoriesLayout_continue)
RelativeLayout continueButton;
categoriesGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
[...]
if (categoriesNamesSelectedList.size() == 0) {
deselectedAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
continueButton.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
continueButton.startAnimation(deselectedAnimation);
}
}
RelativeLayout 中的按钮窃取了布局中的点击事件。您应该从 RelativeLayout 中删除 Button(因为无论如何您都不会使用它)并设置 RelativeLayout 的背景而不是按钮。