1. 上下拉控件,一般分为3个部分,refresh_headview,load_moreview,和中间的一部分view。
中间的view会随着操作者上下划拉的操作和移动位置,下拉的时候,露出头部的view,上拉的时候露出底部的view,这样就组成了一个上下拉刷新的控件了
2.refresh_headview放在view的上面,同时也是放在手机屏幕的上面,假如headview的高度为200dp,那么view的位置从-200dp来放置,同理,load_moreview,放
在view的下面,同时也是在手机屏幕的下面,这样,没有上下划拉view的时候才会显示出来,而如果没有滑动的时候,就只能看到中间的view。
3.一般分为6个状态
1.实例化init 2.释放刷新release_to_refresh 3,刷新refreshing 4,释放加载release_to_load 5,加载loading 6. 完毕done
4.通过事件截取和事件分发来获取起始点和滑动的距离,requestLayout(),来获取布局的展示,然后,改变用以上的6中状态。
5.子布局
protected void onLayout(boolean changed, int l, int t, int r, int b) { //子类的放置 if (!isLayout) { // 这里是第一次进来的时候做一些初始化 refreshView = getChildAt(0); pullableView = getChildAt(1); loadmoreView = getChildAt(2); isLayout = true; initView(); refreshDist = ((ViewGroup) refreshView).getChildAt(0) .getMeasuredHeight(); loadmoreDist = ((ViewGroup) loadmoreView).getChildAt(0) .getMeasuredHeight(); } // 改变子控件的布局,这里直接用(pullDownY + pullUpY)作为偏移量,这样就可以不对当前状态作区分 refreshView.layout(0, (int) (pullDownY + pullUpY) - refreshView.getMeasuredHeight(), refreshView.getMeasuredWidth(), (int) (pullDownY + pullUpY)); pullableView.layout(0, (int) (pullDownY + pullUpY), pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight()); loadmoreView.layout(0, (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(), loadmoreView.getMeasuredWidth(), (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight() + loadmoreView.getMeasuredHeight()); }
滑动的各个状态,可以看到headview、view、footview,在界面上展示的效果
6。添加回调借口
/** * 刷新加载回调接口 * * @author chenjing * */ public interface OnRefreshListener { /** * 刷新操作 */ void onRefresh(PullToRefreshLayout pullToRefreshLayout); /** * 加载操作 */ void onLoadMore(PullToRefreshLayout pullToRefreshLayout); }7.布局设置
<com.jingchen.pulltorefresh.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/refresh_view" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/allview_refresh_head" /> <!-- 支持所有实现Pullable接口的View --> <com.jingchen.pulltorefresh.pullableview.PullableGridView android:id="@+id/content_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="15dp" /> <include layout="@layout/allview_load_more" /> </com.jingchen.pulltorefresh.PullToRefreshLayout>
7.上面的布局是为了通用性,如果只是要一个控件的。比如GridVIew可以上下拉动,只需要在实例化gridVIew的时候,将headView和footview引入进去了就可以了,然后,位置还是按照onlayout()中的就可以了,只是引入布局的方式不同,
8.更多请看一位前辈大神自定义的万能上下拉刷新控件,自行学习,此处借用大神代码,见谅
9、源码下载