自定义带下拉刷新的ListView

下拉刷新现在那个app上基本都有了.
而实现下拉刷新也有很多第三方的,用起来也非常方便!
那能不能自己也写一个呢,学习了学习,现在来写一个吧!

先上效果图:

1.首先自定义类继承ListView 并实现构造方法,在每个构造方法中初始化initView();
并添加header并隐藏header的布局

用于添加头部布局

private void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        header = layoutInflater.inflate(R.layout.header_listview, null);//加载头部布局
        measureView(header);//测量头部布局的宽高
        headerHeight = header.getMeasuredHeight();//得到头部布局的高度
        topPadding(-headerHeight);//默认头部布局隐藏(利用padding的方式)
        this.addHeaderView(header);
        this.setOnScrollListener(this);
    }

对于要测量头部布局的原因:如果在这里直接获取header的高度的话,那么得到的值是0.所以要通知父布局,占用的宽,高,这样就可以得到header的高度了!

利用padding把header放到了屏幕外,实现隐藏的效果

measureView(header)的代码如下

private void measureView(View view) {
        ViewGroup.LayoutParams p = view.getLayoutParams();
        if(p == null){
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int width = ViewGroup.getChildMeasureSpec(0,0,p.width);//左右的边距,内边距,
        int height ;
        int tempHeight = p.height;
        if(tempHeight >0){
            height = MeasureSpec.makeMeasureSpec(tempHeight,MeasureSpec.EXACTLY);
        }else{
            height = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        }
        view.measure(width, height);
    }

topPadding(-headerHeight)的代码如下:

private void topPadding(int topPadding) {
        header.setPadding(header.getPaddingLeft(),topPadding,header.getPaddingRight(),header.getPaddingBottom());
        header.invalidate();
    }

这样就能实现隐藏了!

2.实现下拉的时候header随着手势出现在屏幕中

要检测手势的滑动,那么就重写onTouchEvent(MotionEvent ev)方法
在down的时候判断是否listview滑动到了顶部,如果已经到了顶部,那么就记录此时的startY值
在move的时候随时根据滑动的位置来改变状态,从而实现header随手势出现
在up的时候判断当前的状态实现相应的动作

代码如下:

public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(firstVisibleItem == 0){
                    isRemark  = true;
                    startY = (int)ev.getY();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                onMove(ev);
                break;
            case MotionEvent.ACTION_UP:
                if (state == RELESE) {
                    state = REFRESHING;
                    // 加载最新数据;
                    reflashViewByState();
                    if(iReflashListener != null){
                        iReflashListener.onReflash();
                    }
                } else if (state == PULL) {
                    state = NONE;
                    isRemark = false;
                    reflashViewByState();
                }
                break;
        }

        return super.onTouchEvent(ev);
    }
private void onMove(MotionEvent ev) {
        if (!isRemark) {
            return;
        }
        int tempY = (int) ev.getY();
        int space = tempY - startY;
        int topPadding = space - headerHeight;
        switch (state) {
            case NONE:
                if (space > 0) {
                    state = PULL;
                    reflashViewByState();
                }
                break;
            case PULL:
                topPadding(topPadding);
                if (space > headerHeight + 30 && scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    state = RELESE;
                    reflashViewByState();
                }
                break;
            case RELESE:
                topPadding(topPadding);
                if (space < headerHeight + 30) {
                    state = PULL;
                    reflashViewByState();
                } else if (space <= 0) {
                    state = NONE;
                    isRemark = false;
                    reflashViewByState();
                }
                break;
        }
    }
private void reflashViewByState() {
        TextView tip = (TextView) header.findViewById(R.id.header_text);
        ImageView arrow = (ImageView) header.findViewById(R.id.header_pic);
        ProgressBar progress = (ProgressBar) header.findViewById(R.id.progress);
        RotateAnimation anim = new RotateAnimation(0, 180,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        anim.setDuration(500);
        anim.setFillAfter(true);
        RotateAnimation anim1 = new RotateAnimation(180, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        anim1.setDuration(500);
        anim1.setFillAfter(true);


        switch (state){
            case NONE:
                arrow.clearAnimation();
                topPadding(-headerHeight);
                break;
            case PULL:
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("下拉可以刷新!");
                arrow.clearAnimation();
                arrow.setAnimation(anim1);
                break;
            case RELESE:
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("松开可以刷新!");
                arrow.clearAnimation();
                arrow.setAnimation(anim);
                break;
            case REFRESHING:
                topPadding(50);
                arrow.setVisibility(View.GONE);
                progress.setVisibility(View.VISIBLE);
                tip.setText("正在刷新...");
                arrow.clearAnimation();
                break;
        }
    }

3.前面基本也就实现的差不多了.最后呢,就要自定义接口,让activity实现,从而能实现下拉刷新的时候,做一些任务或者动作,代码就不上传了!

如下你还想实现自定义的加载更多,那么原理同上

接下来就是源码了!
点击下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值