Skip to content

Commit 3d0cd21

Browse files
committed
fix load more bug
1 parent 1658245 commit 3d0cd21

4 files changed

Lines changed: 78 additions & 21 deletions

File tree

commonadapter-sample/src/main/java/com/github/zeng1990java/commonadapter_sample/MainActivity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.ArrayList;
1616
import java.util.List;
17+
import java.util.Random;
1718

1819
public class MainActivity extends AppCompatActivity {
1920

@@ -96,10 +97,17 @@ public void onLoadMore() {
9697
mHandler.postDelayed(new Runnable() {
9798
@Override
9899
public void run() {
99-
mCommonRecyclerAdapter.setIsHasLoadMore(false);
100-
mCommonRecyclerAdapter.addAll(getDatas());
100+
int random = new Random().nextInt(20);
101+
Toast.makeText(getApplicationContext(), "random: "+random, Toast.LENGTH_SHORT).show();
102+
if (random < 10){
103+
mCommonRecyclerAdapter.onLoadMoreError();
104+
}else {
105+
mCommonRecyclerAdapter.setIsHasLoadMore(true);
106+
mCommonRecyclerAdapter.addAll(getDatas());
107+
}
108+
101109
}
102-
}, 5000);
110+
}, 2000);
103111
}
104112
});
105113

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.zeng1990java.commonadapter_sample.widget;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.widget.FrameLayout;
6+
import android.widget.ProgressBar;
7+
import android.widget.TextView;
8+
9+
import com.github.zeng1990java.commonadapter.LoadMoreView;
10+
11+
/**
12+
* $desc
13+
*
14+
* @author zxb
15+
* @date 16/8/6 下午9:41
16+
*/
17+
public class CustomLoadMoreView extends FrameLayout implements LoadMoreView{
18+
19+
LoadState mLoadState = LoadState.IDLE;
20+
21+
private TextView mLoadText;
22+
private ProgressBar mProgressBar;
23+
24+
public CustomLoadMoreView(Context context, AttributeSet attrs) {
25+
super(context, attrs);
26+
}
27+
28+
@Override
29+
protected void onFinishInflate() {
30+
super.onFinishInflate();
31+
mLoadText = (TextView) findViewById(com.github.zeng1990java.commonadapter.R.id.loading_text);
32+
mProgressBar = (ProgressBar) findViewById(com.github.zeng1990java.commonadapter.R.id.loading_progress);
33+
}
34+
35+
@Override
36+
public LoadState getLoadState() {
37+
return mLoadState;
38+
}
39+
40+
@Override
41+
public void setLoadState(LoadState loadState) {
42+
mLoadState = loadState;
43+
if (mLoadState == LoadState.ERROR){
44+
mLoadText.setText("自定义加载失败");
45+
mProgressBar.setVisibility(GONE);
46+
}else {
47+
mLoadText.setText("自定义加载更多...");
48+
mProgressBar.setVisibility(VISIBLE);
49+
}
50+
}
51+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,40 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="match_parent"
4-
android:layout_height="56dp">
2+
<com.github.zeng1990java.commonadapter_sample.widget.CustomLoadMoreView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="56dp">
56

67
<LinearLayout
78
android:layout_width="wrap_content"
89
android:layout_height="wrap_content"
9-
android:orientation="horizontal"
1010
android:layout_gravity="center"
11+
android:orientation="horizontal"
1112
>
1213

1314
<ProgressBar
15+
android:id="@+id/loading_progress"
16+
style="@style/Widget.AppCompat.ProgressBar"
1417
android:layout_width="wrap_content"
1518
android:layout_height="wrap_content"
16-
android:minWidth="24dp"
17-
android:minHeight="24dp"
18-
android:maxWidth="24dp"
19-
android:maxHeight="24dp"
20-
style="@style/Widget.AppCompat.ProgressBar"
2119
android:layout_gravity="center_vertical"
20+
android:maxHeight="24dp"
21+
android:maxWidth="24dp"
22+
android:minHeight="24dp"
23+
android:minWidth="24dp"
2224
/>
2325

2426
<TextView
27+
android:id="@+id/loading_text"
2528
android:layout_width="wrap_content"
2629
android:layout_height="wrap_content"
27-
android:layout_marginLeft="5dp"
2830
android:layout_gravity="center_vertical"
31+
android:layout_marginLeft="5dp"
32+
android:text="自定义加载更多..."
2933
android:textColor="@android:color/black"
3034
android:textSize="16sp"
31-
android:text="自定义加载更多..."
3235
/>
3336

3437
</LinearLayout>
3538

3639

37-
</FrameLayout>
40+
</com.github.zeng1990java.commonadapter_sample.widget.CustomLoadMoreView>

commonadapter/src/main/java/com/github/zeng1990java/commonadapter/CommonRecyclerAdapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@ public void onAttachedToRecyclerView(RecyclerView recyclerView) {
196196
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
197197
@Override
198198
public int getSpanSize(int position) {
199-
if (isLoadMore(position)){
200-
if (mLoadState == LoadMoreView.LoadState.ERROR){
201-
mLoadState = LoadMoreView.LoadState.IDLE;
202-
}
203-
}
204199
if (isHeader(position) || isFooter(position) || isLoadMore(position)){
205200
return gridLayoutManager.getSpanCount();
206201
}
@@ -231,7 +226,7 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
231226
itemView.setOnClickListener(new View.OnClickListener() {
232227
@Override
233228
public void onClick(View v) {
234-
if (isHasLoadMore && !isLoadingMore() && mLoadState == LoadMoreView.LoadState.IDLE){
229+
if (isHasLoadMore && !isLoadingMore()){
235230
mLoadState = LoadMoreView.LoadState.LOADING;
236231
if (mOnLoadMoreListener != null){
237232
mOnLoadMoreListener.onLoadMore();

0 commit comments

Comments
 (0)