項(xiàng)目地址:https://github.com/razerdp/FriendCircle
一起擼個(gè)朋友圈吧這是本文所處文集,所有更新都會(huì)在這個(gè)文集里面哦,歡迎關(guān)注
上篇鏈接:http://www.itdecent.cn/p/4cc3f9c8a713
下篇鏈接:http://www.itdecent.cn/p/885538a261ea
在昨天的那篇文章,我們的評(píng)論控件的優(yōu)化里提及過希望采用一個(gè)對(duì)象池來保存,碰巧,今天在看ViewGroup的代碼的時(shí)候,發(fā)現(xiàn)了這么一個(gè)接口:

根據(jù)文檔,可以知道這個(gè)接口在ViewGroup層次改變的時(shí)候會(huì)回調(diào),回調(diào)兩個(gè)方法:onChildViewAdded和onChildViewRemoved,這兩個(gè)方法正是與addView和removeView掛鉤。
在昨天我們提出過希望在removeView的時(shí)候回收,下次再add的時(shí)候從池里面拿,在今天看到這個(gè)接口后,在下就覺得-----是時(shí)候了。。。
首先我們構(gòu)造一個(gè)簡(jiǎn)單的池,用數(shù)組包著view就可以了
static class CommentPool{
private CommentWidget[] CommentPool;
private int size;
private int curPointer=-1;
public CommentPool(int size) {
this.size = size;
CommentPool=new CommentWidget[size];
}
public synchronized CommentWidget get(){
if (curPointer==-1||curPointer>CommentPool.length)return null;
CommentWidget commentTextView=CommentPool[curPointer];
CommentPool[curPointer]=null;
curPointer--;
return commentTextView;
}
public synchronized boolean put(CommentWidget commentTextView){
if (curPointer==-1||curPointer<CommentPool.length-1) {
curPointer++;
CommentPool[curPointer] = commentTextView;
return true;
}
return false;
}
public void clearPool(){
for (int i = 0; i < CommentPool.length; i++) {
CommentPool[i]=null;
}
curPointer=-1;
}
}
我們主要實(shí)現(xiàn)三個(gè)方法:
- put:入池(不是入棧哦)
- get:出池
- clear:清池
因?yàn)閖ava木有指針,所以我們采用自己夠早一個(gè)游標(biāo)的方法來進(jìn)行池對(duì)象的推入和獲取。
在昨天的代碼里,我們構(gòu)造一個(gè)靜態(tài)池,防止每次反射創(chuàng)建時(shí)被初始化。
//評(píng)論區(qū)的view對(duì)象池
private static final CommentPool COMMENT_TEXT_POOL=new CommentPool(20);
存入的數(shù)量大概20條就足夠了,接下來實(shí)現(xiàn)OnHierarchyChangeListener接口,然后改造我們的addCommentWidget方法:
private void addCommentWidget(List<CommentInfo> commentList) {
if (commentList == null || commentList.size() == 0) return;
/**
* 優(yōu)化方案:
* 因?yàn)槭窃趌istview里面,那么復(fù)用肯定有,意味著滑動(dòng)的時(shí)候必須要removeView或者addView
* 但為了性能提高,不可以直接removeAllViews
* 于是采取以下方案:
* 根據(jù)現(xiàn)有的view進(jìn)行remove/add差額
* 然后統(tǒng)一設(shè)置
*
* 2016-02-26:復(fù)用池進(jìn)一步優(yōu)化
* */
final int childCount = commentLayout.getChildCount();
commentLayout.setOnHierarchyChangeListener(this);
if (childCount < commentList.size()) {
//當(dāng)前的view少于list的長(zhǎng)度,則補(bǔ)充相差的view
int subCount = commentList.size() - childCount;
for (int i = 0; i < subCount; i++) {
CommentWidget commentWidget =COMMENT_TEXT_POOL.get();
if (commentWidget == null) {
commentWidget = new CommentWidget(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin = 1;
params.bottomMargin = 1;
commentWidget.setLayoutParams(params);
commentWidget.setLineSpacing(4, 1);
}
commentWidget.setOnClickListener(this);
commentWidget.setOnLongClickListener(this);
commentLayout.addView(commentWidget);
}
}
else if (childCount > commentList.size()) {
//當(dāng)前的view的數(shù)目比list的長(zhǎng)度大,則減去對(duì)應(yīng)的view
commentLayout.removeViews(commentList.size(), childCount - commentList.size());
}
//綁定數(shù)據(jù)
for (int n = 0; n < commentList.size(); n++) {
CommentWidget commentWidget = (CommentWidget) commentLayout.getChildAt(n);
if (commentWidget != null) commentWidget.setCommentText(commentList.get(n));
}
}
代碼更改量不大,我這里的邏輯是這樣的:
- 首先從池里面拿出可復(fù)用的對(duì)象
- 如果沒有,則創(chuàng)建一個(gè)對(duì)象
- 如果有,則直接使用這個(gè)對(duì)象
- 結(jié)合昨天的邏輯,判斷當(dāng)前view的數(shù)量與評(píng)論數(shù)量,不足,則用第一步的對(duì)象綁定數(shù)據(jù),超過,則removeView
- 在removeView的時(shí)候,對(duì)象進(jìn)池待復(fù)用。
這樣,我們就可以不需要每次都new出那么多對(duì)象了,在onChildViewRemoved的代碼我們這么寫:
@Override
public void onChildViewRemoved(View parent, View child) {
if (child instanceof CommentWidget)
COMMENT_TEXT_POOL.put((CommentWidget)child);
}
也許會(huì)有一個(gè)問題:
- view被remove后入池,但之后復(fù)用這個(gè)對(duì)象,那么與上一個(gè)viewholder會(huì)不會(huì)沖突
這里我也想過,但有一個(gè)地方需要注意,我們?cè)诨瑒?dòng)的時(shí)候,viewholder不同的話,對(duì)應(yīng)的實(shí)體類也是不同的,而我們每次都會(huì)重新綁定數(shù)據(jù),因此我們的對(duì)象是相同,但我們操作的數(shù)據(jù)是不同的,所以并不影響。
最后在put和get打上log看看我們有沒有成功拿到數(shù)據(jù):
app效果圖如下:

可以看得出,我們快速滑動(dòng)的時(shí)候并沒有出現(xiàn)明顯的卡頓,為了更方便測(cè)試,有些地方的評(píng)論都快10條了(gif錄制的時(shí)候我設(shè)置了延時(shí)0.1s)
然后我們監(jiān)測(cè)我們的Log...

可以看得出,在remove的時(shí)候差額view都成功入池,而取出的時(shí)候都可以拿到,實(shí)際上new出來的對(duì)象確實(shí)不多
下面的圖是對(duì)new對(duì)象的監(jiān)測(cè)

可以看到,在池空的時(shí)候創(chuàng)建了對(duì)象,池不空的時(shí)候創(chuàng)建次數(shù)就少了很多,這在一定程度上是有效果的。
下一步將會(huì)進(jìn)行內(nèi)容頁的填充。