Skip to content

Commit

Permalink
Merge pull request #10 from AlanCheen/develop
Browse files Browse the repository at this point in the history
Develop 0.6.0 feature
  • Loading branch information
AlanCheen authored Dec 13, 2018
2 parents e830813 + 8872075 commit bd812c3
Show file tree
Hide file tree
Showing 25 changed files with 411 additions and 183 deletions.
Empty file removed CHANGELOG.md
Empty file.
60 changes: 38 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Flap

[ ![Download](https://api.bintray.com/packages/alancheen/maven/flap/images/download.svg?version=0.5.0) ](https://bintray.com/alancheen/maven/flap/0.5.0/link) [![Build Status](https://travis-ci.org/AlanCheen/Flap.svg?branch=master)](https://travis-ci.org/AlanCheen/Flap)
[![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)

WARNING: Flap is still under development.
**WARNING: Flap is still under development.**

Flap is an library that makes RecyclerView.Adapter more easier to use , especially when you have to support lots of different type ViewHolders.
Flap is a library that makes `RecyclerView.Adapter` more easier to use , especially when you have to support lots of different type ViewHolders.

Flap can save your day by keeping you from writing boilerplate codes.

Expand All @@ -25,7 +25,7 @@ dependencies {

#### Step 1 : Create a model class :

A model class can be a POJO or Java bean.
A model class can be a POJO or Java Bean.

```java
public class SimpleTextModel {
Expand All @@ -39,17 +39,19 @@ public class SimpleTextModel {
}
```

#### Step 2 : Create a `FlapItem` and `FlapItemFactory` :
#### Step 2 : Create a `FlapItem` and a `FlapItemFactory` :

`FlapItem` is a base `ViewHolder` that Flap is using which provides useful methods.
`FlapItem` is the base `ViewHolder` that `Flap` is used internally.

`FlapItemFactory` tells Flap the layout res id which is used when creating a `FlapItem`.
`FlapItemFactory` tells Flap how to create a `FlapItem` as you wish.

Here is a sample :

```java
public class SimpleTextItem extends FlapItem<SimpleTextModel> {

private static final String TAG = "SimpleTextItem";

private TextView tvContent;

public SimpleTextItem(final View itemView) {
Expand All @@ -58,64 +60,64 @@ public class SimpleTextItem extends FlapItem<SimpleTextModel> {
}

@Override
protected void onBind(final SimpleTextModel model) {
protected void onBind(@NonNull final SimpleTextModel model, @NonNull final FlapAdapter adapter, @NonNull final List<Object> payloads) {
tvContent.setText(model.content);
}

public static class SimpleTextItemFactory extends FlapItemFactory<SimpleTextModel, SimpleTextItem> {
public static class SimpleTextItemFactory extends LayoutItemFactory<SimpleTextModel, SimpleTextItem> {

@Override
protected int getLayoutResId(final SimpleTextModel model) {
return R.layout.flap_item_simple_text;
}

}

}
```

#### Step 3 : Create a `FlapAdapter` and register the `FlapItemFactory`


#### Step 3 : Register the `FlapItemFactory` and create your `FlapAdapter`

Create your `FlapAdapter` and register the `SimpleTextItemFactory` that we already created , setup the models :

```java
RecyclerView recyclerView = findViewById(R.id.rv_items);
//register your ItemFactory to Flap
Flap.getDefault().register(SimpleTextModel.class, new SimpleTextItem.SimpleTextItemFactory());

FlapAdapter adapter = new FlapAdapter();

adapter.registerItemFactory(new SimpleTextItemFactory());

List<Object> models = new ArrayList<>();

models.add(new SimpleTextModel("Android"));
models.add(new SimpleTextModel("Java"));
models.add(new SimpleTextModel("Kotlin"));

//set your models to FlapAdapter
adapter.setModels(models);

recyclerView.setAdapter(adapter);
```

You are good to go!
Yeah , you are good to go!

![](art/flap-simple-showcase.png)

## More Feature

Flap adds some features for `FlapItem` :

1. Access a context directly by field `context`
2. Call `findViewById()` instead of `itemView.findViewById`
1. Access a context directly by field `context`.
2. Call `findViewById()` directlly instead of `itemView.findViewById` when you want to find a view.
3. Override `onViewAttachedToWindow` & `onViewDetachedFromWindow` so that you can do something like pausing or resuming a video.

What's more , here are some methods for you that you can override if you need :

1. Override `onBind(final T model, final FlapAdapter adapter, final List<Object> payloads)` when you wanna access your adapter or payloads.
2. Override `onViewAttachedToWindow` & `onViewDetachedFromWindow` so that you can do something like pausing or resuming a video.

### Enable Lifecycle


### Enable Lifecycle

By extending `LifecycleItem` , a lifecycle aware `ViewHolder` , you can get the lifecycle callbacks : `onResume``onPause``onStop``onDestroy` when you care about the lifecycle , `FlapAdapter` binds the `LifecycleOwner` automatically.
By extending `LifecycleItem` , a lifecycle aware `ViewHolder` , you can get the lifecycle callbacks : `onResume``onPause``onStop``onDestroy` by default , when you care about the lifecycle , `FlapAdapter` binds the `LifecycleOwner` automatically.


Releated methods :
Expand All @@ -130,10 +132,18 @@ Releated methods :

Check [Releases](https://github.com/AlanCheen/Flap/releases) for details.


## Todo List

- Support AsyncListDiffer
- Support Lifecycle

## Contribution

Any feedback would be helpful , thanks.



## Contact Me

I'm Fitz , an Engineer working at Alibaba living in China .
Expand All @@ -148,14 +158,20 @@ Follow me on :

Feel free to contact me.



## Apps are using Flap

Contact me if you are using Flap in your App.



## Thanks

I'm using [StefMa/bintray-release](https://github.com/StefMa/bintray-release) to publish Flap to jCenter.



## License

```
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion 28
defaultConfig {
applicationId "me.yifeiyuan.flapdev"
minSdkVersion 17
targetSdkVersion 27
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -20,11 +20,11 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation project(':flap')
}
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:name=".FlapApplication"
>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name=".DifferActivity">
</activity>

</application>

</manifest>
83 changes: 83 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/DifferActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package me.yifeiyuan.flapdev;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import me.yifeiyuan.flap.FlapAdapter;
import me.yifeiyuan.flapdev.simpletext.SimpleTextModel;

/**
* todo support
* AsyncListDiffer
* ListAdapter
*/
public class DifferActivity extends AppCompatActivity {

private static final String TAG = "DifferActivity";

private FlapAdapter flapAdapter;
private AsyncListDiffer<SimpleTextModel> differ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_differ);

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

flapAdapter = new FlapAdapter();

List<SimpleTextModel> models = new ArrayList<>();

models.add(new SimpleTextModel("Android"));
models.add(new SimpleTextModel("Java"));
models.add(new SimpleTextModel("Kotlin"));

flapAdapter.setModels(models);

recyclerView.setAdapter(flapAdapter);

differ = new AsyncListDiffer<SimpleTextModel>(flapAdapter, new DiffUtil.ItemCallback<SimpleTextModel>() {
@Override
public boolean areItemsTheSame(@NonNull final SimpleTextModel simpleTextModel, @NonNull final SimpleTextModel t1) {
Log.d(TAG, "areItemsTheSame() called with: simpleTextModel = [" + simpleTextModel + "], t1 = [" + t1 + "]");
return false;
}

@Override
public boolean areContentsTheSame(@NonNull final SimpleTextModel simpleTextModel, @NonNull final SimpleTextModel t1) {
Log.d(TAG, "areContentsTheSame() called with: simpleTextModel = [" + simpleTextModel + "], t1 = [" + t1 + "]");
return false;
}
});
}

@Override
protected void onResume() {
super.onResume();
changeModels();
}

private void changeModels() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<SimpleTextModel> newModels = new ArrayList<>();
newModels.add(new SimpleTextModel("iOS"));
newModels.add(new SimpleTextModel("OC"));
newModels.add(new SimpleTextModel("Kotlin"));
flapAdapter.setModels(newModels);
differ.submitList(newModels);
}
}, 5000);
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.yifeiyuan.flapdev;

import android.app.Application;
import android.util.Log;

import me.yifeiyuan.flap.Flap;
import me.yifeiyuan.flapdev.simpleimage.SimpleImageItem;
import me.yifeiyuan.flapdev.simpleimage.SimpleImageModel;
import me.yifeiyuan.flapdev.simpletext.SimpleTextItem;
import me.yifeiyuan.flapdev.simpletext.SimpleTextModel;

/**
* Flap
* Created by 程序亦非猿 on 2018/12/13.
*/
public class FlapApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

Flap.setDebug(true);

long t1 = System.currentTimeMillis();

Flap.getDefault().register(SimpleTextModel.class, new SimpleTextItem.SimpleTextItemFactory());
Flap.getDefault().register(SimpleImageModel.class, new SimpleImageItem.SimpleImageItemFactory());

long t2 = System.currentTimeMillis();

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

}
}
8 changes: 3 additions & 5 deletions app/src/main/java/me/yifeiyuan/flapdev/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import me.yifeiyuan.flap.FlapAdapter;
import me.yifeiyuan.flapdev.simpleimage.SimpleImageModel;
import me.yifeiyuan.flapdev.simpleimage.SimpleImageVH;
import me.yifeiyuan.flapdev.simpletext.SimpleTextItem;
import me.yifeiyuan.flapdev.simpletext.SimpleTextModel;

public class MainActivity extends AppCompatActivity {
Expand All @@ -24,15 +22,15 @@ protected void onCreate(Bundle savedInstanceState) {

FlapAdapter adapter = new FlapAdapter();

adapter.registerItemFactory(new SimpleTextItem.SimpleTextItemFactory())
.registerItemFactory(new SimpleImageVH.SimpleImageItemFactory());

List<Object> models = new ArrayList<>();

models.add(new SimpleTextModel("Android"));
models.add(new SimpleTextModel("Java"));
models.add(new SimpleTextModel("Kotlin"));

models.add(new SimpleImageModel());
models.add(new SimpleImageModel());
models.add(new SimpleImageModel());
models.add(new SimpleImageModel());

adapter.setModels(models);
Expand Down
Loading

0 comments on commit bd812c3

Please sign in to comment.