获取listView上滑距离,根据这个距离改变标题栏颜色

1.继承ListView

2.重写onScrollchanged方法,在这个方法中调接口,在主页面实现接口

3.得到liostView的滑动距离,不断在onScrollchanged方法中调用

下面是代码

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
//    获取listView滑动距离改变标题栏颜色
        if (onScrollingTitleColor != null) {
            onScrollingTitleColor.onScrolling(getScroll());
        }
    }

    /**
     * 获得listView滑动距离
     *
     * @return
     */
    public int getScroll() {
        View c = this.getChildAt(0);
        if (c == null) {
            return 0;
        }
        int firstVisiblePosition = this.getFirstVisiblePosition();
        int top = c.getTop();
        return -top + firstVisiblePosition * c.getHeight();
    }

//下面是接口
/**
 * 上滑标题栏变色
 */
public interface OnScrollingTitleColor {
    void onScrolling(int position);
}

private OnScrollingTitleColor onScrollingTitleColor;

public void setOnScrollingChangeTitleColor(OnScrollingTitleColor onScrollingTitleColor) {
    this.onScrollingTitleColor = onScrollingTitleColor;
}

当我们想要实现listView顶部图片下拉放大时,下面这个类,绝对能帮到你

public class MyListView extends ListView {
    /**
     * 要放大的视图
     */
    private ImageView headerView;
    private int originHeight;
    private int maxHeight;

    /**
     * 向下滑动到释放的高度差
     */
    private float distance = 0;
    /**
     * 向下拉动要放大,手指向下滑时,点击的第一个点的Y坐标
     */
    private float startY = 0;
    /**
     * target height
     */
    private int newHeight = 0;
    /**
     * 保存位置
     */
    private int[] location = new int[2];
    /**
     * 得到headerView在屏幕上的距离
     */
    private int screenDistance = 0;

    private int mMaxYOverscrollDistance;

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

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

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

    public void setParallaxImageView(ImageView headerView) {
        if (headerView == null) {
            headerView = ifHeaderViewNull();
        }
        this.headerView = headerView;
        originHeight = headerView.getHeight();
        maxHeight = headerView.getDrawable().getIntrinsicHeight();
    }

    /**
     * 如果传过来的viewnull
     */
    public ImageView ifHeaderViewNull() {
        ImageView imageView = new ImageView(getContext());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
        imageView.setLayoutParams(params);
        imageView.setImageResource(R.mipmap.bg_dynamic_header);
        return imageView;
    }

    private void initBounceListView() {

        final DisplayMetrics metrics = getContext().getResources()
                .getDisplayMetrics();
        final float density = metrics.density;
        mMaxYOverscrollDistance = (int) (density * 200);
        try {
            Class<?> c = (Class<?>) Class.forName(AbsListView.class.getName());
            Field egtField = c.getDeclaredField("mEdgeGlowTop");
            Field egbBottom = c.getDeclaredField("mEdgeGlowBottom");
            egtField.setAccessible(true);
            egbBottom.setAccessible(true);
            Object egtObject = egtField.get(this); // this 指的是ListiVew实例
            Object egbObject = egbBottom.get(this);

            // egtObject.getClass() 实际上是一个 EdgeEffect 其中有两个重要属性 mGlow mEdge
            // 并且这两个属性都是Drawable类型
            Class<?> cc = (Class<?>) Class.forName(egtObject.getClass()
                    .getName());
            Field mGlow = cc.getDeclaredField("mGlow");
            mGlow.setAccessible(true);
            mGlow.set(egtObject, new ColorDrawable(Color.TRANSPARENT));
            mGlow.set(egbObject, new ColorDrawable(Color.TRANSPARENT));

            Field mEdge = cc.getDeclaredField("mEdge");
            mEdge.setAccessible(true);
            mEdge.set(egtObject, new ColorDrawable(Color.TRANSPARENT));
            mEdge.set(egbObject, new ColorDrawable(Color.TRANSPARENT));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 重写overScrollBy,能获取ListView下拉的距离
     * @param deltaX:横向的变化量
     * @param deltaY:纵向的变化量
     * @param scrollX:横向X的偏移量
     * @param scrollY:纵向Y的偏移量
     * @param scrollRangeX:横向X偏移范围
     * @param scrollRangeY:纵向Y的偏移范围
     * @param maxOverScrollX:横向X最大的偏移量
     * @param maxOverScrollY:纵向Y最大的偏移量
     * @param isTouchEvent:是否是触摸产生的滑动超出
     */
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        if (isTouchEvent && deltaY < 0) {

            newHeight = (int) (headerView.getHeight() + Math.abs(deltaY / 3.0f));
            newHeight = Math.min(newHeight, maxHeight);

            headerView.getLayoutParams().height = newHeight;
            headerView.requestLayout();
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }


        @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                startY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                distance = ev.getY() - startY;
                break;
            case MotionEvent.ACTION_UP:
                if (newHeight >= 330) {
                    ResetAnimation resetAnimation = new ResetAnimation(headerView, originHeight);
                    headerView.startAnimation(resetAnimation);
                    if (onRefreshScrollViewListener != null) {
                        onRefreshScrollViewListener.OnRefresh();
                        newHeight = 0;
                    }
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
//    获取listView滑动距离改变标题栏颜色
        if (onScrollingTitleColor != null) {
            onScrollingTitleColor.onScrolling(getScroll());
        }
    }

    /**
     * 获得listView滑动距离
     *
     * @return
     */
    public int getScroll() {
        View c = this.getChildAt(0);
        if (c == null) {
            return 0;
        }
        int firstVisiblePosition = this.getFirstVisiblePosition();
        int top = c.getTop();
        return -top + firstVisiblePosition * c.getHeight();
    }

    /**
     * @param onRefreshScrollViewListener
     * @author 燕潇洒
     * 设置下拉刷新
     */
    public void setOnRefreshScrollViewListener(OnRefreshScrollViewListener onRefreshScrollViewListener) {
        this.onRefreshScrollViewListener = onRefreshScrollViewListener;
    }

    public interface OnRefreshScrollViewListener {
        void OnRefresh();
    }

    private OnRefreshScrollViewListener onRefreshScrollViewListener;

    /**
     * 上滑标题栏变色
     */
    public interface OnScrollingTitleColor {
        void onScrolling(int position);
    }

    private OnScrollingTitleColor onScrollingTitleColor;

    public void setOnScrollingChangeTitleColor(OnScrollingTitleColor onScrollingTitleColor) {
        this.onScrollingTitleColor = onScrollingTitleColor;
    }
}
这是用到的工具类,实现变换的过程

public class ResetAnimation extends Animation {

    private int startHeight;
    private int tragetHeight;
    private ImageView imageView;

    public ResetAnimation(ImageView imageView, int tragetHeight) {
        this.imageView = imageView ;
        this.startHeight = imageView.getHeight();
        this.tragetHeight = tragetHeight;
        this.setDuration(500);
        setInterpolator(new OvershootInterpolator());
        System.out.println("构造函数被调用了");
    }

    /**
     *
     * @param interpolatedTime:从0.0 ————> 1.0的百分比
     * @param t
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        IntEvaluator intEvaluator = new IntEvaluator();
        Integer newHeight = intEvaluator.evaluate(interpolatedTime, startHeight, tragetHeight);
//        System.out.println(startHeight+" = " + tragetHeight);
        imageView.getLayoutParams().height = newHeight ;
        imageView.requestLayout();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值