我們都知道競拍的時(shí)效要求是非常高的。每秒都有可能出現(xiàn)新的拍賣結(jié)果,這個(gè)時(shí)候拍賣的單價(jià),拍賣人,拍賣剩余時(shí)間都可能會(huì)發(fā)生改變.
遇到的問題
因?yàn)楦偱慕Y(jié)果每秒都有可能發(fā)生變化,所以需要每秒都要去獲取新數(shù)據(jù),但這里我只獲取可見區(qū)域的數(shù)據(jù),更新可見區(qū)域數(shù)據(jù),盡量減少不必要的開銷.
首先取得可見區(qū)域數(shù)據(jù)的id,然后請(qǐng)求服務(wù)器
rvGrabList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
firstVisiblePosition = gridLayoutManager.findFirstVisibleItemPosition();
endVisiblePosition = gridLayoutManager.findLastVisibleItemPosition();
if (newState == SCROLL_STATE_IDLE) { //滑動(dòng)停止的時(shí)候開始定時(shí)
mVisibleAuctionGoodsBeans.clear();
mVisibleAuctionGoodsBeans.addAll(mAuctionGoodsBeans.subList(firstVisiblePosition, endVisiblePosition));
}
}
});
上面的代碼就是在數(shù)據(jù)列表滑動(dòng)停止的時(shí)候,獲取中間的可見區(qū)域數(shù)據(jù)集合.
高性能獲取即時(shí)變化的數(shù)據(jù),這個(gè)方法每秒執(zhí)行一次.
private void doClockResetData() {
if (mVisibleAuctionGoodsBeans.isEmpty()) {
return;
}
StringBuilder s = new StringBuilder();
for (AuctionGoodsBean bean : mVisibleAuctionGoodsBeans) {
s.append(bean.goodsId).append("_").append(bean.issueId).append(",");
}
if (!TextUtils.isEmpty(s.toString())) {
AuctionRequest.getInstance().requestGoodsChangeSeconds(this, mActivity, pUtil.getUserToken(), s.toString());
}
}
把獲取的可見區(qū)域數(shù)據(jù)的id組裝字符串,然后請(qǐng)求后臺(tái)高性能數(shù)據(jù)刷新接口.
可見區(qū)域的數(shù)據(jù)一直在變,怎樣高效修改數(shù)據(jù)呢?常規(guī)的notifyDataChage因?yàn)榱斜砘瑒?dòng),導(dǎo)致可見列表錯(cuò)亂,造成了屏幕閃爍。
分析:因?yàn)槲抑皇切薷牧丝梢妳^(qū)域的數(shù)據(jù),所以我只想對(duì)可見區(qū)域的數(shù)據(jù)進(jìn)行更新.
如果使用nofityDataChange()方法,因?yàn)槲耶?dāng)前數(shù)據(jù)排序已經(jīng)亂了,如果調(diào)用這個(gè)方法圖片路徑就與原來的圖片路徑對(duì)應(yīng)不上,所以就會(huì)一直出現(xiàn)閃爍.
所以肯定不能用notifyDataChange()來更新數(shù)據(jù),既然這樣不行,那我就更新需要更新的ViewHolder不就可以了.
private void setSecondsDataChange(ZLResposeResult result) {
final List<AuctionGoodsBean> newGoodBeans = JSON.parseArray(result.getBody(), AuctionGoodsBean.class); //后臺(tái)返回的結(jié)果變化數(shù)據(jù)
for (AuctionGoodsBean newGoodBean : newGoodBeans) { //高性能數(shù)據(jù)
AuctionGoodsBean oldGoodBean = mAuctionGoodSparseArray.get(newGoodBean.goodsId);
if (oldGoodBean.status != 2) {
oldGoodBean.bidderName = newGoodBean.bidderName;
oldGoodBean.status = newGoodBean.issueSta;
oldGoodBean.leftSecond = newGoodBean.leftSecond;
if (newGoodBean.donePrice > oldGoodBean.donePrice || oldGoodBean.status != 1) { //如果價(jià)格發(fā)生了改變,或者狀態(tài)發(fā)生了改變需要刷新一下
oldGoodBean.priceChange = true;
oldGoodBean.donePrice = newGoodBean.donePrice;
oldGoodBean.lastTime = 0; //強(qiáng)制設(shè)為0,為下一次做準(zhǔn)備
homeAuctionAdapter.initAuctionGoods(oldGoodBean);
} else {
oldGoodBean.donePrice = newGoodBean.donePrice;
oldGoodBean.priceChange = false;
}
mAuctionGoodSparseArray.put(oldGoodBean.goodsId, oldGoodBean);
}
}
}
這里主要是要注意一下,這里我遍歷一下要更新的數(shù)據(jù).homeAuctionAdapter.initAuctionGoods(oldGoodBean); 我這里只更新一條數(shù)據(jù),
在我的數(shù)據(jù)適配器中:我有一個(gè)map來存儲(chǔ)我的viewHolder,方便我下次使用的時(shí)候把這個(gè)viewholder取出來,這樣做到了
這段代碼在convert()方法中
String mapTag = bean.goodsId + "_" + bean.issueId;
if (!mapTag.equals(holder.getView(R.id.iv_img).getTag())) {
holder.setImageUrlPic(R.id.iv_img, bean.goodsPic, mActivity);
holder.getView(R.id.iv_img).setTag(mapTag);
}
holder.setText(R.id.tv_title, bean.goodsTitle);
viewHolderMap.put(mapTag, holder);
initAuctionGoods(bean);
列表的時(shí)間因?yàn)閺?fù)用會(huì)出現(xiàn)錯(cuò)亂,
這里我使用了官方的一個(gè)CountDownTimer類做計(jì)時(shí)器,并對(duì)其進(jìn)一步封裝了
public class TimerCountUtil extends CountDownTimer {
private TextView tvTime;// 專門用來顯示時(shí)間
private ITimerCallback callback; //時(shí)間走完后的回調(diào)
private String strExtra; //附加數(shù)據(jù) 打比方時(shí)間是 "00:10:23后完成" 加上后面這一字段
public void setCallback(ITimerCallback callback) {
this.callback = callback;
}
public void setStrExtra(String strExtra) {
this.strExtra = strExtra;
}
/***
* 功能:
* 作者:hg_liuzl@qq.com
* @param millisInFuture 轉(zhuǎn)入的時(shí)間,單位是毫秒
* @param countDownInterval 多少時(shí)間執(zhí)行一次,一般是1000 毫秒
***/
public TimerCountUtil(long millisInFuture, long countDownInterval, TextView tv) {
super(millisInFuture, countDownInterval);
this.tvTime = tv;
}
public TimerCountUtil(long millisInFuture, TextView tv) {
super(millisInFuture, 1000);
this.tvTime = tv;
}
@Override
public void onTick(long leftSeconds) {
tvTime.setText(showTimes(leftSeconds));
}
@Override
public void onFinish() {
if (callback != null) {
callback.doStop();
} else {
tvTime.setText("00:00:00");
}
}
private String showTimes(long time) {
long second = time / 1000;
long hour = second / 60 / 60;
long minute = (second - hour * 60 * 60) / 60;
StringBuilder sbTime = new StringBuilder();
sbTime.append(hour < 10 ? "0" + hour : hour);
sbTime.append(":");
sbTime.append(minute < 10 ? "0" + minute : minute);
sbTime.append(":");
sbTime.append(second < 10 ? "0" + second : second);
if (!TextUtils.isEmpty(strExtra)) {
sbTime.append(" ").append(strExtra);
}
return sbTime.toString();
}
}
這里使用了一個(gè)Map來解決時(shí)間因?yàn)榭丶?fù)用引起錯(cuò)亂的問題,
/***
* 功能:適配器數(shù)據(jù)
* 作者:hg_liuzl@qq.com
* @param
***/
public void initAuctionGoods(AuctionGoodsBean bean) {
String newMapTag = bean.goodsId + "_" + bean.issueId;
ViewHolder holder = viewHolderMap.get(newMapTag);
if (null != holder) {
holder.setText(R.id.tv_money, String.valueOf("¥" + WordUtils.decimalFormat(bean.donePrice)));
if (bean.status == 0) {
preAuctionGoods(holder);
} else if (bean.status == 1) {
//取消當(dāng)前到時(shí)任務(wù),(清除倒時(shí)任務(wù),再啟動(dòng),解除沖突)
TextView textView = holder.getView(R.id.tv_timer);
TimerCountUtil tc = leftTimeMap.get(textView);
if (tc != null) {
tc.cancel();
}
tc = new TimerCountUtil(bean.getLeftTime() * 1000, textView);
tc.start();
leftTimeMap.put(textView, tc);
onAuctioningGoods(holder, bean);
} else {
hasAuctionGoods(holder);
}
}
}
頻繁開啟定時(shí)器造成鎖定問題.
//取消當(dāng)前到時(shí)任務(wù),(清除倒時(shí)任務(wù),再啟動(dòng),解除沖突)
TextView textView = holder.getView(R.id.tv_timer);
TimerCountUtil tc = leftTimeMap.get(textView);
if (tc != null) {
tc.cancel();
}
tc = new TimerCountUtil(bean.getLeftTime() * 1000, textView);
tc.start();
leftTimeMap.put(textView, tc);
這里需要先取消定時(shí)器,
時(shí)間沒有同步,即當(dāng)滑到新的可見區(qū)域后,再回來原來的可見區(qū)域,時(shí)間并沒有走動(dòng).
在我們把當(dāng)前列表的時(shí)間控件滑動(dòng)不可見區(qū)域,再滑回來,發(fā)現(xiàn)時(shí)間還是原來的時(shí)間,并沒有走動(dòng),這是不對(duì)的,我們的時(shí)間應(yīng)該還是要走動(dòng)的,而不能停止,這里我使用了一個(gè)非常巧妙的辦法解決了問題。
問題分析:因?yàn)槲一厝ミ€是使用了原來的時(shí)間,而原來的時(shí)間是一直不變的,所以就造成了這個(gè)問題.
這是在這個(gè)bean的實(shí)體類里面.
public long lastTime; //記錄一下上次時(shí)間
/***
*
* 作者:hg_liuzl@qq.com
* @param
***/
public long getLeftTime() {
if (lastTime == 0) {
lastTime = System.currentTimeMillis();
}
return leftSecond - (System.currentTimeMillis() - lastTime) / 1000;
}
這個(gè)方法 是非常重要的,我要記錄上次啟動(dòng)計(jì)時(shí)器的時(shí)間,再用當(dāng)前時(shí)間減去上次記錄的時(shí)間,那這個(gè)時(shí)間差就是在不可見區(qū)域所走的時(shí)間,我把這個(gè)時(shí)間減掉,就是我要顯示的時(shí)間,結(jié)果驗(yàn)證,完美解決問題.