Dome下载:链接:https://pan.baidu.com/s/13Y5nZSzmS3k00-v0rQ2RoA 密码:0h5u
目前只支持 Android Studio
compile 'com.jwenfeng.pulltorefresh:library:1.2.7'
注意:内容控件 有且只能有一个,目前支持:ScrollView
,ListView
,WebView
,RecyclerView
。
xml version="1.0" encoding="utf-8"?>
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
ScrollView>
com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>
pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
@Override
public void refresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 结束刷新
pullToRefreshLayout.finishRefresh();
}
}, 2000);
}
@Override
public void loadMore() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// 结束加载更多
pullToRefreshLayout.finishLoadMore();
}
}, 2000);
}
});
自定义下拉刷新视图需要实现 HeadView
接口
public interface HeadView {
/**
* 开始下拉
*/
void begin();
/**
* 回调的精度,单位为px
*
* @param progress 当前高度
* @param all 总高度
*/
void progress(float progress, float all);
void finishing(float progress, float all);
/**
* 下拉完毕
*/
void loading();
/**
* 看不见的状态
*/
void normal();
/**
* 返回当前视图
* */
View getView();
}
具体请参考Demo中 HeadRefreshView
上拉加载更多用法和下拉刷新类似,具体参考 LoadMoreView.java
不用每次都设置头部和底部啦,可以继承PullToRefreshLayout,具体参考 NormalPullToRefreshLayout
可以设置下拉刷新和上拉加载控件的高度和拉取的最大高度,默认为60dp,最大拉取为120dp,可自行设置。
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:view_error="@layout/layout_error"
app:view_empty="@layout/layout_empty"
app:view_loading="@layout/layout_loading">
com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>
app:view_error 网络错误页面
app:view_empty 空数据页面
app:view_loading 加载中页面
该三个属性可以不填写,默认样式为上图所示,可以自定义
pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
设置需要显示的视图
// 获取页面
View error = pullToRefreshLayout.getView(ViewStatus.ERROR_STATUS);
获取所需要的视图view
//可以自定义效果
public class LoadMoreView extends FrameLayout implements FooterView {
private TextView tv;
private ImageView arrow;
private ProgressBar progressBar;
public LoadMoreView(Context context) {
this(context,null);
}
public LoadMoreView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public LoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_header,null);
addView(view);
tv = (TextView) view.findViewById(R.id.header_tv);
arrow = (ImageView) view.findViewById(R.id.header_arrow);
progressBar = (ProgressBar) view.findViewById(R.id.header_progress);
}
@Override
public void begin() {
}
@Override
public void progress(float progress, float all) {
float s = progress / all;
if (s >= 0.9f){
arrow.setRotation(0);
}else{
arrow.setRotation(180);
}
if (progress >= all-10){
tv.setText("松开加载更多");
}else{
tv.setText("上拉加载");
}
}
@Override
public void finishing(float progress, float all) {
}
@Override
public void loading() {
arrow.setVisibility(GONE);
progressBar.setVisibility(VISIBLE);
tv.setText("加载中...");
}
@Override
public void normal() {
arrow.setVisibility(VISIBLE);
progressBar.setVisibility(GONE);
tv.setText("上拉加载");
}
@Override
public View getView() {
return this;
}
}