一般来说下拉刷新有这么几个状态,就拿QQ的下拉刷新来说吧
首先是往下拉的时候:
然后是下拉超过一定距离的时候:
然后是手指释放的时候刷新:
最后就是刷新成功或者失败的时候:
大概就是以上效果
所以自定义一个下拉刷新控件需要结合onTouchEvent来实现。
首先,自定义下拉刷新控件的布局文件refresh_header.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--下拉刷新控件-->
<LinearLayout
android:id="@+id/ll_pull_down_refresh"
android:padding="8dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<FrameLayout
android:layout_gravity="center"
android:layout_width="80dp"
android:layout_height="80dp">
<ImageView
android:id="@+id/iv_arrow"
android:src="@drawable/refresh_arrow"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:visibility="visible"
android:id="@+id/pb_status"
android:layout_gravity="center"
android:indeterminateDrawable="@drawable/custom_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
<LinearLayout
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center_horizontal"
android:id="@+id/tv_status"
android:text="下拉刷新"
android:textColor="@android:color/holo_blue_light"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center_horizontal"
android:layout_marginTop="5dp"
android:id="@+id/tv_time"
android:text="上次更新时间: 2017-10-15"
android:textColor="#55000000"
android:textSize="14sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
上面这个布局分为两块,一块显示箭头以及刷新之后的ProgressBar,另外一块显示文字用于表面当前处于什么样的刷新状态以及上次刷新的事件。其中,箭头是用的图片资源,ProgressBar则是自定义的小圆环,由于刷新的时候ProgressBar一直是旋转的,所以它的布局属性为 android:indeterminateDrawable ,表示状态不确定,然后是它的布局custom_progressbar.xml: