Skip to content

Commit 7bd7eca

Browse files
author
ztiany
committed
优化加载更多逻辑
1 parent 1bd5cea commit 7bd7eca

14 files changed

Lines changed: 135 additions & 72 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ android {
77
defaultConfig {
88
applicationId "com.ztiany.loadmore"
99
minSdkVersion 15
10+
//noinspection OldTargetApi
1011
targetSdkVersion 28
1112
versionCode 1
1213
versionName "1.0"
@@ -26,8 +27,9 @@ android {
2627
dependencies {
2728
implementation fileTree(dir: 'libs', include: ['*.jar'])
2829
implementation project(':library')
29-
implementation 'androidx.fragment:fragment:1.0.0'
30-
implementation 'androidx.appcompat:appcompat:1.0.0'
31-
implementation 'androidx.recyclerview:recyclerview:1.0.0'
30+
implementation 'androidx.fragment:fragment:1.2.5'
31+
implementation 'androidx.appcompat:appcompat:1.2.0'
32+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
3233
implementation 'androidx.cardview:cardview:1.0.0'
34+
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
3335
}

app/src/main/java/com/ztiany/loadmore/DemoFragment.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ public class DemoFragment extends BaseLayoutFragment {
4444
private static final String CLICK_LOAD_MORE = "isClickLoadMore";
4545
private static final String AUTO_HIDDEN_MORE = "auto_hidden_more";
4646

47-
public static DemoFragment newInstance(int layoutType, boolean isClickLoadMore, boolean autoHidden) {
47+
public static DemoFragment newInstance(int layoutType, boolean isClickLoadMore, int visibilityWhenNoMore) {
4848
Bundle args = new Bundle();
4949
args.putInt(LAYOUT_TYPE, layoutType);
5050
args.putBoolean(CLICK_LOAD_MORE, isClickLoadMore);
51-
args.putBoolean(AUTO_HIDDEN_MORE, autoHidden);
51+
args.putInt(AUTO_HIDDEN_MORE, visibilityWhenNoMore);
5252
DemoFragment fragment = new DemoFragment();
5353
fragment.setArguments(args);
5454
return fragment;
5555
}
5656

5757
private RecyclerView.LayoutManager createLayoutManager() {
58+
Bundle arguments = getArguments();
59+
assert arguments != null;
5860
int type = getArguments().getInt(LAYOUT_TYPE);
5961
if (type == 1) {
6062
return new LinearLayoutManager(getContext());
@@ -66,7 +68,7 @@ private RecyclerView.LayoutManager createLayoutManager() {
6668
}
6769

6870
public void onButtonClick(View v) {
69-
PopupMenu pop = new PopupMenu(getContext(), v);
71+
PopupMenu pop = new PopupMenu(requireContext(), v);
7072
Menu menu = pop.getMenu();
7173
menu.add(Menu.NONE, 1, 0, "next time fail");
7274
menu.add(Menu.NONE, 2, 1, "next time no more");
@@ -105,7 +107,7 @@ protected int provideLayoutRes() {
105107
}
106108

107109
@Override
108-
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
110+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
109111
super.onViewCreated(view, savedInstanceState);
110112
view.findViewById(R.id.frag_show_option).setOnClickListener(new View.OnClickListener() {
111113
@Override
@@ -127,7 +129,12 @@ public void onActivityCreated(Bundle savedInstanceState) {
127129
mRecyclerView.setLayoutManager(createLayoutManager());
128130
mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
129131
@Override
130-
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
132+
public void getItemOffsets(
133+
@NonNull Rect outRect, @NonNull
134+
View view,
135+
@NonNull RecyclerView parent,
136+
@NonNull RecyclerView.State state
137+
) {
131138
outRect.bottom = DensityUtils.dip2px(getContext(), 10);
132139
super.getItemOffsets(outRect, view, parent, state);
133140
}
@@ -136,12 +143,14 @@ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, Recycle
136143
mWrapperAdapter = WrapperAdapter.wrap(mRecyclerAdapter);
137144
mRecyclerView.setAdapter(mWrapperAdapter);
138145

139-
if (getArguments().getBoolean(CLICK_LOAD_MORE)) {
146+
Bundle arguments = getArguments();
147+
assert arguments != null;
148+
149+
if (arguments.getBoolean(CLICK_LOAD_MORE)) {
140150
mWrapperAdapter.setLoadMode(LoadMode.CLICK_LOAD);
141151
}
142-
if (getArguments().getBoolean(AUTO_HIDDEN_MORE)) {
143-
mWrapperAdapter.setAutoHiddenWhenNoMore(true);
144-
}
152+
153+
mWrapperAdapter.setVisibilityWhenNoMore(getArguments().getInt(AUTO_HIDDEN_MORE, View.VISIBLE));
145154

146155
setOnLoadMoreListener();
147156

@@ -189,6 +198,7 @@ public void run() {
189198
mWrapperAdapter.loadFail();
190199
return;
191200
}
201+
192202
if (!mHasMore) {
193203
mWrapperAdapter.loadCompleted(false);
194204
return;
@@ -218,7 +228,7 @@ public void run() {
218228
"新来的Item" + count++));
219229
mWrapperAdapter.loadCompleted(true);
220230
}
221-
}, 1000);
231+
}, 100);
222232
}
223233
});
224234

app/src/main/java/com/ztiany/loadmore/MainActivity.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.os.Bundle;
44
import android.view.Menu;
55
import android.view.MenuItem;
6+
import android.view.View;
67

78
import androidx.appcompat.app.AppCompatActivity;
89
import androidx.fragment.app.Fragment;
@@ -14,7 +15,7 @@ protected void onCreate(Bundle savedInstanceState) {
1415
super.onCreate(savedInstanceState);
1516
setContentView(R.layout.activity_main);
1617
if (savedInstanceState == null) {
17-
setupFragment(DemoFragment.newInstance(1, false, false));
18+
setupFragment(DemoFragment.newInstance(1, false, View.VISIBLE));
1819
}
1920
}
2021

@@ -28,16 +29,19 @@ public boolean onCreateOptionsMenu(Menu menu) {
2829
public boolean onOptionsItemSelected(MenuItem item) {
2930
switch (item.getItemId()) {
3031
case R.id.menu_grid:
31-
setupFragment(DemoFragment.newInstance(2, false, true));
32+
setupFragment(DemoFragment.newInstance(2, false, View.VISIBLE));
3233
break;
3334
case R.id.menu_staggered:
34-
setupFragment(DemoFragment.newInstance(3, true, false));
35+
setupFragment(DemoFragment.newInstance(3, true, View.VISIBLE));
3536
break;
3637
case R.id.menu_linear:
37-
setupFragment(DemoFragment.newInstance(1, false, false));
38+
setupFragment(DemoFragment.newInstance(1, false, View.VISIBLE));
3839
break;
39-
case R.id.menu_linear_auto_hide:
40-
setupFragment(DemoFragment.newInstance(1, true, true));
40+
case R.id.menu_linear_auto_invisible:
41+
setupFragment(DemoFragment.newInstance(1, true, View.INVISIBLE));
42+
break;
43+
case R.id.menu_linear_auto_gone:
44+
setupFragment(DemoFragment.newInstance(1, false, View.GONE));
4145
break;
4246
}
4347
return true;
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools">
3+
xmlns:tools="http://schemas.android.com/tools">
44

55
<item
66
android:id="@+id/menu_staggered"
77
android:title="staggered click load"
8-
tools:ignore="HardcodedText"/>
8+
tools:ignore="HardcodedText" />
99

1010
<item
1111
android:id="@+id/menu_grid"
1212
android:title="grid"
13-
tools:ignore="HardcodedText"/>
13+
tools:ignore="HardcodedText" />
1414

1515
<item
1616
android:id="@+id/menu_linear"
17-
android:title="linear"/>
17+
android:title="linear" />
1818

1919
<item
20-
android:id="@+id/menu_linear_auto_hide"
21-
android:title="linear auto hide"/>
20+
android:id="@+id/menu_linear_auto_invisible"
21+
android:title="linear auto invisible" />
22+
23+
<item
24+
android:id="@+id/menu_linear_auto_gone"
25+
android:title="linear auto gone" />
2226

2327
</menu>

library/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ android {
66

77
defaultConfig {
88
minSdkVersion 15
9+
//noinspection OldTargetApi
910
targetSdkVersion 28
1011
versionCode 1
1112
versionName "1.0"
@@ -22,8 +23,9 @@ android {
2223
abortOnError false
2324
}
2425
}
26+
2527
dependencies {
26-
compileOnly 'androidx.recyclerview:recyclerview:1.0.0'
28+
compileOnly 'androidx.recyclerview:recyclerview:1.1.0'
2729
}
2830

2931
group='com.github.ztiany'

library/src/main/java/com/ztiany/loadmore/adapter/AdapterInterface.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ public interface AdapterInterface {
1313
* @param rv RecyclerView
1414
*/
1515
void setItemFullSpan(View itemView, RecyclerView rv);
16+
1617
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ztiany.loadmore.adapter;
2+
3+
import androidx.annotation.IntDef;
4+
5+
/**
6+
* @author Ztiany
7+
* Email: ztiany3@gmail.com
8+
* Date : 2020-11-30 15:04
9+
*/
10+
@IntDef({
11+
Direction.UP,
12+
Direction.DOWN
13+
})
14+
@interface Direction {
15+
int UP = 1;
16+
int DOWN = 2;
17+
}

library/src/main/java/com/ztiany/loadmore/adapter/ILoadMore.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ public interface ILoadMore {
1515
void setLoadMoreViewFactory(LoadMoreViewFactory factory);
1616

1717
void setAutoHiddenWhenNoMore(boolean autoHiddenWhenNoMore);
18+
19+
/**
20+
* @param visibility {@link android.view.View#VISIBLE},{@link android.view.View#INVISIBLE},{@link android.view.View#GONE}.
21+
*/
22+
void setVisibilityWhenNoMore(int visibility);
23+
1824
}

library/src/main/java/com/ztiany/loadmore/adapter/ILoadMoreView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public interface ILoadMoreView {
1010
void onCompleted(boolean hasMore);
1111

1212
void onClickLoad();
13+
1314
}

library/src/main/java/com/ztiany/loadmore/adapter/KeepFullSpanUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import androidx.recyclerview.widget.RecyclerView;
88
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
99

10-
11-
class KeepFullSpanUtils {
10+
final class KeepFullSpanUtils {
1211

1312
private InnerSpanSizeLookup mInnerSpanSizeLookup;
13+
1414
GridLayoutManager.SpanSizeLookup mOriginSpanSizeLookup;
1515

1616
KeepFullSpanUtils() {
@@ -80,4 +80,5 @@ public int getSpanSize(int position) {
8080
return mOriginSpanSizeLookup.getSpanSize(position);
8181
}
8282
}
83+
8384
}

0 commit comments

Comments
 (0)