滚动查看时单击的项目 [英] RecyclerView items clicking while scrolling

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

问题描述

我有一个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.

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

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