mNewDatas) {
21 | this.mOldDatas = mOldDatas;
22 | this.mNewDatas = mNewDatas;
23 | }
24 |
25 | //老数据集size
26 | @Override
27 | public int getOldListSize() {
28 | return mOldDatas != null ? mOldDatas.size() : 0;
29 | }
30 |
31 | //新数据集size
32 | @Override
33 | public int getNewListSize() {
34 | return mNewDatas != null ? mNewDatas.size() : 0;
35 | }
36 |
37 | /**
38 | * Called by the DiffUtil to decide whether two object represent the same Item.
39 | * 被DiffUtil调用,用来判断 两个对象是否是相同的Item。
40 | * For example, if your items have unique ids, this method should check their id equality.
41 | * 例如,如果你的Item有唯一的id字段,这个方法就 判断id是否相等。
42 | * 本例判断name字段是否一致
43 | *
44 | * @param oldItemPosition The position of the item in the old list
45 | * @param newItemPosition The position of the item in the new list
46 | * @return True if the two items represent the same object or false if they are different.
47 | */
48 | @Override
49 | public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
50 | return mOldDatas.get(oldItemPosition).getName().equals(mNewDatas.get(newItemPosition).getName());
51 | }
52 |
53 | /**
54 | * Called by the DiffUtil when it wants to check whether two items have the same data.
55 | * 被DiffUtil调用,用来检查 两个item是否含有相同的数据
56 | * DiffUtil uses this information to detect if the contents of an item has changed.
57 | * DiffUtil用返回的信息(true false)来检测当前item的内容是否发生了变化
58 | * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)}
59 | * DiffUtil 用这个方法替代equals方法去检查是否相等。
60 | * so that you can change its behavior depending on your UI.
61 | * 所以你可以根据你的UI去改变它的返回值
62 | * For example, if you are using DiffUtil with a
63 | * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should
64 | * return whether the items' visual representations are the same.
65 | * 例如,如果你用RecyclerView.Adapter 配合DiffUtil使用,你需要返回Item的视觉表现是否相同。
66 | * This method is called only if {@link #areItemsTheSame(int, int)} returns
67 | * {@code true} for these items.
68 | * 这个方法仅仅在areItemsTheSame()返回true时,才调用。
69 | *
70 | * @param oldItemPosition The position of the item in the old list
71 | * @param newItemPosition The position of the item in the new list which replaces the
72 | * oldItem
73 | * @return True if the contents of the items are the same or false if they are different.
74 | */
75 | @Override
76 | public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
77 | TestBean beanOld = mOldDatas.get(oldItemPosition);
78 | TestBean beanNew = mNewDatas.get(newItemPosition);
79 | if (!beanOld.getDesc().equals(beanNew.getDesc())) {
80 | return false;//如果有内容不同,就返回false
81 | }
82 | if (beanOld.getPic() != beanNew.getPic()) {
83 | return false;//如果有内容不同,就返回false
84 | }
85 | return true; //默认两个data内容是相同的
86 | }
87 |
88 | /**
89 | * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and
90 | * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil
91 | * calls this method to get a payload about the change.
92 | *
93 | * 当{@link #areItemsTheSame(int, int)} 返回true,且{@link #areContentsTheSame(int, int)} 返回false时,DiffUtils会回调此方法,
94 | * 去得到这个Item(有哪些)改变的payload。
95 | *
96 | * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the
97 | * particular field that changed in the item and your
98 | * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that
99 | * information to run the correct animation.
100 | *
101 | * 例如,如果你用RecyclerView配合DiffUtils,你可以返回 这个Item改变的那些字段,
102 | * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} 可以用那些信息去执行正确的动画
103 | *
104 | * Default implementation returns {@code null}.\
105 | * 默认的实现是返回null
106 | *
107 | * @param oldItemPosition The position of the item in the old list
108 | * @param newItemPosition The position of the item in the new list
109 | * @return A payload object that represents the change between the two items.
110 | * 返回 一个 代表着新老item的改变内容的 payload对象,
111 | */
112 | @Nullable
113 | @Override
114 | public Object getChangePayload(int oldItemPosition, int newItemPosition) {
115 | //实现这个方法 就能成为文艺青年中的文艺青年
116 | // 定向刷新中的部分更新
117 | // 效率最高
118 | //只是没有了ItemChange的白光一闪动画,(反正我也觉得不太重要)
119 | TestBean oldBean = mOldDatas.get(oldItemPosition);
120 | TestBean newBean = mNewDatas.get(newItemPosition);
121 |
122 | //这里就不用比较核心字段了,一定相等
123 | Bundle payload = new Bundle();
124 | if (!oldBean.getDesc().equals(newBean.getDesc())) {
125 | payload.putString("KEY_DESC", newBean.getDesc());
126 | }
127 | if (oldBean.getPic() != newBean.getPic()) {
128 | payload.putInt("KEY_PIC", newBean.getPic());
129 | }
130 |
131 | if (payload.size() == 0)//如果没有变化 就传空
132 | return null;
133 | return payload;//
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/diffutil/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.diffutil;
2 |
3 | import android.os.Bundle;
4 | import android.os.Handler;
5 | import android.os.Message;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.support.v7.util.DiffUtil;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.view.View;
11 |
12 | import com.mcxtzhang.diffutils.R;
13 |
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | public class MainActivity extends AppCompatActivity {
18 | private List mDatas;
19 | private RecyclerView mRv;
20 | private DiffAdapter mAdapter;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_main);
26 | initData();
27 | mRv = (RecyclerView) findViewById(R.id.rv);
28 | mRv.setLayoutManager(new LinearLayoutManager(this));
29 | mAdapter = new DiffAdapter(this, mDatas);
30 | mRv.setAdapter(mAdapter);
31 | }
32 |
33 | private void initData() {
34 | mDatas = new ArrayList<>();
35 | mDatas.add(new TestBean("张旭童1", "Android", R.drawable.pic1));
36 | mDatas.add(new TestBean("张旭童2", "Java", R.drawable.pic2));
37 | mDatas.add(new TestBean("张旭童3", "背锅", R.drawable.pic3));
38 | mDatas.add(new TestBean("张旭童4", "手撕产品", R.drawable.pic4));
39 | mDatas.add(new TestBean("张旭童5", "手撕测试", R.drawable.pic5));
40 | }
41 |
42 | /**
43 | * 模拟刷新操作
44 | *
45 | * @param view
46 | */
47 | public void onRefresh(View view) {
48 | try {
49 | mNewDatas = new ArrayList<>();
50 | for (TestBean bean : mDatas) {
51 | mNewDatas.add(bean.clone());//clone一遍旧数据 ,模拟刷新操作
52 | }
53 | mNewDatas.add(new TestBean("赵子龙", "帅", R.drawable.pic6));//模拟新增数据
54 | mNewDatas.get(0).setDesc("Android+");
55 | mNewDatas.get(0).setPic(R.drawable.pic7);//模拟修改数据
56 | TestBean testBean = mNewDatas.get(1);//模拟数据位移
57 | mNewDatas.remove(testBean);
58 | mNewDatas.add(testBean);
59 |
60 | //新宠
61 | //利用DiffUtil.calculateDiff()方法,传入一个规则DiffUtil.Callback对象,和是否检测移动item的 boolean变量,得到DiffUtil.DiffResult 的对象
62 | new Thread(new Runnable() {
63 | @Override
64 | public void run() {
65 | //放在子线程中计算DiffResult
66 | DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(mDatas, mNewDatas), true);
67 | Message message = mHandler.obtainMessage(H_CODE_UPDATE);
68 | message.obj = diffResult;//obj存放DiffResult
69 | message.sendToTarget();
70 | }
71 | }).start();
72 | //mAdapter.notifyDataSetChanged();//以前普通青年的我们只能这样,现在我们是文艺青年了,有新宠了
73 |
74 | } catch (CloneNotSupportedException e) {
75 | e.printStackTrace();
76 | }
77 | }
78 |
79 | private static final int H_CODE_UPDATE = 1;
80 | private List mNewDatas;//增加一个变量暂存newList
81 | private Handler mHandler = new Handler() {
82 | @Override
83 | public void handleMessage(Message msg) {
84 | switch (msg.what) {
85 | case H_CODE_UPDATE:
86 | //取出Result
87 | DiffUtil.DiffResult diffResult = (DiffUtil.DiffResult) msg.obj;
88 | //利用DiffUtil.DiffResult对象的dispatchUpdatesTo()方法,传入RecyclerView的Adapter,轻松成为文艺青年
89 | diffResult.dispatchUpdatesTo(mAdapter);
90 |
91 | //这种方法可以fix add 0 不滑动
92 | /*diffResult.dispatchUpdatesTo(new ListUpdateCallback() {
93 | @Override
94 | public void onInserted(int position, int count) {
95 | mAdapter.notifyItemRangeInserted(position, count);
96 | if (position==0){
97 | mRv.scrollToPosition(0);
98 | }
99 | }
100 |
101 | @Override
102 | public void onRemoved(int position, int count) {
103 | mAdapter.notifyItemRangeRemoved(position, count);
104 | }
105 |
106 | @Override
107 | public void onMoved(int fromPosition, int toPosition) {
108 | mAdapter.notifyItemMoved(fromPosition, toPosition);
109 | }
110 |
111 | @Override
112 | public void onChanged(int position, int count, Object payload) {
113 | mAdapter.notifyItemRangeChanged(position, count, payload);
114 | }
115 | });*/
116 |
117 | //别忘了将新数据给Adapter
118 | mDatas = mNewDatas;
119 | mAdapter.setDatas(mDatas);
120 | break;
121 | }
122 | }
123 | };
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/diffutil/TestBean.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.diffutil;
2 |
3 | /**
4 | * 介绍:一个普通的JavaBean,但是实现了clone方法,仅仅用于写Demo时,模拟刷新从网络获取数据用,
5 | * 因为使用DiffUtils比较新老数据集差异时,会遍历新老数据集的每个data,要确保他们的内存地址(指针)不一样,否则比较的是新老data是同一个,就一定相同,
6 | * 实际项目不需要,因为刷新时,数据一般从网络拉取,并且用Gson等解析出来,内存地址一定是不一样的。
7 | * 作者:zhangxutong
8 | * 邮箱:zhangxutong@imcoming.com
9 | * 时间: 2016/9/12.
10 | */
11 | public class TestBean implements Cloneable {
12 | private String name;
13 | private String desc;
14 | private int pic;
15 |
16 | public TestBean(String name, String desc, int pic) {
17 | this.name = name;
18 | this.desc = desc;
19 | this.pic = pic;
20 | }
21 |
22 | public int getPic() {
23 | return pic;
24 | }
25 |
26 | public void setPic(int pic) {
27 | this.pic = pic;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name;
36 | }
37 |
38 | public String getDesc() {
39 | return desc;
40 | }
41 |
42 | public void setDesc(String desc) {
43 | this.desc = desc;
44 | }
45 |
46 | //仅写DEMO 用 实现克隆方法
47 | @Override
48 | public TestBean clone() throws CloneNotSupportedException {
49 | TestBean bean = null;
50 | try {
51 | bean = (TestBean) super.clone();
52 | } catch (CloneNotSupportedException e) {
53 | e.printStackTrace();
54 | }
55 | return bean;
56 | }
57 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/sortedlist/SortedAdapter.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.sortedlist;
2 |
3 | import android.content.Context;
4 | import android.support.v7.util.SortedList;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 |
12 | import com.mcxtzhang.diffutils.R;
13 |
14 | /**
15 | * 介绍:Adapter要修改,
16 | * 数据源都要从以前的ArrayList->替换为SortedList.
17 | * 其他的话,倒没有太大变化,
18 | * 因为SortedList虽然没有继承自List,但是暴漏出API还和List一样的。
19 | * 作者:zhangxutong
20 | * 邮箱:mcxtzhang@163.com
21 | * 主页:http://blog.csdn.net/zxt0601
22 | * 时间: 2016/11/29.
23 | */
24 |
25 | public class SortedAdapter extends RecyclerView.Adapter {
26 | private final static String TAG = "zxt";
27 | /**
28 | * 数据源替换为SortedList,
29 | * 以前可能会用ArrayList。
30 | */
31 | private SortedList mDatas;
32 | private Context mContext;
33 | private LayoutInflater mInflater;
34 |
35 | public SortedAdapter(Context mContext, SortedList mDatas) {
36 | this.mContext = mContext;
37 | this.mDatas = mDatas;
38 | mInflater = LayoutInflater.from(mContext);
39 | }
40 |
41 | public void setDatas(SortedList mDatas) {
42 | this.mDatas = mDatas;
43 | }
44 |
45 | @Override
46 | public SortedAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
47 | return new SortedAdapter.VH(mInflater.inflate(R.layout.item_diff, parent, false));
48 | }
49 |
50 | @Override
51 | public void onBindViewHolder(final SortedAdapter.VH holder, final int position) {
52 | TestSortBean bean = mDatas.get(position);
53 | holder.tv1.setText(bean.getName());
54 | holder.tv2.setText(bean.getId() + "");
55 | holder.iv.setImageResource(bean.getIcon());
56 | }
57 |
58 | @Override
59 | public int getItemCount() {
60 | return mDatas != null ? mDatas.size() : 0;
61 | }
62 |
63 | class VH extends RecyclerView.ViewHolder {
64 | TextView tv1, tv2;
65 | ImageView iv;
66 |
67 | public VH(View itemView) {
68 | super(itemView);
69 | tv1 = (TextView) itemView.findViewById(R.id.tv1);
70 | tv2 = (TextView) itemView.findViewById(R.id.tv2);
71 | iv = (ImageView) itemView.findViewById(R.id.iv);
72 | }
73 | }
74 | }
75 |
76 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/sortedlist/SortedListActivity.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.sortedlist;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.support.v7.util.SortedList;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.View;
9 |
10 | import com.mcxtzhang.diffutils.R;
11 |
12 | /**
13 | * SortedListDemo
14 | */
15 | public class SortedListActivity extends AppCompatActivity {
16 | /**
17 | * 数据源替换为SortedList,
18 | * 以前可能会用ArrayList。
19 | */
20 | private SortedList mDatas;
21 | private RecyclerView mRv;
22 | private SortedAdapter mAdapter;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_sorted_list);
28 |
29 | mRv = (RecyclerView) findViewById(R.id.rv);
30 | mRv.setLayoutManager(new LinearLayoutManager(this));
31 | //★以前构建Adapter时,一般会将data也一起传入,现在有变化
32 | mAdapter = new SortedAdapter(this, null);
33 | mRv.setAdapter(mAdapter);
34 |
35 |
36 | initData();
37 |
38 |
39 | //mDatas.beginBatchedUpdates();
40 | mAdapter.setDatas(mDatas);
41 | //mDatas.endBatchedUpdates();
42 | }
43 |
44 | private void initData() {
45 | //★SortedList初始化的时候,要将Adapter传进来。所以先构建Adapter,再构建SortedList
46 | mDatas = new SortedList<>(TestSortBean.class, new SortedListCallback(mAdapter));
47 | mDatas.add(new TestSortBean(10, "Android", R.drawable.pic1));
48 | //★注意这里有一个重复的字段 会自动去重的。
49 | mDatas.add(new TestSortBean(10, "Android重复", R.drawable.pic1));
50 | mDatas.add(new TestSortBean(2, "Java", R.drawable.pic2));
51 | mDatas.add(new TestSortBean(30, "背锅", R.drawable.pic3));
52 | mDatas.add(new TestSortBean(4, "手撕产品", R.drawable.pic4));
53 | mDatas.add(new TestSortBean(50, "手撕测试", R.drawable.pic5));
54 | }
55 |
56 | /**
57 | * 模拟刷新操作
58 | *
59 | * @param view
60 | */
61 | public void onRefresh(View view) {
62 |
63 | //add 内部会自动调用 mCallback.onInserted(index, 1); ->notifyItemRangeInserted(index,1);
64 | //也就是说我们add一次 它就刷新一次,没有batch操作,有点low
65 |
66 | mDatas.add(new TestSortBean(26, "温油对待产品", R.drawable.pic6));//模拟新增
67 | mDatas.add(new TestSortBean(12, "小马可以来点赞了", R.drawable.pic6));//模拟新增
68 | mDatas.add(new TestSortBean(2, "Python", R.drawable.pic6));//add进去 重复的会自动修改
69 |
70 | // 如果想batch 就必须用addAll()操作,感觉这算一个限制。
71 | //addAll 也分两种
72 | //第一种 以可变参数addAll
73 | //mDatas.addAll(new TestSortBean(26, "帅", R.drawable.pic6),new TestSortBean(27, "帅", R.drawable.pic6));
74 | //第二种 集合形式
75 | /*
76 | List temp = new ArrayList<>();
77 | temp.add(new TestSortBean(26, "帅", R.drawable.pic6));
78 | temp.add(new TestSortBean(28, "帅", R.drawable.pic6));
79 | mDatas.addAll(temp);
80 | */
81 |
82 |
83 | //刷新时,服务器给我们的一般都是一个List
84 | //直接addAll 要先clear, 会闪屏
85 | /* List newDatas = new ArrayList<>();
86 | for (int i = 0; i < mDatas.size(); i++) {
87 | try {
88 | newDatas.add(mDatas.get(i).clone());//clone一遍旧数据 ,模拟刷新操作
89 | } catch (CloneNotSupportedException e) {
90 | e.printStackTrace();
91 | }
92 | }
93 | newDatas.add(new TestSortBean(29, "帅", R.drawable.pic6));//模拟新增数据
94 | newDatas.get(0).setName("Android+");
95 | newDatas.get(0).setIcon(R.drawable.pic7);//模拟修改数据
96 | TestSortBean testBean = newDatas.get(1);//模拟数据位移
97 | newDatas.remove(testBean);
98 | newDatas.add(testBean);
99 | mDatas.clear();
100 | mDatas.addAll(newDatas);*/
101 |
102 |
103 | new Thread(new Runnable() {
104 | @Override
105 | public void run() {
106 | //每次add都会计算一次 想放在子线程中
107 | //然而这是肯定不行的,上文提过,每次add 会自动 mAdapter.notifyItemRangeInserted(position, count);
108 | //这一点就不如DiffUtil啦。
109 | //android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
110 | /*mDatas.add(new TestSortBean(26, "帅", R.drawable.pic6));//模拟新增数据
111 | mDatas.add(new TestSortBean(27, "帅", R.drawable.pic6));//模拟新增数据*/
112 | }
113 | }).start();
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/sortedlist/SortedListCallback.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.sortedlist;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.support.v7.widget.util.SortedListAdapterCallback;
5 |
6 | /**
7 | * 介绍:比较规则Callback。
8 | * 和DiffUtil.Callback。写法套路一毛一样。
9 | * 而且比DiffUtil.Callback简单。
10 | * 因为不用传数据集进来,每次直接给你Item比较。
11 | *
12 | * 作者:zhangxutong
13 | * 邮箱:mcxtzhang@163.com
14 | * 主页:http://blog.csdn.net/zxt0601
15 | * 时间: 2016/11/29.
16 | */
17 |
18 | public class SortedListCallback extends SortedListAdapterCallback {
19 | /**
20 | * Creates a {@link SortedList.Callback} that will forward data change events to the provided
21 | * Adapter.
22 | *
23 | * @param adapter The Adapter instance which should receive events from the SortedList.
24 | */
25 | public SortedListCallback(RecyclerView.Adapter adapter) {
26 | super(adapter);
27 | }
28 |
29 | /**
30 | * 把它当成equals 方法就好
31 | */
32 | @Override
33 | public int compare(TestSortBean o1, TestSortBean o2) {
34 | return o1.getId() - o2.getId();
35 | }
36 |
37 | /**
38 | * 和DiffUtil方法一致,不再赘述
39 | */
40 | @Override
41 | public boolean areItemsTheSame(TestSortBean item1, TestSortBean item2) {
42 | return item1.getId() == item2.getId();
43 | }
44 | /**
45 | * 和DiffUtil方法一致,不再赘述
46 | */
47 | @Override
48 | public boolean areContentsTheSame(TestSortBean oldItem, TestSortBean newItem) {
49 | //默认相同 有一个不同就是不同
50 | if (oldItem.getId() != newItem.getId()) {
51 | return false;
52 | }
53 | if (oldItem.getName().equals(newItem.getName())) {
54 | return false;
55 | }
56 | if (oldItem.getIcon() != newItem.getIcon()) {
57 | return false;
58 | }
59 | return true;
60 | }
61 |
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/mcxtzhang/diffutils/sortedlist/TestSortBean.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils.sortedlist;
2 |
3 | /**
4 | * 介绍:
5 | * 作者:zhangxutong
6 | * 邮箱:mcxtzhang@163.com
7 | * 主页:http://blog.csdn.net/zxt0601
8 | * 时间: 2016/11/29.
9 | */
10 |
11 | public class TestSortBean implements Cloneable{
12 | private int id;
13 | private String name;
14 | private int icon;
15 |
16 | //仅写DEMO 用 实现克隆方法
17 | @Override
18 | public TestSortBean clone() throws CloneNotSupportedException {
19 | TestSortBean bean = null;
20 | try {
21 | bean = (TestSortBean) super.clone();
22 | } catch (CloneNotSupportedException e) {
23 | e.printStackTrace();
24 | }
25 | return bean;
26 | }
27 |
28 | @Override
29 | public String toString() {
30 | return "TestSortBean{" +
31 | "icon='" + icon + '\'' +
32 | ", name='" + name + '\'' +
33 | ", id=" + id +
34 | '}';
35 | }
36 |
37 | public TestSortBean(int id, String name, int icon) {
38 | this.id = id;
39 | this.name = name;
40 | this.icon = icon;
41 | }
42 |
43 | public int getId() {
44 | return id;
45 | }
46 |
47 | public TestSortBean setId(int id) {
48 | this.id = id;
49 | return this;
50 | }
51 |
52 | public String getName() {
53 | return name;
54 | }
55 |
56 | public TestSortBean setName(String name) {
57 | this.name = name;
58 | return this;
59 | }
60 |
61 | public int getIcon() {
62 | return icon;
63 | }
64 |
65 | public TestSortBean setIcon(int icon) {
66 | this.icon = icon;
67 | return this;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic2.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic3.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic4.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic5.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic6.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/pic7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/drawable-xxhdpi/pic7.jpg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
20 |
21 |
22 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_sorted_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
16 |
17 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_diff.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
21 |
22 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DiffUtils
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/mcxtzhang/diffutils/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.mcxtzhang.diffutils;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.2'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mcxtzhang/SupportDemos/73e091cc09620ea7a4caaea3a3a1cf5c5daee843/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------