Android NestScrollView 监听滑动

package com.as.apprehendschool.customviews.scrollview;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ObserveAlphaScrollView extends NestedScrollView {

    AlphaChangeListener alphaChangeListener;

    private float xLast, yLast, xDistance, yDistance;

    // ScrollView的子View, 也是ScrollView的唯一一个子View
    private View contentView;

    // 用于记录正常的布局位置
    private Rect originalRect = new Rect();
    //最顶部的文字 颜色变化
    private int observeHeight;

    //新增的底部按钮图片变化 高度监听
    private int observeBoottomHeight;


    public ObserveAlphaScrollView(Context context) {
        super(context);
    }

    public ObserveAlphaScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObserveAlphaScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        if (getChildCount() > 0) {
            contentView = getChildAt(0);
        }
        super.onFinishInflate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);


        if (contentView == null)
            return;

        // ScrollView中的唯一子控件的位置信息, 这个位置信息在整个控件的生命周期中保持不变
        originalRect.set(contentView.getLeft(), contentView.getTop(),
                contentView.getRight(), contentView.getBottom());
    }

    /**
     * 在这里解决滑动上下滑动,左右滑动冲突
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance) {
                    return false;   //表示向下传递事件
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    public interface AlphaChangeListener {
        void alphaChanging(float alpha, int height);
    }

    public void setObserveHeight(int observeHeight) {
        this.observeHeight = observeHeight;
    }

    public void setAlphaChangeListener(AlphaChangeListener alphaChangeListener) {
        this.alphaChangeListener = alphaChangeListener;
    }


    public interface BottomChangeListener {
        void alphaChanging(float alpha, int height);
    }

    public void setObserveBoottomHeight(int observeBoottomHeight) {
        this.observeBoottomHeight = observeBoottomHeight;
    }

    public BottomChangeListener bottomChangeListener;

    public void setBottomChangeListener(BottomChangeListener bottomChangeListener) {
        this.bottomChangeListener = bottomChangeListener;
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (alphaChangeListener != null) {

            if (t >= observeHeight) {
                alphaChangeListener.alphaChanging(1, t);
            } else {
                //透明度应该是0~1f
                float i = (float) t / (float) observeHeight;
                alphaChangeListener.alphaChanging(i, t);

            }
        }


        if (bottomChangeListener != null ) {

            if (t >= observeHeight) {
                bottomChangeListener.alphaChanging(1, t);
            } else {
                //透明度应该是0~1f
                float i = (float) t / (float) observeHeight;
                bottomChangeListener.alphaChanging(i, t);
            }
        }


    }



}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值