11package com .lhh .ptrrv .library ;
22
33import android .content .Context ;
4- import android .graphics .Canvas ;
5- import android .graphics .Color ;
6- import android .graphics .Paint ;
7- import android .graphics .Rect ;
8- import android .graphics .RectF ;
94import android .support .v4 .widget .SwipeRefreshLayout ;
5+ import android .support .v7 .widget .GridLayoutManager ;
106import android .support .v7 .widget .LinearLayoutManager ;
117import android .support .v7 .widget .RecyclerView ;
128import android .util .AttributeSet ;
@@ -27,27 +23,21 @@ public class PullToRefreshRecyclerView extends SwipeRefreshLayout implements Prv
2723
2824 private RecyclerView mRecyclerView ;
2925
30- private View mRootRelativeLayout ;//主view,包含footer,header等
31-
32- // private RelativeLayout mEmptyViewRelativeLayout;//内嵌view,包含recyclerview和emptyview等七七八八的东西
26+ // private View mRootRelativeLayout;//主view,包含footer,header等
3327
3428 private View mEmptyView ;
3529
36- // private View mHeaderView;
30+ private int mLoadMoreCount = 10 ; //default = 10
3731
3832 private boolean mIsSwipeEnable = false ;
3933
40- // private View mFooterView;
41-
42- // private View mLoadMoreView;//加载更多的view
43-
44- // private NestedScrollView mNestedScrollView;
45-
4634 private Context mContext ;
4735
4836 private BaseFooter mLoadMoreFooter ;
4937
50- private LinearLayoutManager mLinearLayoutManager ;
38+ // private RecyclerView.LayoutManager mLayoutManger;
39+
40+ // private LinearLayoutManager mLinearLayoutManager;
5141
5242 private LayoutParams mFullLayoutParams ;//全屏型lp
5343
@@ -99,23 +89,19 @@ private void setup(Context context){
9989
10090 private void initView (){
10191 //初始化布局
102- mRootRelativeLayout = LayoutInflater .from (mContext ).inflate (R .layout .ptrrv_root_view , null );
92+ mRecyclerView = ( RecyclerView ) LayoutInflater .from (mContext ).inflate (R .layout .ptrrv_root_view , null );
10393
104- this .addView (mRootRelativeLayout , mFullLayoutParams );
94+ this .addView (mRecyclerView , mFullLayoutParams );
10595
10696 this .setColorSchemeResources (R .color .swap_holo_green_bright , R .color .swap_holo_bule_bright ,
10797 R .color .swap_holo_green_bright , R .color .swap_holo_bule_bright );
10898
10999 //初始化loadmoreview
110100
111- // mNestedScrollView = (NestedScrollView)mRootRelativeLayout.findViewById(R.id.nsv);
112- //
113- // mEmptyViewRelativeLayout = (RelativeLayout)mRootRelativeLayout.findViewById(R.id.rlEmpty);
114-
115- mRecyclerView = (RecyclerView )mRootRelativeLayout .findViewById (R .id .recycler_view );
101+ // mRecyclerView = (RecyclerView)mRootRelativeLayout.findViewById(R.id.recycler_view);
116102
117- mLinearLayoutManager = new LinearLayoutManager (mContext );
118- mRecyclerView .setLayoutManager (mLinearLayoutManager );
103+ // mLinearLayoutManager = new LinearLayoutManager(mContext);
104+ // mRecyclerView.setLayoutManager(mLinearLayoutManager);
119105 mRecyclerView .setHasFixedSize (true );
120106
121107 if (!mIsSwipeEnable ) {
@@ -161,14 +147,7 @@ public void setPagingableListener(PullToRefreshRecyclerView.PagingableListener p
161147
162148 @ Override
163149 public void setEmptyView (View emptyView ) {
164- // if(mEmptyView != null) {
165- // //先把它移出去
166- // mEmptyViewRelativeLayout.removeView(mEmptyView);
167- // }
168150 mEmptyView = emptyView ;
169- // if(mEmptyView != null) {
170- // mEmptyViewRelativeLayout.addView(mEmptyView, mFullLayoutParams);
171- // }
172151 }
173152
174153 @ Override
@@ -199,30 +178,63 @@ public void addOnScrollListener(PullToRefreshRecyclerView.OnScrollListener onScr
199178 }
200179
201180 @ Override
202- public LinearLayoutManager getLinearLayoutManager () {
203- return mLinearLayoutManager ;
181+ public RecyclerView .LayoutManager getLayoutManager () {
182+ if (mRecyclerView != null ) {
183+ return mRecyclerView .getLayoutManager ();
184+ }
185+ return null ;
204186 }
205187
206188 @ Override
207189 public void onFinishLoading (boolean hasMoreItems , boolean needSetSelection ) {
208190 //当一页数据太少的时候,不用显示loadingview
209191 //临时修改,当一页数据太少的时候,不用显示loadingview
210- if (mLinearLayoutManager == null ){
192+ if (getLayoutManager () == null ){
211193 return ;
212194 }
213- if (mLinearLayoutManager .getItemCount () < 10 )
195+ if (getLayoutManager () .getItemCount () < mLoadMoreCount )
214196 hasMoreItems = false ;
215197
216198 setHasMoreItems (hasMoreItems );
217199
218200 isLoading = false ;
219201
220202 if (needSetSelection ) {
221- int first = mLinearLayoutManager . findFirstVisibleItemPosition ();
203+ int first = findFirstVisibleItemPosition ();
222204 mRecyclerView .scrollToPosition (--first );
223205 }
224206 }
225207
208+ public int findFirstVisibleItemPosition (){
209+ if (getLayoutManager () != null ) {
210+
211+ if (getLayoutManager () instanceof LinearLayoutManager ) {
212+ return ((LinearLayoutManager ) getLayoutManager ()).findFirstVisibleItemPosition ();
213+ }
214+
215+ if (getLayoutManager () instanceof GridLayoutManager ) {
216+ return ((GridLayoutManager ) getLayoutManager ()).findFirstVisibleItemPosition ();
217+ }
218+
219+ }
220+ return RecyclerView .NO_POSITION ;
221+ }
222+
223+ public int findLastVisibleItemPosition (){
224+ if (getLayoutManager () != null ) {
225+
226+ if (getLayoutManager () instanceof LinearLayoutManager ) {
227+ return ((LinearLayoutManager ) getLayoutManager ()).findLastVisibleItemPosition ();
228+ }
229+
230+ if (getLayoutManager () instanceof GridLayoutManager ) {
231+ return ((GridLayoutManager ) getLayoutManager ()).findLastVisibleItemPosition ();
232+ }
233+
234+ }
235+ return RecyclerView .NO_POSITION ;
236+ }
237+
226238 @ Override
227239 public void setSwipeEnable (boolean enable ) {
228240 //just like extra setEnable(boolean).这里和外部一样的,不过这里控制更方便super.setenable
@@ -240,6 +252,18 @@ public RecyclerView getRecyclerView() {
240252 return this .mRecyclerView ;
241253 }
242254
255+ @ Override
256+ public void setLayoutManager (RecyclerView .LayoutManager layoutManager ) {
257+ if (mRecyclerView != null ){
258+ mRecyclerView .setLayoutManager (layoutManager );
259+ }
260+ }
261+
262+ @ Override
263+ public void setLoadMoreCount (int count ) {
264+ mLoadMoreCount = count ;
265+ }
266+
243267 private void setHasMoreItems (boolean hasMoreItems ) {
244268 this .hasMoreItems = hasMoreItems ;
245269 if (mLoadMoreFooter == null ){
@@ -270,25 +294,30 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
270294 @ Override
271295 public void onScrolled (RecyclerView recyclerView , int dx , int dy ) {
272296 super .onScrolled (recyclerView , dx , dy );
273- //在向下回调之前做处理
297+ //在向下回调之前做处理,do before callback
298+ if (getLayoutManager () == null ){
299+ //here layoutManager is null
300+ return ;
301+ }
274302
275303 int firstVisibleItem , visibleItemCount , totalItemCount , lastVisibleItem ;
276- visibleItemCount = mLinearLayoutManager .getChildCount ();
277- totalItemCount = mLinearLayoutManager .getItemCount ();
278- firstVisibleItem = mLinearLayoutManager . findFirstVisibleItemPosition ();
279- lastVisibleItem = mLinearLayoutManager . findLastVisibleItemPosition ();//有可能最后一项实在太大了,没办法完全显示
304+ visibleItemCount = getLayoutManager () .getChildCount ();
305+ totalItemCount = getLayoutManager () .getItemCount ();
306+ firstVisibleItem = findFirstVisibleItemPosition ();
307+ lastVisibleItem = findLastVisibleItemPosition ();//有可能最后一项实在太大了,没办法完全显示
280308// lastVisibleItem = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
281309
282310 if (mIsSwipeEnable ) {
283- if (mLinearLayoutManager .findFirstCompletelyVisibleItemPosition () != 0 ) {
311+ if (findFirstVisibleItemPosition () != 0 ) {
312+ //这里还有个bug,如果用findFirstCompletelyVisibleItemPosition会产生如果第一条太大,没办法完全显示则无法下啦刷新
284313 //如果不是第一条可见就不让下拉,要不然会出现很严重的到处都能下拉的问题
285314 PullToRefreshRecyclerView .this .setEnabled (false );
286315 } else {
287316 PullToRefreshRecyclerView .this .setEnabled (true );
288317 }
289318 }
290319
291- if (totalItemCount < 10 ){
320+ if (totalItemCount < mLoadMoreCount ){
292321 setHasMoreItems (false );
293322 isLoading = false ;
294323 }else if (!isLoading && hasMoreItems && ((lastVisibleItem + 1 ) == totalItemCount )) {
0 commit comments