Skip to content

Commit 991c326

Browse files
author
Alien
committed
optimize code
1 parent c330016 commit 991c326

14 files changed

Lines changed: 62 additions & 133 deletions

File tree

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ void addAll(List<T> data) {
7171
}
7272
}
7373

74-
7574
@UiThread
7675
public void setData(List<T> data) {
7776
this.mData = data;
7877
notifyDataSetChanged();
7978
}
8079

81-
8280
@UiThread
8381
public boolean removeItem(int position) {
8482
if (isEmpty()) {
@@ -92,7 +90,6 @@ public boolean removeItem(int position) {
9290
return false;
9391
}
9492

95-
9693
@UiThread
9794
public boolean clear() {
9895
if (mData != null) {
@@ -111,7 +108,6 @@ public T getItem(int position) {
111108
return null;
112109
}
113110

114-
115111
@Override
116112
public int getItemCount() {
117113
return getDataSize();
@@ -125,7 +121,6 @@ private int getDataSize() {
125121
return mData == null ? 0 : mData.size();
126122
}
127123

128-
129124
@NonNull
130125
@Override
131126
public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,12 @@ public void run() {
183183
mWrapperAdapter.loadCompleted(mHasMore);
184184
Toast.makeText(getContext(), "刷新完毕", Toast.LENGTH_SHORT).show();
185185
}
186-
}, 1000);
186+
}, 1500);
187187
}
188188
});
189189
}
190190

191191
private void setOnLoadMoreListener() {
192-
193192
mWrapperAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
194193
@Override
195194
public boolean canLoadMore() {

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

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -24,117 +24,21 @@ public class DensityUtils {
2424

2525
private static final float DOT_FIVE = 0.5f;
2626

27-
/**
28-
* dip to px
29-
*
30-
* @param context
31-
* @param dip
32-
* @return
33-
*/
3427
public static int dip2px(Context context, float dip) {
3528
float density = getDensity(context);
3629
return (int) (dip * density + DensityUtils.DOT_FIVE);
3730
}
3831

39-
/**
40-
* px to dip
41-
*
42-
* @param context
43-
* @param px
44-
* @return
45-
*/
46-
public static int px2dip(Context context, float px) {
47-
float density = getDensity(context);
48-
return (int) (px / density + DOT_FIVE);
49-
}
5032

5133
private static DisplayMetrics sDisplayMetrics;
5234

53-
54-
/**
55-
* get screen Size
56-
* @param context
57-
* @return int[] 长度2
58-
*/
59-
public static final int[] getDisplay(Context context) {
60-
initDisplayMetrics(context);
61-
62-
return new int[]{sDisplayMetrics.widthPixels, sDisplayMetrics.heightPixels};
63-
}
64-
65-
66-
/**
67-
* get screen width
68-
*
69-
* @param context
70-
* @return
71-
*/
72-
public static int getDisplayWidth(Context context) {
73-
initDisplayMetrics(context);
74-
return sDisplayMetrics.widthPixels;
75-
}
76-
77-
/**
78-
* get screen height
79-
*
80-
* @param context
81-
* @return
82-
*/
83-
public static int getDisplayHeight(Context context) {
84-
initDisplayMetrics(context);
85-
return sDisplayMetrics.heightPixels;
86-
}
87-
88-
/**
89-
* get screen density
90-
*
91-
* @param context
92-
* @return
93-
*/
9435
public static float getDensity(Context context) {
9536
initDisplayMetrics(context);
9637
return sDisplayMetrics.density;
9738
}
9839

99-
100-
/**
101-
* get screen density dpi
102-
*
103-
* @param context
104-
* @return
105-
*/
106-
public static int getDensityDpi(Context context) {
107-
initDisplayMetrics(context);
108-
return sDisplayMetrics.densityDpi;
109-
}
110-
111-
/**
112-
* init display metrics
113-
*
114-
* @param context
115-
*/
11640
private static synchronized void initDisplayMetrics(Context context) {
11741
sDisplayMetrics = context.getResources().getDisplayMetrics();
11842
}
11943

120-
121-
/**
122-
* is landscape
123-
*
124-
* @param context
125-
* @return
126-
*/
127-
public static boolean isLandscape(Context context) {
128-
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
129-
}
130-
131-
/**
132-
* is portrait
133-
*
134-
* @param context
135-
* @return
136-
*/
137-
public static boolean isPortrait(Context context) {
138-
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
139-
}
14044
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ztiany.loadmore;
22

3+
import android.annotation.SuppressLint;
34
import android.os.Bundle;
45
import android.view.Menu;
56
import android.view.MenuItem;
@@ -25,6 +26,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
2526
return super.onCreateOptionsMenu(menu);
2627
}
2728

29+
@SuppressLint("NonConstantResourceId")
2830
@Override
2931
public boolean onOptionsItemSelected(MenuItem item) {
3032
switch (item.getItemId()) {
@@ -53,4 +55,5 @@ private void setupFragment(Fragment fragment) {
5355
.replace(R.id.act_frag_container, fragment)
5456
.commit();
5557
}
58+
5659
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
import androidx.annotation.IdRes;
77
import androidx.recyclerview.widget.RecyclerView;
88

9-
10-
/**
11-
* Author Ztiany <br/>
12-
* Email ztiany3@gmail.com <br/>
13-
* Date 2016-03-19 20:02 <br/>
14-
* Description:
15-
*/
169
public abstract class ViewHolder<T> extends RecyclerView.ViewHolder {
1710

1811
ViewHolder(View itemView) {
@@ -29,9 +22,8 @@ protected Context getContext() {
2922
}
3023

3124
<V extends View> V findView(@IdRes int viewId) {
32-
@SuppressWarnings("unchecked")//需要什么类型,就返回什么类型
33-
V view = itemView.findViewById(viewId);
34-
return view;
25+
//需要什么类型,就返回什么类型
26+
return itemView.findViewById(viewId);
3527
}
3628

3729
}

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ android {
2626
}
2727

2828
dependencies {
29-
compileOnly 'androidx.recyclerview:recyclerview:1.1.0'
29+
compileOnly 'androidx.recyclerview:recyclerview:1.2.1'
3030
}
3131

3232
group='com.github.ztiany'

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,33 @@ public interface ILoadMore {
1212

1313
void setLoadMode(@LoadMode int loadMore);
1414

15+
/**
16+
* custom load-more item view.
17+
*/
1518
void setLoadMoreViewFactory(LoadMoreViewFactory factory);
1619

20+
/**
21+
* only works when use scrolling to trigger load-more.
22+
*/
23+
void setMinLoadMoreInterval(long minLoadMoreInterval);
24+
25+
/**
26+
* for preview load. only works when use scrolling to trigger load-more.
27+
*/
28+
void setLoadingTriggerThreshold(int threshold);
29+
30+
/**
31+
* @param autoHiddenWhenNoMore ture: set load-more view invisible when no more.
32+
* @see ILoadMore#setVisibilityWhenNoMore(int)
33+
*/
1734
void setAutoHiddenWhenNoMore(boolean autoHiddenWhenNoMore);
1835

1936
/**
20-
* @param visibility {@link android.view.View#VISIBLE},{@link android.view.View#INVISIBLE},{@link android.view.View#GONE}.
37+
* set load-more view's visibility when no more.
38+
*
39+
* @param visibility {@link android.view.View#VISIBLE}, {@link android.view.View#INVISIBLE} or {@link android.view.View#GONE}.
40+
* @see ILoadMore#setAutoHiddenWhenNoMore(boolean)
2141
*/
2242
void setVisibilityWhenNoMore(int visibility);
2343

24-
void setMinLoadMoreInterval(long minLoadMoreInterval);
25-
2644
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void setFullSpanForGird(GridLayoutManager gridLayoutManager) {
6161

6262
private static class InnerSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {
6363

64-
private GridLayoutManager.SpanSizeLookup mOriginSpanSizeLookup;
65-
private GridLayoutManager mGridLayoutManager;
64+
private final GridLayoutManager.SpanSizeLookup mOriginSpanSizeLookup;
65+
private final GridLayoutManager mGridLayoutManager;
6666
private final int mSpanCount;
6767

6868
InnerSpanSizeLookup(GridLayoutManager.SpanSizeLookup originSpanSizeLookup, GridLayoutManager gridLayoutManager) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ztiany.loadmore.adapter;
2+
3+
public class LoadMoreConfig {
4+
5+
static LoadMoreViewFactory sLoadMoreViewFactory;
6+
static long sMixLoadMoreInterval;
7+
8+
public static void setMixLoadMoreInterval(long mixLoadMoreInterval) {
9+
sMixLoadMoreInterval = mixLoadMoreInterval;
10+
}
11+
12+
public static void setDefaultLoadMoreViewFactory(LoadMoreViewFactory loadMoreViewFactory) {
13+
sLoadMoreViewFactory = loadMoreViewFactory;
14+
}
15+
16+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class LoadMoreImpl implements ILoadMore {
1313

1414
private OnLoadMoreListener mOnLoadMoreListener;
1515

16-
private LoadMoreViewFactory mLoadMoreViewFactory;
16+
private LoadMoreViewFactory mLoadMoreViewFactory = LoadMoreConfig.sLoadMoreViewFactory;
17+
private long mMixLoadMoreInterval = LoadMoreConfig.sMixLoadMoreInterval;
1718

1819
private final static int STATUS_NONE = 0;
1920
private final static int STATUS_LOADING = 1;
@@ -27,11 +28,10 @@ class LoadMoreImpl implements ILoadMore {
2728

2829
private long mPreviousTimeCallingLoadMore;
2930

30-
private long mMixLoadMoreInterval = 0;
31-
3231
private final boolean timeLimited;
3332

34-
@LoadMode private int mLoadMode = LoadMode.AUTO_LOAD;
33+
@LoadMode
34+
private int mLoadMode = LoadMode.AUTO_LOAD;
3535

3636
public LoadMoreImpl(boolean useScrollListener) {
3737
timeLimited = useScrollListener;
@@ -128,6 +128,11 @@ public void setMinLoadMoreInterval(long mixLoadMoreInterval) {
128128
mMixLoadMoreInterval = mixLoadMoreInterval;
129129
}
130130

131+
@Override
132+
public void setLoadingTriggerThreshold(int loadingTriggerThreshold) {
133+
//no op
134+
}
135+
131136
private void initLoadMoreView(ViewGroup parent) {
132137
if (mLoadMoreViewFactory == null) {
133138
mLoadMoreView = new LoadMoreView(parent.getContext());

0 commit comments

Comments
 (0)