特点:无侵入的下拉刷新,就是可用在ListView,ScrollView,GridView,ViewPager等所有能滑动的控件上,而且扩展性强,可以监听下拉进度,更改下拉和上拉的布局以及动画,github地址:https://github.com/chrisbanes/Android-PullToRefresh,其作者是Google官方Android工程师ChrisBane
原理:
在onInterceptTouchEvent方法中去判断是否应该拦截以及拖拽的方向,并通过isReadyForPull()方法判断是要拉出刷新布局还是让refreshableView本身滚动,该方法是抽象方法,由子类实现;
将刷新布局抽象为LoadingLayout,并接收当前刷新布局滚动的比例,用以给上拉和下拉刷新动画提供接口,其目前实现类有RotateLoadingLayout和FlipLoadingLayout,即旋转箭头和翻转箭头效果,默认是使用RotateLoadingLayout作为刷新动画效果;
1导入第三方类库 添加此类库的依赖
compile project(':pullToRefresh')
2.设置布局文件
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/ptrScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#8ac100"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#00ff8a"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#10a600"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#22a0c9"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#99a0c9"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#ac00c9"/>
<View
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#ac0011"/>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
3实现刷新功能
ptrScrollView = (PullToRefreshScrollView) findViewById(R.id.ptrScrollView);
ptrScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
ptrScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
requestData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
}
});
}
private void requestData() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(ptrScrollViewActivity.this, "个人信息更新成功! ", Toast.LENGTH_SHORT).show();
ptrScrollView.onRefreshComplete();
}
}, 1000);
}