GridView上的按钮覆盖不响应OnClick事件

时间:2015-02-28 00:32:02

标签: android animation button gridview alpha

我的应用中有一个屏幕,在GridView中显示一些供用户选择的类别,如下所示:

enter image description here

当用户点击某个类别时,会出现一个按钮供用户继续,如下所示:

enter image description here

问题是,出现的绿色按钮不响应@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(这个)被称为。

额外:我在按钮上有一个alpha动画。虽然我调用.setVisibility(VIEW.GONE),但按钮消失,但无法单击按钮所在的位置。我已经阅读了关于动画的相似主题和VIEW.GONE冲突,它可能是一个bug,并建议使用.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);
                }
}

1 个答案:

答案 0 :(得分:2)

RelativeLayout中的按钮会从布局中窃取click事件。你应该从RelativeLayout中删除Button(因为你还没有使用它)并设置RelativeLayout的背景而不是按钮。