单击recyclerview时,父级单击事件不会触发

时间:2015-05-24 03:51:36

标签: android android-cardview android-recyclerview

我有一个带有几个TextView的CardView中的RecyclerView。 CardView具有OnClickListener设置,在单击TextViews时会被触发,但在单击RecyclerView时不会触发。

以下是CardView的样子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100"
        android:minWidth="100dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/text1"
            android:textColor="@color/abc_primary_text_material_light"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:listSelector="@color/highlighted_text_material_light"
            android:layout_weight="98" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/black" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeSummary"
            android:orientation="horizontal"
            android:layout_weight="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/text2"
                android:textAlignment="viewEnd"
                android:textColor="@color/abc_secondary_text_material_light"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="start"
                android:singleLine="true"
                android:layout_alignParentLeft="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/text3"
                android:textColor="@color/abc_primary_text_material_light"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:gravity="end"
                android:singleLine="true"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/text2" />

        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

我不需要在这个RecyclerView上有一个点击监听器,而且当单击RecyclerView时,实际上只需要触发父视图的click事件(OnLongClick事件也是如此)。我还需要RecyclerView来滚动。 RecyclerView是关于如何吃点击事件而不是将其传递给父母的?

5 个答案:

答案 0 :(得分:14)

有一个更好的解决方案。也就是说,继承您的CardView

public class InterceptTouchCardView extends CardView {

    public InterceptTouchCardView(Context context) {
        super(context);
    }

    public InterceptTouchCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InterceptTouchCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Intercept touch event so that inner views cannot receive it.
     * 
     * If a ViewGroup contains a RecyclerView and has an OnTouchListener or something like that,
     * touch events will be directly delivered to inner RecyclerView and handled by it. As a result, 
     * parent ViewGroup won't receive the touch event any longer.
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

答案 1 :(得分:8)

recyclerView.setLayoutFrozen(true);

在setAdapter for recyclerView

之后,setLayoutFrozen为true

答案 2 :(得分:5)

就我而言,我有一个带有几个按钮和RecyclerView的CardView。使用ywwynmDaryl的解决方案,问题是CardView将拦截来自其所有子视图的事件,包括按钮。但我想要的是CardView只拦截RecyclerView的触摸事件。我的解决方案如下:

public class UntouchableRecyclerView extends RecyclerView {

    public UntouchableRecyclerView(Context context) {
        super(context);
    }

    public UntouchableRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UntouchableRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return false;
    }
}

答案 3 :(得分:2)

我想出了如何将click事件发送到RecyclerView的父级。我的解决方案有点像黑客,所以我希望有人能提出更好的解决方案。

在RecylerView.Adapter中:

@Override
public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.my_item_layout, viewGroup, false);

    ViewHolder viewHolder = new ViewHolder(view);

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewGroup.callOnClick();
        }
    });
    viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return viewGroup.performLongClick();
        }
    });

    return viewHolder;
}

然后,我必须在RecyclerView上连接click事件:

RecyclerView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        parentView.callOnClick();
    }
});
RecyclerView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return parentView.performLongClick();
    }
});

答案 4 :(得分:0)

@ywwynm,除了解决方案不允许嵌套的RecyclerView滚动之外,你是在正确的轨道上。我将它与解决方案here结合起来,并提出了这个解决方案来处理click和onLongClick事件以及允许滚动。

"filter" : {
  "query" : {
    "bool" : {
      "must" : {
        "bool" : {
          "should" : {
            "match" : {
              "type" : {
                "query" : 2,
                "type" : "boolean",
                "operator" : "AND"
              }
            }
          }
        }
      }
    }
 }