一起撸个朋友圈吧(step5) - 控件篇【评论控件优化】

项目地址:github.com/razerdp/Fri…

上篇链接:http://www.jianshu.com/p/4cc3f9c8a713

下篇链接:http://www.jianshu.com/p/885538a261ea


在昨天的那篇文章,我们的评论控件的优化里提及过希望采用一个对象池来保存,碰巧,今天在看ViewGroup的代码的时候,发现了这么一个接口:

根据文档,可以知道这个接口在ViewGroup层次改变的时候会回调,回调两个方法:onChildViewAdded和onChildViewRemoved,这两个方法正是与addView和removeView挂钩。

在昨天我们提出过希望在removeView的时候回收,下次再add的时候从池里面拿,在今天看到这个接口后,在下就觉得-----是时候了。。。

首先我们构造一个简单的池,用数组包着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;
        }
    }
复制代码

我们主要实现三个方法:

  • put:入池(不是入栈哦)
  • get:出池
  • clear:清池

因为java木有指针,所以我们采用自己够早一个游标的方法来进行池对象的推入和获取。

在昨天的代码里,我们构造一个静态池,防止每次反射创建时被初始化。

//评论区的view对象池
private static final CommentPool COMMENT_TEXT_POOL=new CommentPool(20);
复制代码

存入的数量大概20条就足够了,接下来实现OnHierarchyChangeListener接口,然后改造我们的addCommentWidget方法:

  private void addCommentWidget(List<CommentInfo> commentList) {
        if (commentList == null || commentList.size() == 0) return;
        /**
         * 优化方案:
         * 因为是在listview里面,那么复用肯定有,意味着滑动的时候必须要removeView或者addView
         * 但为了性能提高,不可以直接removeAllViews
         * 于是采取以下方案:
         *    根据现有的view进行remove/add差额
         *    然后统一设置
         *
         * 2016-02-26:复用池进一步优化
         * */
        final int childCount = commentLayout.getChildCount();
        commentLayout.setOnHierarchyChangeListener(this);
        if (childCount < commentList.size()) {
            //当前的view少于list的长度,则补充相差的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()) {
            //当前的view的数目比list的长度大,则减去对应的view
            commentLayout.removeViews(commentList.size(), childCount - commentList.size());
        }
        //绑定数据
        for (int n = 0; n < commentList.size(); n++) {
            CommentWidget commentWidget = (CommentWidget) commentLayout.getChildAt(n);
            if (commentWidget != null) commentWidget.setCommentText(commentList.get(n));
        }
    }
复制代码

代码更改量不大,我这里的逻辑是这样的:

  • 首先从池里面拿出可复用的对象
    • 如果没有,则创建一个对象
    • 如果有,则直接使用这个对象
  • 结合昨天的逻辑,判断当前view的数量与评论数量,不足,则用第一步的对象绑定数据,超过,则removeView
  • 在removeView的时候,对象进池待复用。

这样,我们就可以不需要每次都new出那么多对象了,在onChildViewRemoved的代码我们这么写:

 @Override
    public void onChildViewRemoved(View parent, View child) {
        if (child instanceof CommentWidget)
            COMMENT_TEXT_POOL.put((CommentWidget)child);
    }
复制代码

也许会有一个问题:

  • view被remove后入池,但之后复用这个对象,那么与上一个viewholder会不会冲突

这里我也想过,但有一个地方需要注意,我们在滑动的时候,viewholder不同的话,对应的实体类也是不同的,而我们每次都会重新绑定数据,因此我们的对象是相同,但我们操作的数据是不同的,所以并不影响。

最后在put和get打上log看看我们有没有成功拿到数据:

app效果图如下:

可以看得出,我们快速滑动的时候并没有出现明显的卡顿,为了更方便测试,有些地方的评论都快10条了(gif录制的时候我设置了延时0.1s)

然后我们监测我们的Log...

可以看得出,在remove的时候差额view都成功入池,而取出的时候都可以拿到,实际上new出来的对象确实不多 下面的图是对new对象的监测

可以看到,在池空的时候创建了对象,池不空的时候创建次数就少了很多,这在一定程度上是有效果的。

下一步将会进行内容页的填充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值