Skip to content

Commit 54e4b78

Browse files
committed
loadmore footer support anim
1 parent 547a534 commit 54e4b78

File tree

7 files changed

+160
-108
lines changed

7 files changed

+160
-108
lines changed
2.26 KB
Binary file not shown.
1.88 KB
Binary file not shown.

Library/src/com/lhh/ptrrv/library/PullToRefreshRecyclerView.java

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import android.content.Context;
44
import android.support.v4.widget.SwipeRefreshLayout;
5-
import android.support.v7.widget.GridLayoutManager;
6-
import android.support.v7.widget.LinearLayoutManager;
75
import android.support.v7.widget.RecyclerView;
86
import android.util.AttributeSet;
9-
import android.util.Log;
107
import android.view.LayoutInflater;
118
import android.view.View;
129
import android.view.ViewTreeObserver;
@@ -16,14 +13,15 @@
1613
import com.lhh.ptrrv.library.footer.LoadMoreFooter;
1714
import com.lhh.ptrrv.library.header.Header;
1815
import com.lhh.ptrrv.library.impl.PrvInterface;
16+
import com.lhh.ptrrv.library.util.PullToRefreshRecyclerViewUtil;
1917
import com.nineoldandroids.view.ViewHelper;
2018

2119
/**
2220
* Created by linhonghong on 2015/11/11.
2321
*/
2422
public class PullToRefreshRecyclerView extends SwipeRefreshLayout implements PrvInterface {
2523

26-
private static final String TAG = "PTRRV";
24+
// private static final String TAG = "PTRRV";
2725

2826
private RecyclerView mRecyclerView;
2927

@@ -33,7 +31,8 @@ public class PullToRefreshRecyclerView extends SwipeRefreshLayout implements Prv
3331
//main view,contain footer,header etc.
3432
private RelativeLayout mRootRelativeLayout;
3533

36-
private View mHeader;//header
34+
//header
35+
private View mHeader;
3736

3837
private View mEmptyView;
3938

@@ -63,6 +62,8 @@ public class PullToRefreshRecyclerView extends SwipeRefreshLayout implements Prv
6362

6463
private InterOnScrollListener mInterOnScrollListener;
6564

65+
private PullToRefreshRecyclerViewUtil mPtrrvUtil;
66+
6667
public interface PagingableListener{
6768
void onLoadMoreItems();
6869
}
@@ -86,25 +87,26 @@ public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
8687
}
8788

8889
/**
89-
* 全局入口
90+
* main
9091
*/
9192
private void setup(Context context){
9293
setupExtra(context);
9394
initView();
9495
setLinster();
9596
}
9697

98+
99+
/**
100+
* initView
101+
*/
97102
private void initView(){
98-
//初始化布局
99103
mRootRelativeLayout = (RelativeLayout)LayoutInflater.from(mContext).inflate(R.layout.ptrrv_root_view, null);
100104

101105
this.addView(mRootRelativeLayout);
102106

103107
this.setColorSchemeResources(R.color.swap_holo_green_bright, R.color.swap_holo_bule_bright,
104108
R.color.swap_holo_green_bright, R.color.swap_holo_bule_bright);
105109

106-
//初始化loadmoreview
107-
108110
mRecyclerView = (RecyclerView)mRootRelativeLayout.findViewById(R.id.recycler_view);
109111

110112
// mLinearLayoutManager = new LinearLayoutManager(mContext);
@@ -125,6 +127,7 @@ private void setupExtra(Context context){
125127
mContext = context;
126128
isLoading = false;
127129
hasMoreItems = false;
130+
mPtrrvUtil = new PullToRefreshRecyclerViewUtil();
128131
// mSpanItem = new int[SPAN_SIZE];
129132
//init something
130133
// if(mFullLayoutParams == null) {
@@ -233,10 +236,10 @@ public void onFinishLoading(boolean hasMoreItems, boolean needSetSelection) {
233236
if(getLayoutManager() == null){
234237
return;
235238
}
236-
if(!hasMoreItems){
239+
if(!hasMoreItems && mLoadMoreFooter != null){
237240

238241
//if it's last line, minus the extra height of loadmore
239-
mCurScroll = mCurScroll - 100;
242+
mCurScroll = mCurScroll - mLoadMoreFooter.getLoadMorePadding();
240243

241244
}
242245

@@ -258,60 +261,15 @@ public void onFinishLoading(boolean hasMoreItems, boolean needSetSelection) {
258261
}
259262

260263
public int findFirstVisibleItemPosition(){
261-
if(getLayoutManager() != null) {
262-
263-
if (getLayoutManager() instanceof LinearLayoutManager) {
264-
return ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
265-
}
266-
267-
if (getLayoutManager() instanceof GridLayoutManager) {
268-
return ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
269-
}
270-
271-
// if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
272-
// return (((StaggeredGridLayoutManager) getLayoutManager()).findFirstVisibleItemPositions(mSpanItem))[0];
273-
// }
274-
275-
}
276-
return RecyclerView.NO_POSITION;
264+
return mPtrrvUtil.findFirstVisibleItemPosition(getLayoutManager());
277265
}
278266

279267
public int findLastVisibleItemPosition(){
280-
if(getLayoutManager() != null) {
281-
282-
if (getLayoutManager() instanceof LinearLayoutManager) {
283-
return ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
284-
}
285-
286-
if (getLayoutManager() instanceof GridLayoutManager) {
287-
return ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
288-
}
289-
290-
// if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
291-
// return (((StaggeredGridLayoutManager) getLayoutManager()).findLastVisibleItemPositions(mSpanItem))[0];
292-
// }
293-
294-
}
295-
return RecyclerView.NO_POSITION;
268+
return mPtrrvUtil.findLastVisibleItemPosition(getLayoutManager());
296269
}
297270

298271
public int findFirstCompletelyVisibleItemPosition(){
299-
if(getLayoutManager() != null) {
300-
301-
if (getLayoutManager() instanceof LinearLayoutManager) {
302-
return ((LinearLayoutManager) getLayoutManager()).findFirstCompletelyVisibleItemPosition();
303-
}
304-
305-
if (getLayoutManager() instanceof GridLayoutManager) {
306-
return ((GridLayoutManager) getLayoutManager()).findFirstCompletelyVisibleItemPosition();
307-
}
308-
309-
// if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
310-
// return (((StaggeredGridLayoutManager) getLayoutManager()).findLastVisibleItemPositions(mSpanItem))[0];
311-
// }
312-
313-
}
314-
return RecyclerView.NO_POSITION;
272+
return mPtrrvUtil.findFirstCompletelyVisibleItemPosition(getLayoutManager());
315273
}
316274

317275
@Override
@@ -357,7 +315,7 @@ public void setLoadmoreString(String str){
357315
private void setHasMoreItems(boolean hasMoreItems) {
358316
this.hasMoreItems = hasMoreItems;
359317
if(mLoadMoreFooter == null){
360-
mLoadMoreFooter = new LoadMoreFooter(mContext);
318+
mLoadMoreFooter = new LoadMoreFooter(mContext,getRecyclerView());
361319
}
362320
if(!this.hasMoreItems) {
363321
//remove loadmore
@@ -409,8 +367,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
409367

410368
if(mIsSwipeEnable) {
411369
if (findFirstCompletelyVisibleItemPosition() != 0) {
412-
//here has a bug, if the item is too big , use findFirstCompletelyVisibleItemPosition会产生如果第一条太大,没办法完全显示则无法下啦刷新,但如果不用又会导致没拉到顶就可以下拉
413-
//如果不是第一条可见就不让下拉,要不然会出现很严重的到处都能下拉的问题
370+
//here has a bug, if the item is too big , use findFirstCompletelyVisibleItemPosition will cannot swipe
414371
PullToRefreshRecyclerView.this.setEnabled(false);
415372
} else {
416373
PullToRefreshRecyclerView.this.setEnabled(true);
@@ -446,7 +403,6 @@ public void onChanged() {
446403
return;
447404
}
448405

449-
//判断并显示emptyview
450406
RecyclerView.Adapter<?> adapter = mRecyclerView.getAdapter();
451407
if(adapter != null && mEmptyView != null) {
452408
if(adapter.getItemCount() == 0) {
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,84 @@
11
package com.lhh.ptrrv.library.footer;
22

3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Rect;
6+
import android.os.Handler;
7+
import android.os.Message;
38
import android.support.v7.widget.RecyclerView;
49

10+
import com.lhh.ptrrv.library.util.PullToRefreshRecyclerViewUtil;
11+
512
/**
613
* Created by Linhh on 15/11/15.
714
*/
815
public class BaseFooter extends RecyclerView.ItemDecoration {
9-
public void setLoadmoreString(String str){
16+
17+
protected RecyclerView mRecyclerView;
18+
protected String mLoadMoreString;
19+
protected static final int MSG_INVILIDATE = 1;
20+
protected long mUpdateTime = 150;
21+
protected PullToRefreshRecyclerViewUtil mPtrrvUtil;
22+
protected int mLoadMorePadding = 100;
23+
24+
public BaseFooter(Context context, RecyclerView recyclerView){
25+
mRecyclerView = recyclerView;
26+
mPtrrvUtil = new PullToRefreshRecyclerViewUtil();
27+
}
28+
29+
public void setLoadmoreString(String str) {
30+
mLoadMoreString = str;
31+
}
32+
33+
public int getLoadMorePadding(){
34+
return mLoadMorePadding;
35+
}
36+
37+
protected Handler mInvalidateHanlder = new Handler(){
38+
@Override
39+
public void handleMessage(Message msg) {
40+
super.handleMessage(msg);
41+
if (mRecyclerView == null || mRecyclerView.getAdapter() == null) {
42+
return;
43+
}
44+
int lastItemPosition = mRecyclerView.getAdapter().getItemCount() - 1;
45+
if (mPtrrvUtil.findLastVisibleItemPosition(mRecyclerView.getLayoutManager()) == lastItemPosition) {
46+
47+
//when the item is visiable do this method
48+
// View view = mRecyclerView.getLayoutManager().findViewByPosition(lastItemPosition);
49+
// mInvilidateRect.set(0, 0, view.getRight() - view.getLeft(), view.getBottom() - view.getTop());
50+
mRecyclerView.invalidate();
51+
52+
}
53+
}
54+
};
55+
56+
57+
@Override
58+
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
59+
super.onDrawOver(c, parent, state);
60+
mInvalidateHanlder.removeMessages(MSG_INVILIDATE);
61+
onDrawLoadMore(c, parent);
62+
mInvalidateHanlder.sendEmptyMessageDelayed(MSG_INVILIDATE, mUpdateTime);
63+
}
64+
65+
/**
66+
* @param outRect
67+
* @param itemPosition
68+
* @param parent
69+
*/
70+
@Override
71+
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
72+
if(itemPosition == parent.getAdapter().getItemCount() - 1) {
73+
outRect.set(0, 0, 0, getLoadMorePadding());
74+
}
75+
}
76+
77+
protected void onDrawLoadMore(Canvas c, RecyclerView parent){
78+
79+
}
80+
81+
public void release(){
82+
mRecyclerView = null;
1083
}
1184
}

Library/src/com/lhh/ptrrv/library/footer/LoadMoreFooter.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.graphics.Canvas;
55
import android.graphics.Color;
66
import android.graphics.Paint;
7-
import android.graphics.Rect;
87
import android.graphics.RectF;
98
import android.support.v7.widget.RecyclerView;
109
import android.view.View;
@@ -21,49 +20,31 @@ public class LoadMoreFooter extends BaseFooter {
2120

2221
private int mCircleSize = 25;
2322

24-
private int mLoadMorePadding = 100;//给loadmore预留一点空间
25-
2623
private int mProgress = 30;//圆圈比例
2724

28-
private int mCircleOffset = 50;
29-
30-
private String mLoadMoreString;
25+
private int mCircleOffset = 70;
3126

32-
public LoadMoreFooter(Context context) {
27+
public LoadMoreFooter(Context context, RecyclerView recyclerView) {
28+
super(context, recyclerView);
3329
paint = new Paint();
3430
oval = new RectF();
3531
mLoadMoreString = context.getString(R.string.loading);
3632
}
3733

3834
@Override
39-
public void setLoadmoreString(String str) {
40-
mLoadMoreString = str;
41-
}
42-
43-
@Override
44-
public void onDraw(Canvas c, RecyclerView parent) {
45-
// final int childCount = parent.getChildCount();
46-
// drawLoadmore(c, parent);
47-
}
48-
49-
@Override
50-
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
51-
super.onDrawOver(c, parent, state);
52-
drawLoadmore(c, parent);
53-
}
54-
55-
56-
public void drawLoadmore(Canvas c, RecyclerView parent) {
35+
public void onDrawLoadMore(Canvas c, RecyclerView parent) {
5736
//这里的画图简直要画死人(=.=#)
58-
//以后考虑换成动画形式
59-
37+
mProgress = mProgress + 5;
38+
if(mProgress == 100){
39+
mProgress = 0;
40+
}
6041
final int left = parent.getPaddingLeft() ;
6142
final int right = parent.getMeasuredWidth() - parent.getPaddingRight() ;
6243
final int childSize = parent.getChildCount() ;
6344
final View child = parent.getChildAt( childSize - 1 ) ;
6445
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
6546
final int top = child.getBottom() + layoutParams.bottomMargin ;
66-
final int bottom = top + mLoadMorePadding/2 ;
47+
final int bottom = top + getLoadMorePadding()/2 ;
6748
paint.setAntiAlias(true);// 抗锯齿
6849
paint.setFlags(Paint.ANTI_ALIAS_FLAG);// 增强消除锯齿
6950
paint.setColor(Color.GRAY);// 画笔为灰色
@@ -78,20 +59,6 @@ public void drawLoadmore(Canvas c, RecyclerView parent) {
7859
paint.setTextSize(40);// 设置文字的大小
7960
paint.setColor(Color.BLACK);// 设置画笔颜色
8061
c.drawText(mLoadMoreString, (right - left) / 2, bottom + 10, paint);
81-
8262
}
8363

84-
85-
/**
86-
* 过时方法,但是不得不用
87-
* @param outRect
88-
* @param itemPosition
89-
* @param parent
90-
*/
91-
@Override
92-
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
93-
if(itemPosition == parent.getAdapter().getItemCount() - 1) {
94-
outRect.set(0, 0, 0, mLoadMorePadding);
95-
}
96-
}
9764
}

0 commit comments

Comments
 (0)