下拉刷新和上拉加載更多,從設(shè)計(jì)層面上來(lái)說(shuō)碌宴,就不是同一個(gè)層面上的,從Google官方推出的下拉刷新控件SwipeRefreshLayout就可以看出蒙畴。
一般來(lái)說(shuō)贰镣,下拉刷新是以包裹整個(gè)列表控件的容器來(lái)實(shí)現(xiàn)的,而上拉加載更多是以列表控件的一部分?jǐn)U展(footerView)來(lái)實(shí)現(xiàn)的膳凝。
下拉刷新
- Google官方的SwipeRefreshLayout
xml中控件初始化:
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
java類中實(shí)現(xiàn)
// 設(shè)置下拉出現(xiàn)小圓圈是否是縮放出現(xiàn)碑隆,出現(xiàn)的位置,最大的下拉位置
mySwipeRefreshLayout.setProgressViewOffset(true, 50, 200);
// 設(shè)置下拉圓圈的大小蹬音,兩個(gè)值 LARGE上煤, DEFAULT
mySwipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
// 設(shè)置下拉圓圈上的顏色,藍(lán)色著淆、綠色劫狠、橙色、紅色
mySwipeRefreshLayout.setColorSchemeResources(
android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
// 通過(guò) setEnabled(false) 禁用下拉刷新
mySwipeRefreshLayout.setEnabled(false);
// 設(shè)定下拉圓圈的背景
mSwipeLayout.setProgressBackgroundColor(R.color.red);
/*
* 設(shè)置手勢(shì)下拉刷新的監(jiān)聽
*/
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// 刷新動(dòng)畫開始后回調(diào)到此方法
}
}
);
- 使用第三方下拉刷新控件永部,可以更好地支持自定義下拉樣式独泞。
這里推薦一個(gè)比較好的第三方控件:android-Ultra-Pull-To-Refresh
compile 'in.srain.cube:ultra-ptr:1.0.10'
xml中控件初始化:
<in.srain.cube.views.ptr.PtrFrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ptr_duration_to_close="200"
app:ptr_duration_to_close_header="1000"
app:ptr_keep_header_when_refresh="true"
app:ptr_pull_to_fresh="false"
app:ptr_ratio_of_header_height_to_refresh="1.2"
app:ptr_resistance="1.7">
<com.zdf.zhihu.view.widget.LoadMoreRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.zdf.zhihu.view.widget.LoadMoreRecyclerView>
</in.srain.cube.views.ptr.PtrFrameLayout>
java類中實(shí)現(xiàn):
private void initRefreshView() {
// 這里可以完全自定義各種樣式,只要實(shí)現(xiàn)PtrUIHandler苔埋,
// 具體請(qǐng)參考官方文檔也源碼
MaterialHeader header = new MaterialHeader(getContext());
refreshView.setHeaderView(header);
refreshView.addPtrUIHandler(header);
refreshView.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
page = 0;
requestTopMovies();
}
});
}
@Override
public void autoRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
refreshView.autoRefresh();
}
}, 100);
}
@Override
public void refreshComplete() {
handler.post(new Runnable() {
@Override
public void run() {
refreshView.refreshComplete();
}
});
}
上拉加載更多
以列表控件的一部分?jǐn)U展實(shí)現(xiàn)阐肤,這里以RecyclerView為例,新建一個(gè)類LoadMoreRecyclerView讲坎,繼承至RecyclerView孕惜,主要的功能就是實(shí)現(xiàn)加載更多邏輯和顯示控件:
/**
* 集成了上拉加載更多的RecyclerView
*
* Created by XiaoFeng on 2017/2/3.
*/
public class LoadMoreRecyclerView extends RecyclerView {
private final int STATE_NONE = 0x00;
private final int STATE_LOADING = 0x01;
private final int STATE_FAILURE = 0x02;
private final int STATE_COMPLETE = 0x03;
// 滑到底部里最后一個(gè)個(gè)數(shù)的閥值
private static final int VISIBLE_THRESHOLD = 1;
private int state;
private boolean loadMoreEnabled = true;
private OnLoadMoreListener listener;
private SuperAdapter superAdapter;
private LoadMoreView loadMoreView;
private LinearLayoutManager layoutManager;
public LoadMoreRecyclerView(Context context) {
this(context, null);
}
public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void initLoadMore(@NonNull OnLoadMoreListener listener) {
this.listener = listener;
if (getAdapter() instanceof SuperAdapter) {
superAdapter = (SuperAdapter) getAdapter();
}
loadMoreView = new LoadMoreView(getContext());
layoutManager = (LinearLayoutManager) getLayoutManager();
addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (canLoadMore() && dy >= 0) {
int totalItemCount = layoutManager.getItemCount();
int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
if (lastVisibleItem + VISIBLE_THRESHOLD >= totalItemCount) {
onLoadMore(recyclerView);
}
}
}
});
}
public void setLoadMoreEnabled(boolean enabled) {
this.loadMoreEnabled = enabled;
}
private boolean canLoadMore() {
return state != STATE_LOADING && loadMoreEnabled;
}
private void onLoadMore(RecyclerView recyclerView) {
state = STATE_LOADING;
if (loadMoreView != null) {
loadMoreView.loading();
}
recyclerView.post(new Runnable() {
@Override
public void run() {
if (state == STATE_LOADING) {
if (superAdapter != null && loadMoreView != null) {
superAdapter.addFooterView(loadMoreView);
}
}
}
});
if (listener != null) {
listener.onLoadMore();
}
}
public void loadMoreFailed() {
state = STATE_FAILURE;
if (loadMoreView != null) {
loadMoreView.failure();
}
if (superAdapter != null) {
superAdapter.removeFooterView();
}
}
public void loadMoreComplete() {
state = STATE_COMPLETE;
if (loadMoreView != null) {
loadMoreView.complete();
}
if (superAdapter != null) {
superAdapter.removeFooterView();
}
}
}
上面出現(xiàn)的SuperAdapter是使用了一個(gè)第三方封裝的萬(wàn)能Adapter,同時(shí)支持ListView和RecyclerView晨炕,個(gè)人感覺(jué)比鴻洋大神封裝的萬(wàn)能Adapter要好一些衫画,但是基本原理都是一樣的。原理可以看他們各自的文章介紹瓮栗,已經(jīng)寫的很詳細(xì)了削罩,這里就不多說(shuō)了瞄勾。
http://www.reibang.com/p/d6a76fd3ea5b
http://blog.csdn.net/lmj623565791/article/details/38902805
http://blog.csdn.net/lmj623565791/article/details/51118836
關(guān)于分頁(yè)加載
伴隨著上拉加載更多功能的,往往是分頁(yè)加載弥激,關(guān)于分頁(yè)加載的頁(yè)數(shù)是在列表控件內(nèi)部自動(dòng)控制进陡?還是在列表控件外部自己控制?我個(gè)人認(rèn)為應(yīng)該放在列表控件外部自己來(lái)控制頁(yè)數(shù)微服,因?yàn)檫@個(gè)頁(yè)數(shù)不光是在上拉加載更多時(shí)會(huì)使用到趾疚,在下拉刷新的時(shí)候也會(huì)使用到,并且對(duì)頁(yè)數(shù)需要清零以蕴。
這里提供一種在列表控件外部控制頁(yè)數(shù)的實(shí)現(xiàn)邏輯:
// 分頁(yè)頁(yè)數(shù)
private int page = 0;
// 初始化下拉刷新控件
private void initRefreshView() {
MaterialHeader header = new MaterialHeader(getContext());
refreshView.setHeaderView(header);
refreshView.addPtrUIHandler(header);
refreshView.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
page = 0;
requestTopMovies();
}
});
}
// 初始化RecyclerView糙麦,包含上拉加載更多
private void initRecyclerView() {
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new SubjectAdapter(getContext());
recyclerView.setAdapter(adapter);
recyclerView.initLoadMore(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
page++;
requestTopMovies();
}
});
}
// 具有分頁(yè)操作的網(wǎng)絡(luò)請(qǐng)求
private void requestTopMovies() {
Subscriber subscriber = new RxCallback<List<Movie>>() {
@Override
public void onSuccess(List<Movie> movieList) {
if (adapter != null) {
if (page == 0) {
adapter.clear();
} else {
recyclerView.loadMoreComplete();
}
adapter.addAll(movieList);
}
}
@Override
public void onFinished() {
refreshComplete();
}
};
RxRetrofitClient.getInstance().requestTop250Movies(subscriber, page, 10);
}
參考:
https://github.com/hanks-zyh/SwipeRefreshLayout
http://www.reibang.com/p/d6a76fd3ea5b
http://blog.csdn.net/lmj623565791/article/details/38902805
http://blog.csdn.net/lmj623565791/article/details/51118836