22
33import android .content .Context ;
44import android .support .v4 .widget .SwipeRefreshLayout ;
5- import android .support .v7 .widget .GridLayoutManager ;
6- import android .support .v7 .widget .LinearLayoutManager ;
75import android .support .v7 .widget .RecyclerView ;
86import android .util .AttributeSet ;
9- import android .util .Log ;
107import android .view .LayoutInflater ;
118import android .view .View ;
129import android .view .ViewTreeObserver ;
1613import com .lhh .ptrrv .library .footer .LoadMoreFooter ;
1714import com .lhh .ptrrv .library .header .Header ;
1815import com .lhh .ptrrv .library .impl .PrvInterface ;
16+ import com .lhh .ptrrv .library .util .PullToRefreshRecyclerViewUtil ;
1917import com .nineoldandroids .view .ViewHelper ;
2018
2119/**
2220 * Created by linhonghong on 2015/11/11.
2321 */
2422public 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 ) {
0 commit comments