修复上次发布《自定义回弹的ScrollView》最后写道的小Bug

一、效果图

二、具体修改的代码

判断是否需要回弹的代码修改前后对比(点击查看 修改出处

    /**
     * 判断是否需要弹性
     *
     * @return
     */
//    public boolean isNeedMove() {
//        int offset = childView.getMeasuredHeight() - getHeight();// 到底
//        int scroll = getScrollY();// 到顶
//        return scroll == 0 || offset == scroll;
//    }

    /**
     * 判断是顶部回弹还是底部回弹
     *
     * @return 0-顶部 1-底部 2-中间
     */
    public int moveLocation() {
        int offset = childView.getMeasuredHeight() - getHeight();
        int scroll = getScrollY();
        if (scroll == 0) return 0;// 到顶
        if (offset == scroll) return 1;// 到底
        return 2;
    }

滑动逻辑部分的修改前后对比

case MotionEvent.ACTION_MOVE:
                if (moveLocation() == 0) {// 顶部回弹(底部回弹类似写法)
                    int distance = 0;
                    float nowY = ev.getY();
                    if (nowY - downY > 0) {// 判断是否是手指向下滑动
                        isCount = true;// 开启回弹
                    } else {
                        isCount = false;// 关闭回弹
                    }
                    if (nowY - y > 0 && isCount) {// 手指向下滑动
                        distance = 10;// 每次调用偏移的距离
                    }

                    if (nowY - y < 0 && isCount) {// 手指向上滑动
                        distance = -10;
                    }
                    y = nowY;
                    if (childView.getTop() < 500) {// 不能超过500
                        childView.layout(childView.getLeft(), childView.getTop() + distance, childView.getRight(), childView.getBottom() + distance);
                    }

                    if (isCount) {// 消耗事件
                        return true;
                    } else {// 交由父类
                        return super.onTouchEvent(ev);
                    }

                }

//                if (isNeedMove() || isCount) {
//                    float nowY = ev.getY();
//                    // 现在的位置和上次位置的差
//                    int distanceY = (int) (nowY - y);
//
//                    if (!isCount) {// 保证每次从0开始,因为这里的startY是全局变量 有点时候第一次 nowY - startY<0 有的时候>>0
//                        distanceY = 0; // 在这里要归0.
//                    }
//                    y = nowY;
//                    // 从新摆放子View的位置
//                    Log.d("aaa", "distanceY = " + distanceY + "---childView.getTop()=" + childView.getTop());
//
//                    childView.layout(childView.getLeft(), childView.getTop() + distanceY / 3,
//                            childView.getRight(), childView.getBottom() + distanceY / 3);
//                    isCount = true;
//                }
                break;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值