下拉刷新祸轮,上拉加載兽埃,在Android項(xiàng)目中可謂是隨處可見,其實(shí)現(xiàn)方式也是各種各樣适袜。今天介紹一個(gè)android support v4包下提供的下拉刷新控件SwipeRefreshLayout,個(gè)人覺得這個(gè)還是比較好看的
</br>
一柄错、下拉刷新
看看知乎中的下拉刷新效果圖:
如何使用
//xml布局
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/wifi_swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fl_title">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_wifi_table"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
//Activity中使用
wifi_swipe_refresh = (SwipeRefreshLayout) findViewById(R.id.wifi_swipe_refresh);
wifi_swipe_refresh.setOnRefreshListener(this);//SwipeRefreshLayout.OnRefreshListener
wifi_swipe_refresh.setColorSchemeResources(
R.color.google_blue,
R.color.google_green,
R.color.google_red,
R.color.google_yellow
);
上述監(jiān)聽事件(這里使用 RxJava實(shí)現(xiàn)定時(shí)操作)
@Override
public void onRefresh() {
Observable.timer(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).map(new Func1<Long, Object>() {
@Override
public Object call(Long aLong) {
initData();
if (null != wifi_swipe_refresh) {
wifi_swipe_refresh.setRefreshing(false);
}
return null;
}
}).subscribe();
}
進(jìn)入時(shí)加載circleview 加載動(dòng)畫:
1、默認(rèn)setRefreshing(true)方法,無(wú)法實(shí)線初次加載顯示動(dòng)畫的效果售貌。我們可以這樣處理:
swipeRefreshLayout.setProgressViewOffset(false, 0,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().
getDisplayMetrics()));
swipeRefreshLayout.setRefreshing(true);.0
2给猾、使用swipeRefreshLayout.post()方式來(lái)加載顯示動(dòng)畫
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
關(guān)閉circleview 加載動(dòng)畫:
由于子線程中不能更新UI,所以需要通過(guò)hander來(lái)定時(shí)關(guān)閉加載動(dòng)畫
使用handler計(jì)時(shí) 定時(shí)關(guān)閉
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
}
},2000);//2000ms后取消(關(guān)閉)
避免刷新過(guò)程中仍可以操作界面,自定義SwipeRefreshLayout颂跨,通過(guò)父類拿到refresh狀態(tài)敢伸,從而進(jìn)行事件的傳遞機(jī)制控制
public class MySwipeRefreshLayout extends SwipeRefreshLayout {
public MySwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (super.isRefreshing()) {//調(diào)用父view中的isRefreshing()方法,從而拿到刷新狀態(tài)毫捣。刷新時(shí)消化點(diǎn)擊事件
return true;
}
return super.onInterceptTouchEvent(ev);
}
}
二、上拉加載(滑到底部自動(dòng)加載)
結(jié)合HeaderViewRecyclerAdapter
private void initView() {
firstCreateFootView = true;//首次加載headview蔓同,headview始終只能創(chuàng)建一次
manager = new LinearLayoutManager(getActivity());
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_fans.setLayoutManager(manager);
adapter = new FansInfoAdapter(getActivity());
/**
* 核心代碼:RecyclerView.Adapter的adapter作為參數(shù),初始化HeaderViewRecyclerAdapter
* headerViewRecyclerAdapter才是作為RecyclerView的adapter蹲诀,也是在這一步完成綁定headerView
*/
headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(adapter);
recyclerView_fans.setAdapter(headerViewRecyclerAdapter);
recyclerView_fans.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisiableItemPosition = manager.findLastVisibleItemPosition();
if (lastVisiableItemPosition + 1 == headerViewRecyclerAdapter.getItemCount() && headerViewRecyclerAdapter.getItemCount() >= pageSize) {
LogUtil.i("info", "滑動(dòng)到底了pos:" + lastVisiableItemPosition);
//執(zhí)行加載更多數(shù)據(jù)
simulateLoadMoreData();
}
}
});
/**
* 下拉刷新監(jiān)聽
*/
swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Observable.timer(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).map(new Func1<Long, Object>() {
@Override
public Object call(Long aLong) {
initData();
swipe_refresh_layout.setRefreshing(false);
headerViewRecyclerAdapter.notifyDataSetChanged();
// ToastUtils.show(getActivity(), "刷新成功");
return null;
}
}).subscribe();
}
});
}
核心的點(diǎn)在于何時(shí)去創(chuàng)建底部的headerview斑粱,這很有講究,因?yàn)槲覀冞@里做的是滑動(dòng)到底部自動(dòng)加載更多數(shù)據(jù)脯爪,因此判斷這個(gè)臨界點(diǎn)成為核心则北,解決思路:從每次的請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù)回調(diào)中判斷數(shù)據(jù)條目是否滿一頁(yè)即是否等于pageSize,瞞著條件即可調(diào)用創(chuàng)建hederview方法,動(dòng)態(tài)添加headerview痕慢,注意:全程只可以創(chuàng)建一次尚揣,因此這邊還要控制好。
//第一次網(wǎng)絡(luò)請(qǐng)求掖举,數(shù)據(jù)回調(diào)時(shí)進(jìn)行判斷快骗,滿足條件才創(chuàng)建
private void createLoadMoreView() {
if (getActivity() == null) {
return;
}
//headerView 布局可以自行定義
View loadMoreView = LayoutInflater.from(getActivity()).inflate(R.layout.item_view_load_more, recyclerView_fans, false);
headerViewRecyclerAdapter.addFooterView(loadMoreView);
}
同時(shí)當(dāng)數(shù)據(jù)已加載全部時(shí)還需要將headerview隱藏(remove)
//此處進(jìn)行判斷,滿足條件時(shí)調(diào)用headerViewRecyclerAdapter.notifyItemRemoved(headerViewRecyclerAdapter.getItemCount());去掉headerview
if (null != fansCardTableBeen.get(0).getUser_phone() && fansCardTableBeen.get(0).getUser_phone().equals("")) {//已滑到最后一頁(yè)
if (currentPage != 0) {
currentPage--;
ToastUtils.show(getActivity(), "沒有更多數(shù)據(jù)");
headerViewRecyclerAdapter.notifyItemRemoved(headerViewRecyclerAdapter.getItemCount());
} else {
tv_empty_data.setVisibility(View.VISIBLE);
recyclerView_fans.setVisibility(View.GONE);
}
} else {
for (FansCardTableBean bean : fansCardTableBeen) {
list.add(bean);
}
adapter.setData(list);
adapter.notifyDataSetChanged();
}
}
這里貼上HeaderViewRecyclerAdapter 源碼(來(lái)源于網(wǎng)絡(luò)博友博客)
/** * Created by Android Studio * User: Ailurus(ailurus@foxmail.com) * Date: 2015-10-26 * Time: 18:23 */
public class HeaderViewRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int HEADERS_START = Integer.MIN_VALUE;
private static final int FOOTERS_START = Integer.MIN_VALUE + 10;
private static final int ITEMS_START = Integer.MIN_VALUE + 20;
private static final int ADAPTER_MAX_TYPES = 100;
private RecyclerView.Adapter mWrappedAdapter;
private List<View> mHeaderViews, mFooterViews;
private Map<Class, Integer> mItemTypesOffset;
public HeaderViewRecyclerAdapter(RecyclerView.Adapter adapter) {
mHeaderViews = new ArrayList<>();
mFooterViews = new ArrayList<>();
mItemTypesOffset = new HashMap<>();
setWrappedAdapter(adapter);
}
public void setAdapter(RecyclerView.Adapter adapter) {
if (mWrappedAdapter != null && mWrappedAdapter.getItemCount() > 0) {
notifyItemRangeRemoved(getHeaderCount(), mWrappedAdapter.getItemCount());
}
setWrappedAdapter(adapter);
notifyItemRangeInserted(getHeaderCount(), mWrappedAdapter.getItemCount());
}
@Override
public int getItemViewType(int position) {
int hCount = getHeaderCount();
if (position < hCount) return HEADERS_START + position;
else {
int itemCount = mWrappedAdapter.getItemCount();
if (position < hCount + itemCount) {
return getAdapterTypeOffset() + mWrappedAdapter.getItemViewType(position - hCount);
} else return FOOTERS_START + position - hCount - itemCount;
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if (viewType < HEADERS_START + getHeaderCount())
return new StaticViewHolder(mHeaderViews.get(viewType - HEADERS_START));
else if (viewType < FOOTERS_START + getFooterCount())
return new StaticViewHolder(mFooterViews.get(viewType - FOOTERS_START));
else {
return mWrappedAdapter.onCreateViewHolder(viewGroup, viewType - getAdapterTypeOffset());
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
int hCount = getHeaderCount();
if (position >= hCount && position < hCount + mWrappedAdapter.getItemCount())
mWrappedAdapter.onBindViewHolder(viewHolder, position - hCount);
}
public void addHeaderView(View view) {
mHeaderViews.add(view);
}
public void addFooterView(View view) {
mFooterViews.add(view);
}
@Override
public int getItemCount() {
return getHeaderCount() + getFooterCount() + getWrappedItemCount();
}
public int getWrappedItemCount() {
return mWrappedAdapter.getItemCount();
}
public int getHeaderCount() {
return mHeaderViews.size();
}
public int getFooterCount() {
return mFooterViews.size();
}
private void setWrappedAdapter(RecyclerView.Adapter adapter) {
if (mWrappedAdapter != null) mWrappedAdapter.unregisterAdapterDataObserver(mDataObserver);
mWrappedAdapter = adapter;
Class adapterClass = mWrappedAdapter.getClass();
if (!mItemTypesOffset.containsKey(adapterClass)) putAdapterTypeOffset(adapterClass);
mWrappedAdapter.registerAdapterDataObserver(mDataObserver);
}
private void putAdapterTypeOffset(Class adapterClass) {
mItemTypesOffset.put(adapterClass, ITEMS_START + mItemTypesOffset.size() * ADAPTER_MAX_TYPES);
}
private int getAdapterTypeOffset() {
return mItemTypesOffset.get(mWrappedAdapter.getClass());
}
private RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
notifyDataSetChanged();
}
@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
notifyItemRangeChanged(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
notifyItemRangeInserted(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
super.onItemRangeRemoved(positionStart, itemCount);
notifyItemRangeRemoved(positionStart + getHeaderCount(), itemCount);
}
@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
int hCount = getHeaderCount();
// TODO: No notifyItemRangeMoved method?
notifyItemRangeChanged(fromPosition + hCount, toPosition + hCount + itemCount);
}
};
private static class StaticViewHolder extends RecyclerView.ViewHolder {
public StaticViewHolder(View itemView) {
super(itemView);
}
}
}