github: https://github.com/niniloveyou/SwipeRecyclerView
感覺還可以的star下謝謝!
效果圖:(效果圖循環播放後,總感覺有些詭異!, 可能是gif截的點不對, 在手機上看效果正常的)
我就不講代碼是如何實現的了。說下實現了什麼內容:
- 支持自動下拉刷新
//設置自動下拉刷新,切記要在recyclerView.setOnLoadListener()之後調用
// 因爲在沒有設置監聽接口的情況下,setRefreshing(true),調用不到OnLoadListener
mSwipeRecyclerView.setRefreshing(true);
- 支持emptyView
mSwipeRecyclerView.setEmptyView(View emptyView);
3.支持禁止上拉加載更多/下拉刷新
//禁止下拉刷新
mSwipeRecyclerView.setRefreshEnable(false);
//禁止加載更多
mSwipeRecyclerView.setLoadMoreEnable(false);
4.支持自定義footer view
//設置footerView
//但是自定義的footerView必須繼承BaseFooterView
mSwipeRecyclerView.setFooterView(new SimpleFooterView(this));
5.支持GridLayoutManager的SpanSizeLookup
//由於SwipeRecyclerView中對GridLayoutManager的SpanSizeLookup做了處理,因此對於使用了
//GridLayoutManager又要使用SpanSizeLookup的情況,可以這樣使用!
mSwipeRecyclerView.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return 3;
}
});
6.關於footerView的分割線 獲取childCount - 1 不包含footerView即可
//設置去除footerView 的分割線
mSwipeRecyclerView.getRecyclerView().addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFEECCCC);
Rect rect = new Rect();
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount() - 1;
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
//獲得child的佈局信息
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int itemDividerHeight = 1;//px
rect.set(left + 50, top, right - 50, top + itemDividerHeight);
c.drawRect(rect, paint);
}
}
});
7.如果需要對SwipeRefreshLayout或RecyclerView做其他的設置
mSwipeRecyclerView.getSwipeRefreshLayout()
mSwipeRecyclerView.getRecyclerView()
8.可能存在的問題
由於Recycler.Adapter中關於數據集更新的方法全是final的,無法重寫,並且自定義的DataObserver也沒法實現的方法 如:notifyItemMoved方法 因此使用除SwipeRecyclerView中DataObserver的方法之外的更新數據集的方法,可能會有問題所以更新數據集建議採用DataObserver中有的方法。
github: https://github.com/niniloveyou/SwipeRecyclerView