Skip to content

Commit b93d2e0

Browse files
authored
Merge pull request #13 from AlanCheen/develop
release 0.9.0 feature
2 parents d7c2ea5 + d4c97fb commit b93d2e0

File tree

8 files changed

+94
-13
lines changed

8 files changed

+94
-13
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# Flap
22

3-
[![Download](https://api.bintray.com/packages/alancheen/maven/flap/images/download.svg?version=0.6.0)](https://bintray.com/alancheen/maven/flap/0.6.0/link) [![Build Status](https://travis-ci.org/AlanCheen/Flap.svg?branch=master)](https://travis-ci.org/AlanCheen/Flap) ![RecyclerView](https://img.shields.io/badge/RecyclerView-28.0.0-brightgreen.svg) ![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat) [![license](https://img.shields.io/github/license/AlanCheen/Flap.svg)](./LICENSE)
3+
[![Download](https://api.bintray.com/packages/alancheen/maven/flap/images/download.svg?version=0.9.0)](https://bintray.com/alancheen/maven/flap/0.9.0/link) [![Build Status](https://travis-ci.org/AlanCheen/Flap.svg?branch=master)](https://travis-ci.org/AlanCheen/Flap) ![RecyclerView](https://img.shields.io/badge/RecyclerView-28.0.0-brightgreen.svg) ![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat) [![license](https://img.shields.io/github/license/AlanCheen/Flap.svg)](./LICENSE)
44

55
**[WIP]WARNING: Flap is still under development.**
66

7-
`Flap` is a library that makes `RecyclerView.Adapter` more easier to use , especially when you have to support lots of different type ViewHolders.
7+
`Flap` is a library that makes `RecyclerView.Adapter` much more easier to use , by keeping you from writing boilerplate codes and providing lots advance features , especially when you have to support lots of different type items.
88

9-
Flap can save your day by keeping you from writing boilerplate codes.
10-
11-
Have a try , thanks !
9+
Have a try !
1210

1311
## Integrate Flap
1412

app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public void onCreate() {
2828

2929
Log.e("Flap", "Init Flap time cost :" + (t2 - t1));
3030

31+
Flap.getDefault().getFlapItemPool().setMaxRecycledViews(new SimpleImageItem.Factory().getItemViewType(null), 8);
32+
3133
}
3234
}

app/src/main/java/me/yifeiyuan/flapdev/MainActivity.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ protected void onCreate(Bundle savedInstanceState) {
2020

2121
RecyclerView recyclerView = findViewById(R.id.rv_items);
2222

23+
createSimpleTestCase(recyclerView);
24+
25+
createAdvanceTestCase(recyclerView);
26+
}
27+
28+
private void createSimpleTestCase(final RecyclerView recyclerView) {
2329
FlapAdapter adapter = new FlapAdapter();
2430

2531
List<Object> models = new ArrayList<>();
@@ -36,8 +42,19 @@ protected void onCreate(Bundle savedInstanceState) {
3642
adapter.setModels(models);
3743

3844
recyclerView.setAdapter(adapter);
45+
}
46+
47+
private void createAdvanceTestCase(final RecyclerView recyclerView) {
3948

40-
// List<Object> models = mockModels();
49+
List<Object> models = mockModels();
50+
51+
FlapAdapter adapter = new FlapAdapter();
52+
adapter.setUseGlobalPool(true)
53+
.setLifecycleEnable(true)
54+
.setLifecycleOwner(this)
55+
.setModels(models);
56+
57+
recyclerView.setAdapter(adapter);
4158
}
4259

4360
private List<Object> mockModels() {

flap/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies {
3333

3434
apply plugin: "guru.stefma.bintrayrelease"
3535

36-
version = "0.7.0"
36+
version = "0.9.0"
3737
group = "me.yifeiyuan.flap"
3838
androidArtifact {
3939
artifactId = "flap"

flap/src/main/java/me/yifeiyuan/flap/Flap.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public final class Flap implements IFlap {
2424
private final Map<Class<?>, FlapItemFactory> itemFactories;
2525
private final SparseArray<FlapItemFactory> factoryMapping;
2626

27+
private final FlapItemPool GLOBAL_POOL = new FlapItemPool();
28+
2729
private static volatile Flap sInstance;
2830

2931
public static Flap getDefault() {
@@ -115,6 +117,21 @@ public void onBindViewHolder(@NonNull final FlapItem holder, final Object model,
115117
holder.bind(model, flapAdapter, payloads);
116118
}
117119

120+
@Override
121+
public void onViewAttachedToWindow(@NonNull final FlapItem holder, @NonNull final FlapAdapter flapAdapter) {
122+
holder.onViewAttachedToWindow(flapAdapter);
123+
}
124+
125+
@Override
126+
public void onViewDetachedFromWindow(@NonNull final FlapItem holder, @NonNull final FlapAdapter flapAdapter) {
127+
holder.onViewDetachedFromWindow(flapAdapter);
128+
}
129+
130+
@Override
131+
public void onViewRecycled(@NonNull final FlapItem holder, @NonNull final FlapAdapter flapAdapter) {
132+
holder.onViewRecycled(flapAdapter);
133+
}
134+
118135
@NonNull
119136
@Override
120137
public FlapItem onCreateDefaultViewHolder(@NonNull final LayoutInflater inflater, @NonNull final ViewGroup parent, final int viewType) {
@@ -124,4 +141,8 @@ public FlapItem onCreateDefaultViewHolder(@NonNull final LayoutInflater inflater
124141
public static void setDebug(final boolean isDebugging) {
125142
FlapDebug.setDebug(isDebugging);
126143
}
144+
145+
public FlapItemPool getFlapItemPool() {
146+
return GLOBAL_POOL;
147+
}
127148
}

flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public class FlapAdapter extends RecyclerView.Adapter<FlapItem> {
2020
@NonNull
2121
private final Flap flap = Flap.getDefault();
2222

23-
private LifecycleOwner lifecycleOwner;
24-
2523
@NonNull
2624
private List<?> models = new ArrayList<>();
2725

26+
private LifecycleOwner lifecycleOwner;
27+
2828
private boolean lifecycleEnable = true;
2929

30+
private boolean useGlobalPool = true;
31+
3032
@NonNull
3133
@Override
3234
public FlapItem onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
@@ -72,24 +74,27 @@ public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) {
7274
if (recyclerView.getContext() instanceof LifecycleOwner && lifecycleOwner == null) {
7375
setLifecycleOwner((LifecycleOwner) recyclerView.getContext());
7476
}
77+
if (useGlobalPool) {
78+
recyclerView.setRecycledViewPool(flap.getFlapItemPool());
79+
}
7580
}
7681

7782
@Override
7883
public void onViewAttachedToWindow(@NonNull FlapItem holder) {
7984
super.onViewAttachedToWindow(holder);
80-
holder.onViewAttachedToWindow(this);
85+
flap.onViewAttachedToWindow(holder, this);
8186
}
8287

8388
@Override
8489
public void onViewDetachedFromWindow(@NonNull FlapItem holder) {
8590
super.onViewDetachedFromWindow(holder);
86-
holder.onViewDetachedFromWindow(this);
91+
flap.onViewDetachedFromWindow(holder, this);
8792
}
8893

8994
@Override
9095
public void onViewRecycled(@NonNull final FlapItem holder) {
9196
super.onViewRecycled(holder);
92-
holder.onViewRecycled(this);
97+
flap.onViewRecycled(holder, this);
9398
}
9499

95100
@Override
@@ -115,13 +120,29 @@ private Object getModel(final int position) {
115120
return getModels().get(position);
116121
}
117122

118-
public void setModels(@NonNull List<?> models) {
123+
public FlapAdapter setModels(@NonNull List<?> models) {
119124
checkNotNull(models, "models can't be null here");
120125
this.models = models;
126+
return this;
121127
}
122128

123129
@NonNull
124130
public List<?> getModels() {
125131
return models;
126132
}
133+
134+
/**
135+
* Set whether use the global RecycledViewPool or not.
136+
*
137+
* NOTE : Call this before you call RecyclerView.setAdapter.
138+
*
139+
* @param enable true by default
140+
*
141+
* @return this
142+
*/
143+
public FlapAdapter setUseGlobalPool(final boolean enable) {
144+
this.useGlobalPool = enable;
145+
return this;
146+
}
147+
127148
}

flap/src/main/java/me/yifeiyuan/flap/FlapAdapterDelegate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/**
1010
* Created by 程序亦非猿 on 2019/1/2.
11+
*
12+
* A delegate of FlapAdapter.
1113
*/
1214
interface FlapAdapterDelegate {
1315

@@ -17,4 +19,10 @@ interface FlapAdapterDelegate {
1719
FlapItem onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, int viewType);
1820

1921
void onBindViewHolder(@NonNull final FlapItem holder, Object model, @NonNull FlapAdapter flapAdapter, @NonNull List<Object> payloads);
22+
23+
void onViewAttachedToWindow(@NonNull FlapItem holder,@NonNull FlapAdapter flapAdapter);
24+
25+
void onViewDetachedFromWindow(@NonNull FlapItem holder,@NonNull FlapAdapter flapAdapter);
26+
27+
void onViewRecycled(@NonNull FlapItem holder,@NonNull FlapAdapter flapAdapter);
2028
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package me.yifeiyuan.flap;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
5+
/**
6+
* Created by 程序亦非猿 on 2019/1/2.
7+
*
8+
* A global RecycledViewPool that can be shared among RecyclerViews , which is enabled by default.
9+
*
10+
* @see FlapAdapter#setUseGlobalPool(boolean)
11+
*/
12+
public class FlapItemPool extends RecyclerView.RecycledViewPool {
13+
14+
}

0 commit comments

Comments
 (0)