setOnClickListener和setOnLongClickListener不能同时工作

时间:2018-12-05 01:55:57

标签: java android

setOnClickListener和setOnLongClickListener不能同时工作。当我删除一个时,另一个起作用,反之亦然。

此外,我想说的是,如果代码的第一行更改为childRelativeLayout.setVisibility (View.GONE);,则parentRelativeLayout.setOnClickListener根本不起作用。

        childRelativeLayout.setVisibility(View.VISIBLE);
    Animation slide_down = AnimationUtils.loadAnimation(context, R.anim.slide_down);
    Animation slide_up = AnimationUtils.loadAnimation(context, R.anim.slide_up);

    parentRelativeLayout.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (childRelativeLayout.getVisibility() == View.GONE) {
                childRelativeLayout.startAnimation(slide_down);
                slide_down.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        childRelativeLayout.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
            } else {
                childRelativeLayout.startAnimation(slide_up);
                slide_up.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        childRelativeLayout.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
            }
        }
    });

和onLongClick

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
            mBuilder.setTitle("Edytuj zadanie");

            LayoutInflater mInflater = LayoutInflater.from(context);
            @SuppressLint("InflateParams") View mView = mInflater.inflate(R.layout.daily_alertdialog, null, false);

            //here is code...

            mBuilder.setView(mView);
            AlertDialog alertDialog = mBuilder.create();
            alertDialog.show();

            return true;
        }
    });

parentRelativeLayout

<RelativeLayout
    android:id="@+id/parentRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8sp"
    android:paddingStart="8sp"
    android:paddingEnd="8sp"
    android:paddingBottom="4sp">

    <TextView
        android:id="@+id/goal_list_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="22sp"
        android:layout_marginBottom="8sp"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        tools:text="Cel 1" />

    <TextView
        android:id="@+id/category_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goal_list_name_text_view"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp"
        tools:text="Kategoria: xyz" />

    <TextView
        android:id="@+id/place_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goal_list_name_text_view"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp"
        tools:text="Miejsce wykonania:" />

    <TextView
        android:id="@+id/date_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/goal_list_name_text_view"
        android:layout_alignParentEnd="true"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp"
        tools:text="14 luty 2018" />

    <TextView
        android:id="@+id/time_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/date_text_view"
        android:layout_alignBottom="@+id/goal_list_name_text_view"
        android:textColor="@android:color/darker_gray"
        android:textSize="12sp"
        tools:text="16:00" />

</RelativeLayout>

childRelativeLayout

 <RelativeLayout
    android:id="@+id/childRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/parentRelativeLayout"
    android:layout_margin="4sp"
    android:animateLayoutChanges="true"
    tools:ignore="Orientation"
    android:visibility="gone">

    <View
        android:id="@+id/childView"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#f5000000"
        android:layout_marginTop="2sp"/>

    <TextView
        android:id="@+id/plusDayTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12sp"
        android:layout_marginTop="2sp"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/oneDay"
        android:textColor="@android:color/black"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/doneTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="2sp"
        android:layout_marginEnd="12sp"
        android:clickable="true"
        android:focusable="true"
        android:text="@string/done"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        tools:ignore="RelativeOverlap" />

</RelativeLayout>

slide_down.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="400"
    android:fromYDelta="-60%"
    android:toYDelta="0" />

slide_up.xml

<translate
    android:duration="400"
    android:fromYDelta="0"
    android:toYDelta="-60%" />

1 个答案:

答案 0 :(得分:0)

您的_first返回onLongClick到操作系统,指示该事件已被处理,不应调用其他单击事件。尝试将该值更改为true,这样可以解决您的问题。