欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android笔记之:App列表之下拉刷新的使用

程序员文章站 2023-12-10 22:20:34
android的listview是应用最广的一个组件,功能强大,扩展性灵活(不局限于listview本身一个类),前面的文章有介绍分组,拖拽,3d立体,游标,圆角,而今天我...
android的listview是应用最广的一个组件,功能强大,扩展性灵活(不局限于listview本身一个类),前面的文章有介绍分组,拖拽,3d立体,游标,圆角,而今天我们要介绍的是另外一个扩展listview:下拉刷新的listview。
    下拉刷新界面最初流行于iphone应用界面,如图:

Android笔记之:App列表之下拉刷新的使用
    然后在android中也逐渐被应用,比如微博,资讯类。
    所以,今天要实现的结果应该也是类似的,先贴出最终完成效果,如下图,接下来我们一步一步实现。

Android笔记之:App列表之下拉刷新的使用

 

1. 流程分析
    下拉刷新最主要的流程是:
    (1). 下拉,显示提示头部界面(headerview),这个过程提示用户"下拉刷新"
    (2). 下拉到一定程度,超出了刷新最基本的下拉界限,我们认为达到了刷新的条件,提示用户可以"松手刷新"了,效果上允许用户继续下拉
    (3). 用户松手,可能用户下拉远远不止提示头部界面,所以这一步,先反弹回仅显示提示头部界面,然后提示用户"正在加载"。
    (4). 加载完成后,隐藏提示头部界面。
    示意图如下:

Android笔记之:App列表之下拉刷新的使用->Android笔记之:App列表之下拉刷新的使用->Android笔记之:App列表之下拉刷新的使用

2. 实现分析
    当前我们要实现上述流程,是基于listview的,所以对应listview本身的功能我们来分析一下实现原理:
    (1). 下拉,显示提示头部界面,这个过程提示用户"下拉刷新"
        a. 下拉的操作,首先是监听滚动,listview提供了onscroll()方法
        b. 与下拉类似一个动作向下飞滑,所以listview的scrollstate有3种值:scroll_state_idle, scroll_state_touch_scroll, scroll_state_fling,意思容易理解,而我们要下拉的触发条件是scroll_state_touch_scroll。判断当前的下拉操作状态,listview提供了public void onscrollstatechanged(abslistview view, int scrollstate) {}。
    c. 下拉的过程中,我们可能还需要下拉到多少的边界值处理,重写ontouchevent(motionevent ev){}方法,可依据action_down,action_move,action_up实现更精细的判断。
    (2). 下拉到一定程度,超出了刷新最基本的下拉界限,我们认为达到了刷新的条件,提示用户可以"松手刷新"了,效果上允许用户继续下拉
        a. 达到下拉刷新界限,一般指达到header的高度的,所以有两步,第一,获取header的高度,第二,当header.getbottom()>=header的高度时,我们认为就达到了刷新界限值
        b. 继续允许用户下拉,当header完全下拉后,默认无法继续下拉,但是可以增加header的paddingtop实现这种效果
    (3). 用户松手,可能用户下拉远远不止提示头部界面,所以这一步,先反弹回仅显示提示头部界面,然后提示用户"正在加载"。
        a. 松手后反弹,这个不能一下子弹回去,看上去太突然,需要一步一步柔性的弹回去,像弹簧一样,我们可以new一个thread循环计算减少paddingtop,直到paddingtop为0,反弹结束。
        b. 正在加载,在子线程里处理后台任务
    (4). 加载完成后,隐藏提示头部界面。
        a. 后台任务完成后,我们需要隐藏header,setselection(1)即实现了从第2项开始显示,间接隐藏了header。
上面我们分析了实现过程的轮廓,接下来,通过细节说明和代码具体实现。

3. 初始化
    一切状态显示都是用headerview显示的,所以我们需要一个headerview的layout,使用addheaderview方法添加到listview中。
    同时,默认状态下,headerview是不显示的,只是在下拉后才显示,所以我们需要隐藏headerview且不影响后续的下拉显示,用setselection(1)。
    refresh_list_header.xml布局如下:

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">
    <progressbar android:id="@+id/refresh_list_header_progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        style="?android:attr/progressbarstylesmall"
        android:visibility="gone">
    </progressbar>
    <imageview android:id="@+id/refresh_list_header_pull_down"
        android:layout_width="9dip"
        android:layout_height="25dip"
        android:layout_gravity="center"
        android:src="@drawable/refresh_list_pull_down" />
    <imageview android:id="@+id/refresh_list_header_release_up"
        android:layout_width="9dip"
        android:layout_height="25dip"
        android:layout_gravity="center"
        android:src="@drawable/refresh_list_release_up"
        android:visibility="gone" />
    <relativelayout android:layout_width="180dip"
        android:layout_height="wrap_content">
        <textview android:id="@+id/refresh_list_header_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignparenttop="true"
            android:textsize="12dip"
            android:textcolor="#192f06"
            android:paddingtop="8dip"
            android:text="@string/app_list_header_refresh_down"/>
        <textview android:id="@+id/refresh_list_header_last_update"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_below="@id/refresh_list_header_text"
            android:textsize="12dip"
            android:textcolor="#192f06"
            android:paddingbottom="8dip"
            android:text="@string/app_list_header_refresh_last_update"/>
    </relativelayout>
</linearlayout>   

代码中在构造函数中添加init()方法加载如下:
复制代码 代码如下:

private linearlayout mheaderlinearlayout = null;
private textview mheadertextview = null;
private textview mheaderupdatetext = null;
private imageview mheaderpulldownimageview = null;
private imageview mheaderreleasedownimageview = null;
private progressbar mheaderprogressbar = null;

public refreshlistview(context context) {
    this(context, null);
}
public refreshlistview(context context, attributeset attrs) {
    super(context, attrs);
    init(context);
}

void init(final context context) {
    mheaderlinearlayout = (linearlayout) layoutinflater.from(context).inflate(r.layout.refresh_list_header, null);
    addheaderview(mheaderlinearlayout);
    mheadertextview = (textview) findviewbyid(r.id.refresh_list_header_text);
    mheaderupdatetext = (textview) findviewbyid(r.id.refresh_list_header_last_update);
    mheaderpulldownimageview = (imageview) findviewbyid(r.id.refresh_list_header_pull_down);
    mheaderreleasedownimageview = (imageview) findviewbyid(r.id.refresh_list_header_release_up);
    mheaderprogressbar = (progressbar) findviewbyid(r.id.refresh_list_header_progressbar);

    setselection(1);
}

   默认就显示完成了。

4. headerview的默认高度测量
    因为下拉到headerview全部显示出来,就由提示"下拉刷新"变为"松手刷新",全部显示的出来的测量标准就是header.getbottom()>=header的高度。
    所以,首先我们需要测量headerview的默认高度。

复制代码 代码如下:

//因为是在构造函数里测量高度,应该先measure一下
private void measureview(view child) {
    viewgroup.layoutparams p = child.getlayoutparams();
    if (p == null) {
        p = new viewgroup.layoutparams(viewgroup.layoutparams.fill_parent,
                viewgroup.layoutparams.wrap_content);
    }

    int childwidthspec = viewgroup.getchildmeasurespec(0, 0 + 0, p.width);
    int lpheight = p.height;
    int childheightspec;
    if (lpheight > 0) {
        childheightspec = measurespec.makemeasurespec(lpheight,
                measurespec.exactly);
    } else {
        childheightspec = measurespec.makemeasurespec(0,
                measurespec.unspecified);
    }
    child.measure(childwidthspec, childheightspec);
}

然后在init的上述代码后面加上调用measureview后,使用getmeasureheight()方法获取header的高度:
复制代码 代码如下:

private int mheaderheight;
void init(final context context) {
    ... ...
    measureview(mheaderlinearlayout);
    mheaderheight = mheaderlinearlayout.getmeasuredheight();
}

  后面我们就会用到这个mheaderheight.

5. scrollstate监听记录
    scrollstate有3种,使用onscrollstatechanged()方法监听记录。

复制代码 代码如下:

private int mcurrentscrollstate;
@override
public void onscrollstatechanged(abslistview view, int scrollstate) {
    mcurrentscrollstate = scrollstate;
}

    然后即可使用mcurrentscrollstate作为后面判断的条件了。

6. 刷新状态分析
    因为一些地方需要知道我们处在正常状态下还是进入下拉刷新状态还是松手反弹状态,比如,
    (1). 在非正常的状态下,我们不小心飞滑了一下(松手的瞬间容易出现这种情况),我们不能setselection(1)的,否则总是松手后header跳的一下消失掉了。
    (2). 下拉后要做一个下拉效果的特殊处理,需要用到over_pull_refresh(松手刷新状态下)
    (3). 松手反弹后要做一个反弹效果的特殊处理,需要用到over_pull_refresh和enter_pull_refresh。

复制代码 代码如下:

private final static int none_pull_refresh = 0;   //正常状态
private final static int enter_pull_refresh = 1;  //进入下拉刷新状态
private final static int over_pull_refresh = 2;   //进入松手刷新状态
private final static int exit_pull_refresh = 3;     //松手后反弹后加载状态
private int mpullrefreshstate = 0;                         //记录刷新状态
@override
public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
    if (mcurrentscrollstate ==scroll_state_touch_scroll
            && firstvisibleitem == 0
            && (mheaderlinearlayout.getbottom() >= 0 && mheaderlinearlayout.getbottom() < mheaderheight)) {
        //进入且仅进入下拉刷新状态
        if (mpullrefreshstate == none_pull_refresh) {
            mpullrefreshstate = enter_pull_refresh;
        }
    } else if (mcurrentscrollstate ==scroll_state_touch_scroll
            && firstvisibleitem == 0
            && (mheaderlinearlayout.getbottom() >= mheaderheight)) {
        //下拉达到界限,进入松手刷新状态
        if (mpullrefreshstate == enter_pull_refresh || mpullrefreshstate == none_pull_refresh) {
            mpullrefreshstate = over_pull_refresh;
            //下面是进入松手刷新状态需要做的一个显示改变
            mdowny = mmovey;//用于后面的下拉特殊效果
            mheadertextview.settext("松手刷新");
            mheaderpulldownimageview.setvisibility(view.gone);
            mheaderreleasedownimageview.setvisibility(view.visible);
        }
    } else if (mcurrentscrollstate ==scroll_state_touch_scroll && firstvisibleitem != 0) {
        //不刷新了
        if (mpullrefreshstate == enter_pull_refresh) {
            mpullrefreshstate = none_pull_refresh;
        }
    } else if (mcurrentscrollstate == scroll_state_fling && firstvisibleitem == 0) {
        //飞滑状态,不能显示出header,也不能影响正常的飞滑
        //只在正常情况下才纠正位置
        if (mpullrefreshstate == none_pull_refresh) {
            setselection(1);
        }
    }
}

  mpullrefreshstate将是后面我们处理边界的重要变量。

6. 下拉效果的特殊处理
    所谓的特殊处理,当header完全显示后,下拉只按下拉1/3的距离下拉,给用户一种艰难下拉,该松手的弹簧感觉。
    这个在ontouchevent里处理比较方便:

复制代码 代码如下:

private float mdowny;
private float mmovey;
@override
public boolean ontouchevent(motionevent ev) {
    switch (ev.getaction()) {
        case motionevent.action_down:
            //记下按下位置
            //改变
            mdowny = ev.gety();
            break;
        case motionevent.action_move:
            //移动时手指的位置
            mmovey = ev.gety();
            if (mpullrefreshstate == over_pull_refresh) {
                //注意下面的mdowny在onscroll的第二个else中被改变了
                mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                        (int)((mmovey - mdowny)/3), //1/3距离折扣
                        mheaderlinearlayout.getpaddingright(),
                        mheaderlinearlayout.getpaddingbottom());
            }
            break;
        case motionevent.action_up:
            ... ...
            break;
    }
    return super.ontouchevent(ev);
}

//重复贴出下面这段需要注意的代码
@override
public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
    ... ...
    else if (mcurrentscrollstate == scroll_state_touch_scroll
            && firstvisibleitem == 0
            && (mheaderlinearlayout.getbottom() >= mheaderheight)) {
        //下拉达到界限,进入松手刷新状态
        if (mpullrefreshstate == enter_pull_refresh || mpullrefreshstate == none_pull_refresh) {
            mpullrefreshstate = over_pull_refresh;
            mdowny = mmovey; //为下拉1/3折扣效果记录开始位置
            mheadertextview.settext("松手刷新");//显示松手刷新
            mheaderpulldownimageview.setvisibility(view.gone);//隐藏"下拉刷新"
            mheaderreleasedownimageview.setvisibility(view.visible);//显示向上的箭头
        }
    }
    ... ...
}

  onscroll里监听到了进入松手刷新状态,ontouchevent就开始在action_move中处理1/3折扣问题。

7. 反弹效果的特殊处理
    松手后我们需要一个柔性的反弹效果,意味着我们弹回去的过程需要分一步步走,我的解决方案是:
    在子线程里计算paddingtop,并减少到原来的3/4,循环通知主线程,直到paddingtop小于1(这个值取一个小值,合适即可)。
    松手后,当然是在ontouchevent的action_up条件下处理比较方便:

复制代码 代码如下:

//因为涉及到handler数据处理,为方便我们定义如下常量
private final static int refresh_backing = 0;      //反弹中
private final static int refresh_baced = 1;        //达到刷新界限,反弹结束后
private final static int refresh_return = 2;       //没有达到刷新界限,返回
private final static int refresh_done = 3;         //加载数据结束

@override
public boolean ontouchevent(motionevent ev) {
    switch (ev.getaction()) {
        ... ...
        case motionevent.action_up:
            //when you action up, it will do these:
            //1. roll back util header toppadding is 0
            //2. hide the header by setselection(1)
            if (mpullrefreshstate == over_pull_refresh || mpullrefreshstate == enter_pull_refresh) {
                new thread() {
                    public void run() {
                        message msg;
                        while(mheaderlinearlayout.getpaddingtop() > 1) {
                            msg = mhandler.obtainmessage();
                            msg.what = refresh_backing;
                            mhandler.sendmessage(msg);
                            try {
                                sleep(5);//慢一点反弹,别一下子就弹回去了
                            } catch (interruptedexception e) {
                                e.printstacktrace();
                            }
                        }
                        msg = mhandler.obtainmessage();
                        if (mpullrefreshstate == over_pull_refresh) {
                            msg.what = refresh_baced;//加载数据完成,结束返回
                        } else {
                            msg.what = refresh_return;//未达到刷新界限,直接返回
                        }
                        mhandler.sendmessage(msg);
                    };
                }.start();
            }
            break;
    }
    return super.ontouchevent(ev);
}

private handler mhandler = new handler(){
    @override
    public void handlemessage(message msg) {
        switch (msg.what) {
        case refresh_backing:
            mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                    (int) (mheaderlinearlayout.getpaddingtop()*0.75f),
                    mheaderlinearlayout.getpaddingright(),
                    mheaderlinearlayout.getpaddingbottom());
            break;
        case refresh_baced:
            mheadertextview.settext("正在加载...");
            mheaderprogressbar.setvisibility(view.visible);
            mheaderpulldownimageview.setvisibility(view.gone);
            mheaderreleasedownimageview.setvisibility(view.gone);
            mpullrefreshstate = exit_pull_refresh;
            new thread() {
                public void run() {
                    sleep(2000);//处理后台加载数据
                    message msg = mhandler.obtainmessage();
                    msg.what = refresh_done;
                    //通知主线程加载数据完成
                    mhandler.sendmessage(msg);
                };
            }.start();
            break;
        case refresh_return:
            //未达到刷新界限,返回
            mheadertextview.settext("下拉刷新");
            mheaderprogressbar.setvisibility(view.invisible);
            mheaderpulldownimageview.setvisibility(view.visible);
            mheaderreleasedownimageview.setvisibility(view.gone);
            mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                    0,
                    mheaderlinearlayout.getpaddingright(),
                    mheaderlinearlayout.getpaddingbottom());
            mpullrefreshstate = none_pull_refresh;
            setselection(1);
            break;
        case refresh_done:
            //刷新结束后,恢复原始默认状态
            mheadertextview.settext("下拉刷新");
            mheaderprogressbar.setvisibility(view.invisible);
            mheaderpulldownimageview.setvisibility(view.visible);
            mheaderreleasedownimageview.setvisibility(view.gone);
            mheaderupdatetext.settext(getcontext().getstring(r.string.app_list_header_refresh_last_update,
                    msimpledateformat.format(new date())));
            mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                    0,
                    mheaderlinearlayout.getpaddingright(),
                    mheaderlinearlayout.getpaddingbottom());
            mpullrefreshstate = none_pull_refresh;
            setselection(1);
            break;
        default:
            break;
        }
    }
};

    为了一下子看的明确,我把效果中的数据处理代码也贴出来了。

8. 切入数据加载过程
    上面数据后台处理我们用sleep(2000)来处理,实际处理中,作为公共组件,我们也不好把具体代码直接写在这里,我们需要一个更灵活的分离:
    (1). 定义接口
    (2). 注入接口

复制代码 代码如下:

//定义接口
public interface refreshlistener {
    object refreshing();                //加载数据
    void refreshed(object obj);    //外部可扩展加载完成后的操作
}

//注入接口
private object mrefreshobject = null; //传值
private refreshlistener mrefreshlistener = null;
public void setonrefreshlistener(refreshlistener refreshlistener) {
    this.mrefreshlistener = refreshlistener;
}

 
//我们需要重写上面的mhandler如下代码
case refresh_baced:
    ... ...
    new thread() {
        public void run() {
            if (mrefreshlistener != null) {
                mrefreshobject = mrefreshlistener.refreshing();
            }
            message msg = mhandler.obtainmessage();
            msg.what = refresh_done;
            mhandler.sendmessage(msg);
        };
    }.start();
    break;
case refresh_done:
    ... ...
    mpullrefreshstate = none_pull_refresh;
    setselection(1);
    if (mrefreshlistener != null) {
        mrefreshlistener.refreshed(mrefreshobject);
    }
    break;

在其他地方我们就可以不修改这个listview组件的代码,使用如下:
复制代码 代码如下:

public xxx implements refreshlistener{

@override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        //类似如下
        ((refreshlistview) listview).setonrefreshlistener(this);
    }

    @override
    public object refreshing() {
        string result = null;
        //result = fileutils.readtextfile(file);
        return result;
    }

    @override
    public void refreshed(object obj) {
        if (obj != null) {
           //扩展操作
        }
    };
}

  很方便了。

9. 扩展"更多"功能
    下拉刷新之外,我们也可以通过相同方法使用footerview切入底部"更多"过程,这里我就不详细说明了

10. 源码
    上面的每段代码都看做是"零部件",需要组合一下。
    因为我们上面实现了下拉刷新,还增加了"更多"功能,我们直接命名这个类为refreshlistview吧:

复制代码 代码如下:

package com.tianxia.lib.baseworld.widget;

import java.text.simpledateformat;
import java.util.date;

import android.content.context;
import android.os.handler;
import android.os.message;
import android.util.attributeset;
import android.view.layoutinflater;
import android.view.motionevent;
import android.view.view;
import android.view.viewgroup;
import android.widget.abslistview;
import android.widget.abslistview.onscrolllistener;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.listadapter;
import android.widget.listview;
import android.widget.progressbar;
import android.widget.textview;

import com.tianxia.lib.baseworld.r;

/**
 * 下拉刷新,底部更多
 *
 */
public class refreshlistview extends listview implements onscrolllistener{

    private float mdowny;
    private float mmovey;

    private int mheaderheight;

    private int mcurrentscrollstate;

    private final static int none_pull_refresh = 0;    //正常状态
    private final static int enter_pull_refresh = 1;   //进入下拉刷新状态
    private final static int over_pull_refresh = 2;    //进入松手刷新状态
    private final static int exit_pull_refresh = 3;    //松手后反弹和加载状态
    private int mpullrefreshstate = 0;                 //记录刷新状态

    private final static int refresh_backing = 0;      //反弹中
    private final static int refresh_baced = 1;        //达到刷新界限,反弹结束后
    private final static int refresh_return = 2;       //没有达到刷新界限,返回
    private final static int refresh_done = 3;         //加载数据结束

    private linearlayout mheaderlinearlayout = null;
    private linearlayout mfooterlinearlayout = null;
    private textview mheadertextview = null;
    private textview mheaderupdatetext = null;
    private imageview mheaderpulldownimageview = null;
    private imageview mheaderreleasedownimageview = null;
    private progressbar mheaderprogressbar = null;
    private textview mfootertextview = null;
    private progressbar mfooterprogressbar = null;

    private simpledateformat msimpledateformat;

    private object mrefreshobject = null;
    private refreshlistener mrefreshlistener = null;
    public void setonrefreshlistener(refreshlistener refreshlistener) {
        this.mrefreshlistener = refreshlistener;
    }

    public refreshlistview(context context) {
        this(context, null);
    }

    public refreshlistview(context context, attributeset attrs) {
        super(context, attrs);
        init(context);
    }

    void init(final context context) {
        mheaderlinearlayout = (linearlayout) layoutinflater.from(context).inflate(r.layout.refresh_list_header, null);
        addheaderview(mheaderlinearlayout);
        mheadertextview = (textview) findviewbyid(r.id.refresh_list_header_text);
        mheaderupdatetext = (textview) findviewbyid(r.id.refresh_list_header_last_update);
        mheaderpulldownimageview = (imageview) findviewbyid(r.id.refresh_list_header_pull_down);
        mheaderreleasedownimageview = (imageview) findviewbyid(r.id.refresh_list_header_release_up);
        mheaderprogressbar = (progressbar) findviewbyid(r.id.refresh_list_header_progressbar);

        mfooterlinearlayout = (linearlayout) layoutinflater.from(context).inflate(r.layout.refresh_list_footer, null);
        addfooterview(mfooterlinearlayout);
        mfooterprogressbar = (progressbar) findviewbyid(r.id.refresh_list_footer_progressbar);
        mfootertextview = (textview) mfooterlinearlayout.findviewbyid(r.id.refresh_list_footer_text);
        mfooterlinearlayout.setonclicklistener(new onclicklistener() {
            @override
            public void onclick(view v) {
                if (context.getstring(r.string.app_list_footer_more).equals(mfootertextview.gettext())) {
                    mfootertextview.settext(r.string.app_list_footer_loading);
                    mfooterprogressbar.setvisibility(view.visible);
                    if (mrefreshlistener != null) {
                        mrefreshlistener.more();
                    }
                }
            }
        });

        setselection(1);
        setonscrolllistener(this);
        measureview(mheaderlinearlayout);
        mheaderheight = mheaderlinearlayout.getmeasuredheight();

        msimpledateformat = new simpledateformat("yyyy-mm-dd hh:mm");
        mheaderupdatetext.settext(context.getstring(r.string.app_list_header_refresh_last_update, msimpledateformat.format(new date())));
    }

    @override
    public boolean ontouchevent(motionevent ev) {
        switch (ev.getaction()) {
            case motionevent.action_down:
                mdowny = ev.gety();
                break;
            case motionevent.action_move:
                mmovey = ev.gety();
                if (mpullrefreshstate == over_pull_refresh) {
                    mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                            (int)((mmovey - mdowny)/3),
                            mheaderlinearlayout.getpaddingright(),
                            mheaderlinearlayout.getpaddingbottom());
                }
                break;
            case motionevent.action_up:
                //when you action up, it will do these:
                //1. roll back util header toppadding is 0
                //2. hide the header by setselection(1)
                if (mpullrefreshstate == over_pull_refresh || mpullrefreshstate == enter_pull_refresh) {
                    new thread() {
                        public void run() {
                            message msg;
                            while(mheaderlinearlayout.getpaddingtop() > 1) {
                                msg = mhandler.obtainmessage();
                                msg.what = refresh_backing;
                                mhandler.sendmessage(msg);
                                try {
                                    sleep(5);
                                } catch (interruptedexception e) {
                                    e.printstacktrace();
                                }
                            }
                            msg = mhandler.obtainmessage();
                            if (mpullrefreshstate == over_pull_refresh) {
                                msg.what = refresh_baced;
                            } else {
                                msg.what = refresh_return;
                            }
                            mhandler.sendmessage(msg);
                        };
                    }.start();
                }
                break;
        }
        return super.ontouchevent(ev);
    }

    @override
    public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
        if (mcurrentscrollstate == scroll_state_touch_scroll
                && firstvisibleitem == 0
                && (mheaderlinearlayout.getbottom() >= 0 && mheaderlinearlayout.getbottom() < mheaderheight)) {
            //进入且仅进入下拉刷新状态
            if (mpullrefreshstate == none_pull_refresh) {
                mpullrefreshstate = enter_pull_refresh;
            }
        } else if (mcurrentscrollstate == scroll_state_touch_scroll
                && firstvisibleitem == 0
                && (mheaderlinearlayout.getbottom() >= mheaderheight)) {
            //下拉达到界限,进入松手刷新状态
            if (mpullrefreshstate == enter_pull_refresh || mpullrefreshstate == none_pull_refresh) {
                mpullrefreshstate = over_pull_refresh;
                mdowny = mmovey; //为下拉1/3折扣效果记录开始位置
                mheadertextview.settext("松手刷新");//显示松手刷新
                mheaderpulldownimageview.setvisibility(view.gone);//隐藏"下拉刷新"
                mheaderreleasedownimageview.setvisibility(view.visible);//显示向上的箭头
            }
        } else if (mcurrentscrollstate == scroll_state_touch_scroll && firstvisibleitem != 0) {
            //不刷新了
            if (mpullrefreshstate == enter_pull_refresh) {
                mpullrefreshstate = none_pull_refresh;
            }
        } else if (mcurrentscrollstate == scroll_state_fling && firstvisibleitem == 0) {
            //飞滑状态,不能显示出header,也不能影响正常的飞滑
            //只在正常情况下才纠正位置
            if (mpullrefreshstate == none_pull_refresh) {
                setselection(1);
            }
        }
    }

    @override
    public void onscrollstatechanged(abslistview view, int scrollstate) {
        mcurrentscrollstate = scrollstate;
    }

    @override
    public void setadapter(listadapter adapter) {
        super.setadapter(adapter);
        setselection(1);
    }

    private void measureview(view child) {
        viewgroup.layoutparams p = child.getlayoutparams();
        if (p == null) {
            p = new viewgroup.layoutparams(viewgroup.layoutparams.fill_parent,
                    viewgroup.layoutparams.wrap_content);
        }

        int childwidthspec = viewgroup.getchildmeasurespec(0, 0 + 0, p.width);
        int lpheight = p.height;
        int childheightspec;
        if (lpheight > 0) {
            childheightspec = measurespec.makemeasurespec(lpheight,
                    measurespec.exactly);
        } else {
            childheightspec = measurespec.makemeasurespec(0,
                    measurespec.unspecified);
        }
        child.measure(childwidthspec, childheightspec);
    }

    private handler mhandler = new handler(){
        @override
        public void handlemessage(message msg) {
            switch (msg.what) {
            case refresh_backing:
                mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                        (int) (mheaderlinearlayout.getpaddingtop()*0.75f),
                        mheaderlinearlayout.getpaddingright(),
                        mheaderlinearlayout.getpaddingbottom());
                break;
            case refresh_baced:
                mheadertextview.settext("正在加载...");
                mheaderprogressbar.setvisibility(view.visible);
                mheaderpulldownimageview.setvisibility(view.gone);
                mheaderreleasedownimageview.setvisibility(view.gone);
                mpullrefreshstate = exit_pull_refresh;
                new thread() {
                    public void run() {
                        if (mrefreshlistener != null) {
                            mrefreshobject = mrefreshlistener.refreshing();
                        }
                        message msg = mhandler.obtainmessage();
                        msg.what = refresh_done;
                        mhandler.sendmessage(msg);
                    };
                }.start();
                break;
            case refresh_return:
                mheadertextview.settext("下拉刷新");
                mheaderprogressbar.setvisibility(view.invisible);
                mheaderpulldownimageview.setvisibility(view.visible);
                mheaderreleasedownimageview.setvisibility(view.gone);
                mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                        0,
                        mheaderlinearlayout.getpaddingright(),
                        mheaderlinearlayout.getpaddingbottom());
                mpullrefreshstate = none_pull_refresh;
                setselection(1);
                break;
            case refresh_done:
                mheadertextview.settext("下拉刷新");
                mheaderprogressbar.setvisibility(view.invisible);
                mheaderpulldownimageview.setvisibility(view.visible);
                mheaderreleasedownimageview.setvisibility(view.gone);
                mheaderupdatetext.settext(getcontext().getstring(r.string.app_list_header_refresh_last_update,
                        msimpledateformat.format(new date())));
                mheaderlinearlayout.setpadding(mheaderlinearlayout.getpaddingleft(),
                        0,
                        mheaderlinearlayout.getpaddingright(),
                        mheaderlinearlayout.getpaddingbottom());
                mpullrefreshstate = none_pull_refresh;
                setselection(1);
                if (mrefreshlistener != null) {
                    mrefreshlistener.refreshed(mrefreshobject);
                }
                break;
            default:
                break;
            }
        }
    };
    public interface refreshlistener {
        object refreshing();
        void refreshed(object obj);
        void more();
    }

    public void finishfootview() {
        mfooterprogressbar.setvisibility(view.gone);
        mfootertextview.settext(r.string.app_list_footer_more);
    }

    public void addfootview() {
        if (getfooterviewscount() == 0) {
            addfooterview(mfooterlinearlayout);
        }
    }

    public void removefootview() {
        removefooterview(mfooterlinearlayout);
    }
}