PullToRefresh上拉重新整理下拉載入更多的使用
阿新 • • 發佈:2019-02-10
效果圖
1、新增 Gradle 依賴
目前只支援 Android Studio
compile 'com.jwenfeng.pulltorefresh:library:1.2.7'
2、佈局檔案
注意:內容控制元件 有且只能有一個,目前支援: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>
3、在Activity或者Fragment中使用
pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() { @Override public void refresh() { new Handler().postDelayed(new Runnable() { @Override publicvoid run() { // 結束重新整理 pullToRefreshLayout.finishRefresh(); } }, 2000); } @Override public void loadMore() { new Handler().postDelayed(new Runnable() { @Override public void run() { // 結束載入更多 pullToRefreshLayout.finishLoadMore(); } }, 2000); } });
4、自定義檢視
自定義下拉重新整理檢視需要實現 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(); }
不用每次都設定頭部和底部啦,可以繼承PullToRefreshLayout,具體參考 NormalPullToRefreshLayout
5、其他
可以設定下拉重新整理和上拉載入控制元件的高度和拉取的最大高度,預設為60dp,最大拉取為120dp,可自行設定。
6、v1.1.0 新增自定義3中狀態頁面
截圖
用法
XML
<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 載入中頁面
該三個屬性可以不填寫,預設樣式為上圖所示,可以自定義
java用法
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;
}
}