【问题标题】:Enable click items, even when scrolling RecyclerView启用点击项,即使在滚动 RecyclerView 时也是如此
【发布时间】:2018-06-24 11:31:21
【问题描述】:

正常行为:当我们投掷滚动回收器视图时,项目将滚动,然后点击,RV 将停止滚动,不执行项目点击。

我需要什么:停止滚动+执行项目点击!

任何hacky方式?

【问题讨论】:

    标签: android android-recyclerview touch android-touch-event


    【解决方案1】:

    我从这里得到了答案 link

    我想添加更多代码...

    public class MyRecyclerView extends RecyclerView {
    public MyRecyclerView(Context context) {
        super(context);
    }
    
    public MyRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
    
        //https://stackoverflow.com/a/46569656/8863321
    
        boolean requestCancelDisallowInterceptTouchEvent = getScrollState() == SCROLL_STATE_SETTLING;
        boolean consumed = super.onInterceptTouchEvent(e);
        final int action = e.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;
    

    } }

    这是非常罕见的情况,但这个技巧将来可能会对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多