HorizontalInterruptView 实现监听水平滑动事件

11 篇文章 0 订阅
package com.tigo.tankemao.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * @author huangxiaohui
 * @date 2021/2/5
 */
public class HorizontalInterruptView extends LinearLayout {
    public static final String TAG = "HorizontalInterruptView";
    private List<View> changePaddingHorizontalViews = null;
    private float lastMoveX = 0f;
    private float currentMarginLeft = 0;
    private float minMargin = 0;
    private Scroller scroller;
    private VelocityTracker vt;
    public HorizontalInterruptView(Context context) {
        super(context);
        init();
    }

    public HorizontalInterruptView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        scroller = new Scroller(getContext());
        vt = VelocityTracker.obtain();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean isInterrupt = false;
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                if(!scroller.isFinished())
                {
                    scroller.abortAnimation();
                }
                lastMoveX = ev.getX();
                minMargin = (getWidth() - getInterrputViewsMaxWidth());
                vt.clear();
                vt.addMovement(ev);
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
                int currentDx = (int)Math.abs(ev.getX() - lastMoveX);
                if(currentDx >= scrollSlop * 2)
                {
                    isInterrupt = true;
                }
                break;
        }
        if(isInterrupt && minMargin < 0)
        {
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }

    private int getInterrputViewsMaxWidth() {
        if(changePaddingHorizontalViews == null)
        {
            return 0;
        }
        int maxWidth = 0;
        for (View changePaddingHorizontalView : changePaddingHorizontalViews) {
            maxWidth = Math.max(maxWidth, changePaddingHorizontalView.getWidth());
        }
        return maxWidth;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        vt.addMovement(ev);
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_MOVE:
                if(changePaddingHorizontalViews != null)
                {
                    for (View view : changePaddingHorizontalViews) {
                        ViewGroup.LayoutParams lp = view.getLayoutParams();
                        if(lp instanceof MarginLayoutParams)
                        {
                            float dx = ev.getX() - lastMoveX;
                            float temp = currentMarginLeft + dx;

                            if(temp > 0)
                            {
                                temp = 0;
                            }else if(temp < minMargin)
                            {
                                temp = minMargin;
                            }

                            MarginLayoutParams mlp = (MarginLayoutParams) lp;
                            currentMarginLeft = temp;
                            mlp.leftMargin = (int) currentMarginLeft;
                            view.requestLayout();
                            lastMoveX = ev.getX();
                        }
                    }
                }

                break;

            case MotionEvent.ACTION_UP:
                vt.computeCurrentVelocity(1000);//1000ms
                float velocity = vt.getXVelocity();//获取每1000ms移动的像素
                if(Math.abs(velocity) > ViewConfiguration.get(getContext()).getScaledMinimumFlingVelocity())
                {
                    scroller.fling((int)currentMarginLeft, 0, (int)vt.getXVelocity(), 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
                    postInvalidate();
                }
                break;
        }
        return true;
    }

    public void setChangePaddingHorizontalViews(List<View> changePaddingHorizontalViews) {
        this.changePaddingHorizontalViews = changePaddingHorizontalViews;
        if(changePaddingHorizontalViews == null)
        {
            return;
        }
        for (View view : changePaddingHorizontalViews) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if(lp instanceof MarginLayoutParams)
            {
                MarginLayoutParams mlp = (MarginLayoutParams) lp;
                mlp.leftMargin = (int) currentMarginLeft;
                view.requestLayout();
            }
        }
    }

    @Override
    public void computeScroll() {
        if(scroller.computeScrollOffset())
        {
            boolean isAbort = false;
            if(changePaddingHorizontalViews != null) {
                for (View view : changePaddingHorizontalViews) {
                    ViewGroup.LayoutParams lp = view.getLayoutParams();
                    if (lp instanceof MarginLayoutParams) {
                        MarginLayoutParams mlp = (MarginLayoutParams) lp;
                        int temp = scroller.getCurrX();
                        if(temp > 0)
                        {
                            temp = 0;
                            isAbort = true;
                        }else if(temp < minMargin)
                        {
                            temp = (int)minMargin;
                            isAbort = true;
                        }
                        currentMarginLeft = temp;
                        mlp.leftMargin = (int) currentMarginLeft;
                        view.requestLayout();
                    }
                }
            }

            if(isAbort)
            {
                scroller.abortAnimation();
            }else{
                postInvalidate();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值