onClickListener与OnTouchListener冲突

时间:2017-10-21 08:18:36

标签: android onclick android-cardview ontouchevent

伙计们,我想让onTouchListner与CardView中的onClickListner一起正常工作。我为CardView设置了一个onClickListener,我还为该CardView内部的imageview设置了一个onTouchListner。但我发现它会如果单击imageView,则同时执行onclick和onTouch函数。 ImageView应仅用于监视事件。点击cardview应该进入另一个活动以显示有关该事件的更多详细信息。

下面的文件请看一下。

<event.bruce.com.eventfetcher.view.MyCardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?attr/selectableItemBackground"
    android:clickable="true"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="15dp"
   >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">
            <ImageView
                android:id="@+id/event_image"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                 />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_alignBottom="@id/event_image"
                android:layout_alignRight="@id/event_image">
                <event.bruce.com.eventfetcher.view.CustomCircleImageViewView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/share"
                    android:src="@drawable/share"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    app:civ_border_width="0dp"
                    android:layout_margin="5dp"
                    app:civ_fill_color="#FFFFFF"
                    />

                <event.bruce.com.eventfetcher.view.CustomCircleImageViewView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/star"
                    android:src="@drawable/star"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    app:civ_border_width="0dp"
                    android:layout_margin="5dp"
                    app:civ_fill_color="#FFFFFF"
                    />
            </LinearLayout>

        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/display_month"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="SEP."
                        android:textSize="13sp"
                        android:textStyle="bold"
                        android:textColor="@android:color/holo_red_light"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/display_date"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="28sp"
                        android:textColor="@android:color/black"
                        android:text="28"
                        />

                </LinearLayout>

            </LinearLayout>
            <LinearLayout
                android:layout_weight="8"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/event_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:padding="5dp"
                    android:typeface="normal"
                    android:textColor="#000000"
                    android:text=""
                    android:textSize="18sp"
                    android:maxLines="2"
                     />


            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</event.bruce.com.eventfetcher.view.MyCardView>

这是我设置onClickListener和onTouchLister的地方。

 public MyViewHolder(View view) {
        super(view);
        cardView = (CardView) view.findViewById(R.id.cardView);
        cardView.setRadius(12);
        cardView.setCardElevation(8);
        cardView.setContentPadding(5, 5, 5, 5);

        event_image = (ImageView) view.findViewById(R.id.event_image);


        event_name = (TextView) view.findViewById(R.id.event_name);
       //event_time = (TextView) view.findViewById(R.id.event_time);
        star = (ImageView) view.findViewById(R.id.star);
        share = (ImageView) view.findViewById(R.id.share);

        star.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                int action = ev.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        Log.e(TAG, "onTouchEvent action:ACTION_DOWN");

                      //Toast.makeText(MyApplication.getContext(), 
                       "Login 
                        successfull" + User.getUserInstance().getUserName(), 
                        Toast.LENGTH_SHORT).show();

                        ObjectAnimator scaleXAnimator = 
                        ObjectAnimator.ofFloat(star, "scaleX", 1f, 0.5f, 
                        1f);
                        ObjectAnimator scaleYAnimator = 
                        ObjectAnimator.ofFloat(star, "scaleY", 1f, 0.5f, 
                        1f);
                        AnimatorSet set = new AnimatorSet();
                        set.play(scaleXAnimator).with(scaleYAnimator);
                        set.setDuration(250);
                        set.start();

                        starEvent((int) v.getTag(), star);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.e(TAG, "onTouchEvent action:ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e(TAG, "onTouchEvent action:ACTION_UP");
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.e(TAG, "onTouchEvent action:ACTION_CANCEL");
                        break;
                }
                return true;
            }
        });

        share.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                int action = ev.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        //Save event to server
                        int eventPos = (Integer) v.getTag();
                        Event touchedEvent = api.results.get(eventPos);
                        showShare(touchedEvent);
                        //After saved
                        Log.e(TAG, "onTouchEvent action:ACTION_DOWN");

                        ObjectAnimator scaleXAnimator = 
                        ObjectAnimator.ofFloat(share, "scaleX", 1f, 0.5f, 
                        1f);

                        ObjectAnimator scaleYAnimator = 
                        ObjectAnimator.ofFloat(share, "scaleY", 1f, 0.5f, 
                        1f);
                        AnimatorSet set = new AnimatorSet();

                        set.play(scaleXAnimator).with(scaleYAnimator);
                        set.setDuration(250);
                        set.start();

                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.e(TAG, "onTouchEvent action:ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e(TAG, "onTouchEvent action:ACTION_UP");
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.e(TAG, "onTouchEvent action:ACTION_CANCEL");
                        break;
                }
                return true;
            }
        });

        display_date = (TextView) view.findViewById(R.id.display_date);
        display_month = (TextView) view.findViewById(R.id.display_month);

    }

在onBindViewHolder中,我为整个cardview本身设置了一个onClickListener。

 public void onBindViewHolder(RecyclerView.ViewHolder holder, final int 
 position) {
//BitmapUtils bitmapUtils = new BitmapUtils(MyApplication.getContext());
    holder = (MyViewHolder) holder;
    ((MyViewHolder) holder).itemView.setOnClickListener(new 
       View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("Image path", "" + api.results.get(position).image_url);
            uiListener.startDetailActivity(api.results.get(position));
        }
    });




  ((MyViewHolder)holder).event_name.setText(api.results.get(position).name);
    Date latestDate = getMostDate((ArrayList<Event.Schedule>) 
    api.results.get(position).getSchedules());
    api.results.get(position).setLatestDate(latestDate);

    ((MyViewHolder) holder).share.setTag(position);
    ((MyViewHolder) holder).star.setTag(position);

    String latestDateMonth = StringUtils.getMonth(latestDate);
    String latestDateDay = String.valueOf(latestDate.getDate());
    ((MyViewHolder) holder).display_month.setText(latestDateMonth);
    ((MyViewHolder) holder).display_date.setText(latestDateDay);

    if(!isWorkForFavourite) {
        if (staredApi != null) {
            if (isEventInFavourite(api.results.get(position))) {
                ((MyViewHolder) 
     holder).star.setImageResource(R.drawable.star_pressed);
            } else {
                ((MyViewHolder) 
     holder).star.setImageResource(R.drawable.star);
            }
        } else {
            ((MyViewHolder) holder).star.setImageResource(R.drawable.star);
        }
    }else{

      ((MyViewHolder)holder).star.setImageResource(R.drawable.star_pressed);
    } 
 if (api.results.get(position).image_url != null && api.results.get(position).image_url.length() > 0) {
        Picasso.with(MyApplication.getContext())
                .load(api.results.get(position).image_url)
                .fit()
                .centerCrop()
                .placeholder(R.drawable.preload_image)
                .error(R.drawable.imageunavailable)
                .into(((MyViewHolder) holder).event_image);
    } else {
        Picasso.with(MyApplication.getContext())
                .load("https://cdn.evbstatic.com/s3-build/perm_001/f8c5fa/django/images/discovery/default_logos/4.png")
                .fit()
                .centerCrop()
                .placeholder(R.drawable.preload_image)
                .error(R.drawable.imageunavailable)
                .into(((MyViewHolder) holder).event_image);
    }


}

1 个答案:

答案 0 :(得分:0)

使用onLongClickListener而不是onClickListener