主要有如下部分
这个类主要是将新旧数据进行比较的类 public class DiffCallBack extends DiffUtil.Callback { private List<OverallStockBean> mOldDatas, mNewDatas;//看名字 public DiffCallBack(List<OverallStockBean> mOldDatas, List<OverallStockBean> mNewDatas) { this.mOldDatas = mOldDatas; this.mNewDatas = mNewDatas; } //老数据集size @Override public int getOldListSize() { return mOldDatas != null ? mOldDatas.size() : 0; } //新数据集size @Override public int getNewListSize() { return mNewDatas != null ? mNewDatas.size() : 0; } /** * Called by the DiffUtil to decide whether two object represent the same Item. * 被DiffUtil调用,用来判断 两个对象是否是相同的Item。 * For example, if your items have unique ids, this method should check their id equality. * 例如,如果你的Item有唯一的id字段,这个方法就 判断id是否相等。 * 本例判断name字段是否一致 * * @param oldItemPosition The position of the item in the old list * @param newItemPosition The position of the item in the new list * @return True if the two items represent the same object or false if they are different. */ @Override public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { if(mOldDatas==null||mNewDatas==null||mOldDatas.get(oldItemPosition)==null||mNewDatas.get(newItemPosition)==null){ return false; } return mOldDatas.get(oldItemPosition).getSymbol().equals(mNewDatas.get(newItemPosition).getSymbol()); } /** * Called by the DiffUtil when it wants to check whether two items have the same data. * 被DiffUtil调用,用来检查 两个item是否含有相同的数据 * DiffUtil uses this information to detect if the contents of an item has changed. * DiffUtil用返回的信息(true false)来检测当前item的内容是否发生了变化 * DiffUtil uses this method to check equality instead of {@link Object#equals(Object)} * DiffUtil 用这个方法替代equals方法去检查是否相等。 * so that you can change its behavior depending on your UI. * 所以你可以根据你的UI去改变它的返回值 * For example, if you are using DiffUtil with a * {@link android.support.v7.widget.RecyclerView.Adapter RecyclerView.Adapter}, you should * return whether the items' visual representations are the same. * 例如,如果你用RecyclerView.Adapter 配合DiffUtil使用,你需要返回Item的视觉表现是否相同。 * This method is called only if {@link #areItemsTheSame(int, int)} returns * {@code true} for these items. * 这个方法仅仅在areItemsTheSame()返回true时,才调用。 * * @param oldItemPosition The position of the item in the old list * @param newItemPosition The position of the item in the new list which replaces the * oldItem * @return True if the contents of the items are the same or false if they are different. */ @Override public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { OverallStockBean beanOld = mOldDatas.get(oldItemPosition); OverallStockBean beanNew = mNewDatas.get(newItemPosition); if (!beanOld.getPrice().equals(beanNew.getPrice())) { return false;//如果有内容不同,就返回false } return true; //默认两个data内容是相同的 } /** * When {@link #areItemsTheSame(int, int)} returns {@code true} for two items and * {@link #areContentsTheSame(int, int)} returns false for them, DiffUtil * calls this method to get a payload about the change. * <p> * 当{@link #areItemsTheSame(int, int)} 返回true,且{@link #areContentsTheSame(int, int)} 返回false时,DiffUtils会回调此方法, * 去得到这个Item(有哪些)改变的payload。 * <p> * For example, if you are using DiffUtil with {@link RecyclerView}, you can return the * particular field that changed in the item and your * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} can use that * information to run the correct animation. * <p> * 例如,如果你用RecyclerView配合DiffUtils,你可以返回 这个Item改变的那些字段, * {@link android.support.v7.widget.RecyclerView.ItemAnimator ItemAnimator} 可以用那些信息去执行正确的动画 * <p> * Default implementation returns {@code null}.\ * 默认的实现是返回null * * @param oldItemPosition The position of the item in the old list * @param newItemPosition The position of the item in the new list * @return A payload object that represents the change between the two items. * 返回 一个 代表着新老item的改变内容的 payload对象, */ @Nullable @Override public Object getChangePayload(int oldItemPosition, int newItemPosition) { //实现这个方法 就能成为文艺青年中的文艺青年 // 定向刷新中的部分更新 // 效率最高 //只是没有了ItemChange的白光一闪动画,(反正我也觉得不太重要) OverallStockBean oldBean = mOldDatas.get(oldItemPosition); OverallStockBean newBean = mNewDatas.get(newItemPosition); //这里就不用比较核心字段了,一定相等 Bundle payload = new Bundle(); // if(!oldBean.getSymbol().equals(newBean.getSymbol())){ // payload.putString("KEY_DESC", newBean.getPrice()); // return payload; // } if (Double.valueOf(oldBean.getPrice())>(Double.valueOf(newBean.getPrice()))||Double.valueOf(oldBean.getUpdownrate())>(Double.valueOf(newBean.getUpdownrate()))) { payload.putString("up", newBean.getPrice()); return payload; } if (Double.valueOf(oldBean.getPrice())<(Double.valueOf(newBean.getPrice()))||Double.valueOf(oldBean.getUpdownrate())<(Double.valueOf(newBean.getUpdownrate()))) { payload.putString("down", newBean.getPrice()); return payload; } if (payload.size() == 0)//如果没有变化 就传空 return null; return payload;// } }
接下来是适配器里对数据的处理部分
@Override public void onBindViewHolder(DiffVH holder, int position, List<Object> payloads) { if (payloads==null||payloads.isEmpty()) { onBindViewHolder(holder, position); } else { //需要更新的类型 Bundle payload = (Bundle) payloads.get(0);//取出我们在getChangePayload()方法返回的bundle OverallStockBean result = mDatas.get(position);//取出新数据源,(可以不用) for (String key : payload.keySet()) { switch (key) { case "KEY_DESC": holder.imgBg.setAlpha(0); holder.tvStockName.setText(result.getName()); holder.tvStockCode.setText(result.getSymbol()); holder.tvPrice.setText(result.getPrice() + ""); if (Double.valueOf(result.getUpdownrate()) >0) { // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_red)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_login_color_s_2)); holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.login_color)); } else if(Double.valueOf(result.getUpdownrate()) < 0){ holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_32b586)); holder.tvproporttion.setText(result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.green_down)); // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_green)); }else { holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.color_pointtxt)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_gray_s_2)); } break; case "up": holder.tvStockName.setText(result.getName()); holder.tvStockCode.setText(result.getSymbol()); holder.tvPrice.setText(result.getPrice() + ""); holder.tvPrice.setText(result.getPrice() + ""); if (Double.valueOf(result.getUpdownrate()) >0) { // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_red)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_login_color_s_2)); holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.login_color)); } else if(Double.valueOf(result.getUpdownrate()) < 0){ holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_32b586)); holder.tvproporttion.setText(result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.green_down)); // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_green)); }else { holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.color_pointtxt)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_gray_s_2)); } holder.tvPrice.setText(result.getPrice()); holder.imgBg.setBackground(mContext.getResources().getDrawable(R.mipmap.quotation_bg_red)); ObjectAnimator animator = ObjectAnimator.ofFloat(holder.imgBg, "alpha", 0f, 1f, 0f); animator.setDuration(200); animator.start(); break; case "down": holder.tvStockName.setText(result.getName()); holder.tvStockCode.setText(result.getSymbol()); holder.tvPrice.setText(result.getPrice() + ""); if (Double.valueOf(result.getUpdownrate()) >0) { // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_red)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_login_color_s_2)); holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.login_color)); } else if(Double.valueOf(result.getUpdownrate()) < 0){ holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_c_32b586)); holder.tvproporttion.setText(result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.green_down)); // rlItem.setBackground(getContext().getResources().getDrawable(R.mipmap.market_bg_green)); }else { holder.tvproporttion.setText("+" + result.getUpdownrate() + "%"); holder.tvPrice.setTextColor(mContext.getResources().getColor(R.color.color_pointtxt)); holder.tvproporttion.setBackground(mContext.getResources().getDrawable(R.drawable.bg_gray_s_2)); } holder.tvPrice.setText(result.getPrice()); holder.imgBg.setBackground(mContext.getResources().getDrawable(R.mipmap.quotation_bg_green)); ObjectAnimator animator1 = ObjectAnimator.ofFloat(holder.imgBg, "alpha", 0f, 1f, 0f); animator1.setDuration(200); animator1.start(); break; default: break; } } } }
接下来是在子线程中得到变化数据(红色部分得到变化数据
)
class ShareVariableRunnable implements Runnable {
@Override
public synchronized void run() {
List<OverallStockBean> list = new ArrayList<>();
try {
list = getOptionalOverallStcksIsWeebJSON(message.getResponseText());
if(!beanMap.isEmpty()) {
if (!beanMap.get(list.get(0).getSymbol()).getPrice().equals(list.get(0).getPrice())) {
beanMap.put(list.get(0).getSymbol(), list.get(0));
mNewDatas = getList(beanMap);
diffAdapter.setDatas(mNewDatas);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(mDatas, mNewDatas), false);
mDatas=mNewDatas;
// diffAdapter.setDatas(mDatas);
if(myHandler!=null) {
Message message = myHandler.obtainMessage(H_CODE_UPDATE);
message.obj = diffResult;//obj存放DiffResult
message.sendToTarget();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
接下是handler处理信息部分