RecyclerView处理鼠标滑轮滑动事件

RecyclerView处理鼠标滑轮滑动事件

项目进行中,遇到了RecyclerView的Item无法根据鼠标滑轮进行滑动问题,对于RecyclerView的滑动体验较差,因此撰写此博客分享RecyclerView鼠标滑轮事件解决方案。

  1. 重写RecyclerView的onGenericMotionEvent事件,针对事件中的ACTION_SCROLL进行处理
  2. 通过event.getAxisValue(MotionEvent.AXIS_VSCROLL)获取设备屏幕相对位置,通过RecyclerViewy的scrollBy方法滑动RecyclerView位置,代码如下:

    public class CustomRecyclerView extends RecyclerView {
            private float mVerticalScrollFactor = 20.f;
    
            public CustomRecyclerView(Context context) {
                super(context);
            }
    
            public CustomRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
    
             public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
                super(context, attrs);
            }
    
            @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
            @Override
                public boolean onGenericMotionEvent(MotionEvent event) {//鼠标滑轮滑动事件
                 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
                     if (event.getAction() == MotionEvent.ACTION_SCROLL &&
                          getScrollState() == SCROLL_STATE_IDLE) {//鼠标滑轮事件执行&&RecyclerView不是真正滑动
                         final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);//获取轴线距离
                        if (vscroll != 0) {
                         final int delta = -1 * (int) (vscroll * mVerticalScrollFactor);
                         if (ViewCompat.canScrollVertically(this, delta > 0 ? 1 : -1)) {
                          scrollBy(0, delta);
                          return true;
                }
            }
        }
    }
            return super.onGenericMotionEvent(event);
        }
    }
    

ListView嵌套ScrollView滑动事件冲突

项目进行中会遇到ScrollView嵌套ListView,ScrollView不可滑动情况,解决此问题的方法主要有:

  1. 重写ListView的onMeasure方法让ListView高度达到最大,实现根据Item自适应效果,代码如下:

    public class ListViewForScrollView extends ListView {
         public ListViewForScrollView(Context context) {
             super(context);
     }
    
        public ListViewForScrollView(Context context, AttributeSet attrs) {
             super(context, attrs);
     }
    
    public ListViewForScrollView(Context context, AttributeSet attrs,
                             int defStyle) {
        super(context, attrs, defStyle);
    }
    
     @Override
    /**
      * 重写该方法,达到使ListView适应ScrollView的效果
    */
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
            MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
     }
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值