简述
目前流行android 下拉刷新上拉加载开源框架有很多,XListView,PullToRefresh,android-Ultra-Pull-To-Refresh 等,但是能够像android-Ultra-Pull-To-Refresh那样,将上下拉处理事件分装到一个类处理,并且提供自定义上下拉显示效果的开源组件不多。所以,小编初试,封装一个兼容性不错,而且可以自定义上下拉效果的组件popularrefreshlayout,
支持gradle引用 compile 'com.awarmisland.android:popularrefreshlayout:1.0.6'
功能简述
目前,小编设置两种下拉刷新效果,一种是经典默认下拉刷新效果,一种是Material风格的,如效果图所示。经过调试,PopularRefreshLayout支持ScrollView ,ListView,WebView等。
使用方式
- gradle.build中添加引用 compile 'com.awarmisland.android:popularrefreshlayout:1.0.4'****
- 将需要下拉刷新的内容外面添加组件
<com.awarmisland.android.popularrefreshlayout.RefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--刷新内容-->
<com.awarmisland.android.popularrefreshlayout.RefreshLayout />
3.设置下拉刷新,上拉加载监听(以默认下拉刷新效果内嵌ListView为例子)
RefreshLayout refresh = (RefreshLayout) findViewById(R.id.refresh);
refresh.setAllowLoadMore(true);//设置支持上拉加载
refresh.setRefreshListener(refreshListener);
private RefreshListener refreshListener = new RefreshListener() {
@Override
public void onRefresh(final RefreshLayout refreshLayout) {
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.finishRefresh();
adapter.clear();
//刷新数据
adapter.notifyDataSetChanged();
}
}, 3000);
}
@Override
public void onRefreshLoadMore(final RefreshLayout refreshLayout) {
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
refreshLayout.finishRefreshLoadMore();
//添加数据
}
}, 3000);
}
};
4.自定义下拉上拉效果,通过自定义类继承BaseHeader 和BaseFooter,提供上下拉手势过程onBegin——>onPull ——>onRelease——>onRefreshing——>onComlete
public abstract void onComlete(RefreshLayout materialRefreshLayout);
public abstract void onBegin(RefreshLayout materialRefreshLayout);
public abstract void onPull(RefreshLayout materialRefreshLayout, float fraction);
public abstract void onRelease(RefreshLayout materialRefreshLayout, float fraction);
public abstract void onRefreshing(RefreshLayout materialRefreshLayout);
5.以Material风格下拉刷新效果为例,添加自定义刷新效果
RefreshLayout refresh = (RefreshLayout) findViewById(R.id.refresh);
MaterialHeaderView headerView = new MaterialHeaderView(this);//初始化自定义刷新头部
refresh.setHeaderView(headerView);//添加头部
refresh.setAllowLoadMore(true);