Skip to content

Commit 6abae92

Browse files
committed
增加对gridview的支持,修复bug
1 parent 2612c82 commit 6abae92

File tree

10 files changed

+86
-52
lines changed

10 files changed

+86
-52
lines changed

Library/Library.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@
8787
</content>
8888
<orderEntry type="jdk" jdkName="Android API 22 Platform (1)" jdkType="Android SDK" />
8989
<orderEntry type="sourceFolder" forTests="false" />
90-
<orderEntry type="library" exported="" name="appcompat-v7-22.1.1" level="project" />
9190
<orderEntry type="library" exported="" name="support-v4-22.2.1" level="project" />
9291
<orderEntry type="library" exported="" name="recyclerview-v7-22.2.1" level="project" />
9392
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
93+
<orderEntry type="library" exported="" name="appcompat-v7-22.1.1" level="project" />
9494
</component>
9595
</module>
7.49 KB
Binary file not shown.
185 Bytes
Binary file not shown.
6.3 KB
Binary file not shown.
188 Bytes
Binary file not shown.

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

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package com.lhh.ptrrv.library;
22

33
import 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;
94
import android.support.v4.widget.SwipeRefreshLayout;
5+
import android.support.v7.widget.GridLayoutManager;
106
import android.support.v7.widget.LinearLayoutManager;
117
import android.support.v7.widget.RecyclerView;
128
import 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)) {

Library/src/com/lhh/ptrrv/library/impl/PrvInterface.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public interface PrvInterface{
1818
public void setHeader(View view);
1919
public void setFooter(View view);
2020
public void addOnScrollListener(PullToRefreshRecyclerView.OnScrollListener onScrollLinstener);
21-
public LinearLayoutManager getLinearLayoutManager();
21+
public RecyclerView.LayoutManager getLayoutManager();
2222
public void onFinishLoading(boolean hasMoreItems, boolean needSetSelection);
2323
public void setSwipeEnable(boolean enable);//设置是否可以下拉
2424
public boolean isSwipeEnable();//返回当前组件是否可以下拉
2525
public RecyclerView getRecyclerView();
26+
public void setLayoutManager(RecyclerView.LayoutManager layoutManager);
27+
public void setLoadMoreCount(int count);//如果不达到count数量不让加载更多
2628
public void release();
2729
}

app/app.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090
</content>
9191
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
9292
<orderEntry type="sourceFolder" forTests="false" />
93-
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
94-
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
9593
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
94+
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
95+
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
9696
<orderEntry type="module" module-name="Library" exported="" />
9797
</component>
9898
</module>

app/src/main/java/com/linhh/ptrrv/android_pulltorefreshrecyclerview/MainActivity.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@
66
import android.support.v4.widget.SwipeRefreshLayout;
77
import android.support.v7.app.AppCompatActivity;
88
import android.os.Bundle;
9+
import android.support.v7.widget.LinearLayoutManager;
910
import android.support.v7.widget.RecyclerView;
1011
import android.view.View;
1112
import android.view.ViewGroup;
1213
import android.widget.Toast;
1314

1415
import com.lhh.ptrrv.library.PullToRefreshRecyclerView;
15-
16+
/**
17+
* Created by linhonghong on 2015/11/13.
18+
*/
1619
public class MainActivity extends AppCompatActivity {
1720

1821
private PullToRefreshRecyclerView mPtrrv;
1922
private PtrrvAdapter mAdapter;
20-
private static final int DEFAULT_ITEM_SIZE = 20;
21-
private static final int ITEM_SIZE_OFFSET = 20;
23+
private static final int DEFAULT_ITEM_SIZE = 50;
24+
private static final int ITEM_SIZE_OFFSET = 50;
2225

2326
private static final int MSG_CODE_REFRESH = 0;
2427
private static final int MSG_CODE_LOADMORE = 1;
@@ -36,6 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3639
private void findViews(){
3740
mPtrrv = (PullToRefreshRecyclerView) this.findViewById(R.id.ptrrv);
3841
mPtrrv.setSwipeEnable(true);//open swipe
42+
mPtrrv.setLayoutManager(new LinearLayoutManager(this));
3943
mPtrrv.setPagingableListener(new PullToRefreshRecyclerView.PagingableListener() {
4044
@Override
4145
public void onLoadMoreItems() {

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
android:paddingRight="@dimen/activity_horizontal_margin"
66
android:paddingTop="@dimen/activity_vertical_margin"
77
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
8-
98
<com.lhh.ptrrv.library.PullToRefreshRecyclerView
109
android:id="@+id/ptrrv"
1110
android:layout_width="match_parent"

0 commit comments

Comments
 (0)