Skip to content

Commit

Permalink
Merge pull request #15 from AlanCheen/feature/asynclistdiffer
Browse files Browse the repository at this point in the history
Feature/asynclistdiffer
  • Loading branch information
AlanCheen authored Jan 8, 2019
2 parents a9d11b9 + 68c2f4a commit a942741
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 27 deletions.
59 changes: 33 additions & 26 deletions app/src/main/java/me/yifeiyuan/flapdev/DifferActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
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.flap.extensions.DifferFlapAdapter;
import me.yifeiyuan.flapdev.simpletext.SimpleTextModel;

/**
Expand All @@ -24,8 +22,7 @@ public class DifferActivity extends AppCompatActivity {

private static final String TAG = "DifferActivity";

private FlapAdapter flapAdapter;
private AsyncListDiffer<SimpleTextModel> differ;
private DifferFlapAdapter<SimpleTextModel> flapAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -34,49 +31,59 @@ protected void onCreate(Bundle savedInstanceState) {

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.setData(models);

recyclerView.setAdapter(flapAdapter);

differ = new AsyncListDiffer<SimpleTextModel>(flapAdapter, new DiffUtil.ItemCallback<SimpleTextModel>() {
flapAdapter = new DifferFlapAdapter<>(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;
return true;
}

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

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

for (int i = 0; i < 20; i++) {
models.add(new SimpleTextModel("Android :" + i));
}

flapAdapter.setData(models);

recyclerView.setAdapter(flapAdapter);
}

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

private void testClear() {
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
// flapAdapter.getData().clear(); will throw UnsupportedOperationException
flapAdapter.setData(new ArrayList<>());
}
}, 3000);
}

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"));

for (int i = 0; i < 20; i++) {
// newModels.add(new SimpleTextModel("Android :" + i));
newModels.add(new SimpleTextModel("Android :" + (i % 2 == 0 ? i : (666))));
}

flapAdapter.setData(newModels);
differ.submitList(newModels);
}
}, 5000);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.yifeiyuan.flapdev.simpletext;

import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

Expand All @@ -27,6 +28,7 @@ public SimpleTextItem(final View itemView) {

@Override
protected void onBind(@NonNull final SimpleTextModel model, @NonNull final FlapAdapter adapter, @NonNull final List<Object> payloads) {
Log.d(TAG, "onBind: " + getAdapterPosition());
tvContent.setText(model.content);
}

Expand Down
2 changes: 1 addition & 1 deletion flap/src/main/java/me/yifeiyuan/flap/FlapAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public FlapAdapter setLifecycleEnable(boolean lifecycleEnable) {
return this;
}

private Object getItem(final int position) {
protected Object getItem(final int position) {
return getData().get(position);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.yifeiyuan.flap.extensions;

import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.AsyncDifferConfig;
import android.support.v7.recyclerview.extensions.AsyncListDiffer;
import android.support.v7.util.AdapterListUpdateCallback;
import android.support.v7.util.DiffUtil;

import java.util.List;

import me.yifeiyuan.flap.FlapAdapter;

/**
* Created by 程序亦非猿 on 2019/1/4.
*/
@SuppressWarnings("unchecked")
public class DifferFlapAdapter<T> extends FlapAdapter {

private AsyncListDiffer<T> differ;

public DifferFlapAdapter(final @NonNull DiffUtil.ItemCallback<T> itemCallback) {
differ = new AsyncListDiffer(this, itemCallback);
}

public DifferFlapAdapter(@NonNull AsyncDifferConfig<T> config) {
differ = new AsyncListDiffer(new AdapterListUpdateCallback(this), config);
}

@Override
public DifferFlapAdapter setData(@NonNull final List<?> data) {
differ.submitList((List<T>) data);
return this;
}

@Override
protected T getItem(int position) {
return differ.getCurrentList().get(position);
}

@Override
public int getItemCount() {
return differ.getCurrentList().size();
}

@NonNull
@Override
public List<T> getData() {
return differ.getCurrentList();
}
}

0 comments on commit a942741

Please sign in to comment.