-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kale
committed
Feb 8, 2017
1 parent
7974222
commit 99d0033
Showing
7 changed files
with
119 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
app/src/main/java/kale/commonadapter/DiffRcvActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
app/src/main/java/kale/commonadapter/item/BaseAdapterItem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters