Skip to content

Commit

Permalink
增加diffUtil的demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale committed Feb 8, 2017
1 parent 7974222 commit 99d0033
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 42 deletions.
1 change: 1 addition & 0 deletions adapter/src/main/java/kale/adapter/CommonRcvAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ static class RcvAdapterItem extends RecyclerView.ViewHolder {
this.item.bindViews(itemView);
this.item.setViews();
}

}

///////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<activity android:name=".ListViewActivity" />
<activity android:name=".RecyclerViewActivity" />
<activity android:name=".HeaderFooterActivity" />
<activity android:name=".DiffRcvActivity" />
</application>

</manifest>
104 changes: 104 additions & 0 deletions app/src/main/java/kale/commonadapter/DiffRcvActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package kale.commonadapter;

import java.util.List;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import kale.adapter.CommonRcvAdapter;
import kale.adapter.item.AdapterItem;
import kale.commonadapter.item.TextItem;
import kale.commonadapter.model.DemoModel;
import kale.commonadapter.util.DataManager;
import kale.commonadapter.util.LayoutUtil;

/**
* @author Kale
* @date 2017/2/8
*/
public class DiffRcvActivity extends AppCompatActivity {

private static final String TAG = "DiffRcvActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView recyclerView = new RecyclerView(this);
LayoutUtil.setContentView(this, recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
final DiffRcvAdapter<DemoModel> adapter = new DiffRcvAdapter<DemoModel>(DataManager.loadData(this, 3)) {
@NonNull
@Override
public AdapterItem createItem(Object type) {
return new TextItem();
}

@Override
protected boolean isContentSame(DemoModel oldItemData, DemoModel newItemData) {
return oldItemData.content.equals(newItemData.content);
}
};

recyclerView.setAdapter(adapter);

recyclerView.postDelayed(new Runnable() {
@Override
public void run() {
adapter.setData(DataManager.loadData(DiffRcvActivity.this, 3));
}
}, 1000);
}

public static abstract class DiffRcvAdapter<T> extends CommonRcvAdapter<T> {

DiffRcvAdapter(@Nullable List<T> data) {
super(data);
}

@Override
public void setData(@NonNull final List<T> data) {
DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return getItemCount();
}

@Override
public int getNewListSize() {
return data.size();
}

/**
* 检测是否是相同的item,这里暂时通过位置判断
*/
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
boolean result = oldItemPosition == newItemPosition;
Log.d(TAG, "areItemsTheSame: " + result);
return result;
}

/**
* 检测是否是相同的数据
* 这个方法仅仅在areItemsTheSame()返回true时,才调用。
*/
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
boolean result = isContentSame(getData().get(oldItemPosition), data.get(newItemPosition));
Log.d(TAG, "areContentsTheSame: " + result);
return result;
}
}).dispatchUpdatesTo(this);
super.setData(data);

}

protected abstract boolean isContentSame(T oldItemData, T newItemData);
}

}
4 changes: 4 additions & 0 deletions app/src/main/java/kale/commonadapter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.rcv_btn).setOnClickListener(this);
findViewById(R.id.rcv_btn2).setOnClickListener(this);
findViewById(R.id.viewpager_btn).setOnClickListener(this);
findViewById(R.id.diff_btn).setOnClickListener(this);
}

@Override
Expand All @@ -35,6 +36,9 @@ public void onClick(View v) {
case R.id.viewpager_btn:
clz = ViewPagerActivity.class;
break;
case R.id.diff_btn:
clz = DiffRcvActivity.class;
break;
}
startActivity(new Intent(this, clz));
}
Expand Down
39 changes: 0 additions & 39 deletions app/src/main/java/kale/commonadapter/item/BaseAdapterItem.java

This file was deleted.

2 changes: 0 additions & 2 deletions app/src/main/java/kale/commonadapter/item/TextItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
public class TextItem implements AdapterItem<DemoModel> {

private static final String TAG = "TextItem";

@Override
public int getLayoutResId() {
return R.layout.demo_item_text;
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/res/layout/main_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:id="@+id/list_view_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="100dp"
android:layout_marginTop="40dp"
android:text="ListView"
/>

Expand Down Expand Up @@ -39,4 +39,12 @@
android:text="viewpager"
/>

<Button
android:id="@+id/diff_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="26dp"
android:text="diffUtil"
/>

</LinearLayout>

0 comments on commit 99d0033

Please sign in to comment.