RecyclerView 项目在滚动时点击 [英] RecyclerView items clicking while scrolling

查看:50
本文介绍了RecyclerView 项目在滚动时点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RecyclerView.在其适配器的 onBindViewHolder() 中,我将 OnClickListener 设置为 ViewHolder 的根布局.

I have a RecyclerView. Inside its adapter's onBindViewHolder() I set the OnClickListener to the ViewHolder's root layout.

但是,我最近注意到在滚动时无法点击 RecyclerView 的项目.只有在它停止之后.如果我在滚动时单击 RecyclerView 上的某个位置,RecyclerView 只会停在该位置,不会单击任何项​​目.

However, I've recently noticed that I can't click on the RecyclerView's items while it's being scrolled. Only after it's stopped. If I click somewhere on the RecyclerView while I'm scrolling it, the RecyclerView only stops at that position, no items are clicked.

您可以在 Gmail 应用中了解我的意思.滚动其 RecyclerView 时无法打开任何电子邮件.

You can see what I mean inside the Gmail app. You can't open any email while you are scrolling its RecyclerView.

但是,Google Play 应用的行为有所不同.您可以在滚动应用程序列表的同时打开应用程序页面.您甚至可以在滚动父 RecyclerView 的同时水平滚动内部 RecyclerView.

However, the Google Play app behaves differently. You can open app pages while scrolling the lists of apps. You can even scroll inner RecyclerViews horizontally while the parent RecyclerView is being scrolled.

我还注意到 ScrollView 的相同行为.例如,如果您在其中放置了很多按钮并开始滚动它,则不会触发按钮侦听器.只有在 ScrollView 完全停止后才能触发它们.

Also I've noticed the same behaviour for a ScrollView. For example, if you put a lot of buttons inside it and begin to scroll it, the button listeners are not triggered. They can be triggered only after the ScrollView is stopped completely.

您知道如何实现在 Google Play 应用中的行为吗?谢谢!

Do you have any idea how it's possible to achieve the behaviour like in the Google Play app? Thank you!

推荐答案

这是 Kimi Chiu 在一个相关问题:

Here's a code written by Kimi Chiu in a related question:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean requestCancelDisallowInterceptTouchEvent = getScrollState() == SCROLL_STATE_SETTLING;
    boolean consumed = super.onInterceptTouchEvent(event);
    final int action = event.getActionMasked();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if( requestCancelDisallowInterceptTouchEvent ){
                getParent().requestDisallowInterceptTouchEvent(false);
                // stop scroll to enable child view get the touch event
                stopScroll();
                // not consume the event
                return false;
            }
            break;
    }

    return consumed;
}

如果您子类化 RecyclerView 类并覆盖此方法,它的行为将与问题相同.

If you subclass the RecyclerView class and override this method, it will behave as in the question.

这篇关于RecyclerView 项目在滚动时点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆