当RecyclerView被点击时,父级点击事件未触发。

32

我有一个 RecyclerView,它位于一个 CardView 中,并包含了几个 TextView。CardView 设置了一个 OnClickListener,在点击 TextView 时触发,但在点击 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被点击时触发父视图的点击事件(长按事件也是如此)。同时,我还需要RecyclerView可以滚动。RecyclerView是否会阻止点击事件并且不向上传递给父视图呢?

5个回答

26

有一个更好的解决方案,那就是对你的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;
    }
}

15
recyclerView.setLayoutFrozen(true);

在设置RecyclerView的适配器后,只需将setLayoutFrozen设置为true即可。


8
这种方法可以防止RecyclerView在某些情况下被更新,从而使适配器的onBindViewHolder方法不会被调用。 - peppered
3
这会禁用滚动事件。 - Nigam Patro
当回收时,不会刷新项目布局。不应在包含另一个RecyclerView的列表中使用此功能。 - thirtyyuan
9
setLayoutFrozen 已经被弃用了,如果你只需要在 Recycler 中显示数据而不需要其他交互(如滚动、点击等),则应使用 suppressLayout(true) - bko

12

在我的案例中,我有一个 CardView,里面放了几个按钮和一个 RecyclerView。根据 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;
    }
}

谢谢你的回答George。默认情况下,当布局没有被冻结时,Recyclerview-OnTouchevents会返回true,这意味着它会处理。在这种情况下,触摸事件将完全被禁用。 - Mallikarjungouda Annigeri
完美解决方案!! - Pratik Tank
这将禁用滚动事件。 - icarumbas

2

我找到了获取RecyclerView父视图的点击事件的方法。我的解决方案有点像是一个hack,所以我希望有人能想出更好的解决方案。

在RecyclerView.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上挂钩点击事件:

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();
    }
});

这个可以运行,但是没有显示ViewGroup的点击效果。 - android developer

2

@ywwynm,您的想法是正确的,但是该解决方案不能允许嵌套的RecyclerView滚动。我将其与此处的解决方案相结合,提出了此解决方案,以处理单击和长按事件,并允许滚动。

public class InterceptTouchCardView extends CardView {
    private GestureDetector mGestureDetector;
    private boolean mLongClicked;

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

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

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

    private void Initialize() {
        mGestureDetector = new GestureDetector(getContext(),
                new GestureDetector.SimpleOnGestureListener() {
                    public boolean onDown(MotionEvent e) {
                        mLongClicked = false;
                        return true;
                    }

                    public void onLongPress(MotionEvent e) {
                        mLongClicked = true;
                        performLongClick();
                    }
                });
    }

    /**
     * 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.
     *
     * We can't Intercept the touch event if we want to allow scrolling since ACTION_DOWN always
     * happens before ACTION_MOVE.  So handle touch events here since onTouchEvent won't be triggered.
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        if (ev.getAction() == MotionEvent.ACTION_UP && !mLongClicked)
            this.callOnClick();
        return false;
    }
}

这种方法的缺点是 CardView 的选择器将不再使用,因此任何 CardView 的反馈都不会显示出来。 - Ionut Negru
它还会消耗应该用于RecyclerView项目的点击。 - David Corsalini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接