ViewGroup的TouchTarget设计思维

2023-05-20,,

TouchTarget需要创建的时候先看下池子里面有没有被回收上来的,如果没有可以利用的就给你创建一个新的,否则就从池子里面把头部的那个可以利用的给你,你把你的东西放进去就行了。回收的时候就是把你放在池子的头部,并清空你里面的东西,防止你重复的回收(一次就好)

  public static TouchTarget obtain(@NonNull View child, int pointerIdBits) {
if (child == null) {
throw new IllegalArgumentException("child must be non-null");
} final TouchTarget target;
synchronized (sRecycleLock) {
//链表的头部为空说明池子里面没有被回收上来的touchTagget对象,那么就直接创建,只要你需要就满足你
if (sRecycleBin == null) {
target = new TouchTarget();
} else {
//从池子里面去拿出来,做一个链表的减员操作
target = sRecycleBin;
sRecycleBin = target.next;
sRecycledCount--;
target.next = null;
}
}
//填充好你自己的东西就好了
target.child = child;
target.pointerIdBits = pointerIdBits;
return target;
} public void recycle() {
//回收一次后就会把child置空,再次回收就给你报错
if (child == null) {
throw new IllegalStateException("already recycled once");
} synchronized (sRecycleLock) {
if (sRecycledCount < MAX_RECYCLED) {
//池子放的下话就把你放在链表的头部,做一个链表的插入操作
next = sRecycleBin;
sRecycleBin = this;
sRecycledCount += 1;
} else {
//回收池已经满了,你自己被跟我们关联了,处于游离状态等待垃圾回收吧
next = null;
}
//把自己的东西置空
child = null;
}
}

ViewGroup的TouchTarget设计思维的相关教程结束。

《ViewGroup的TouchTarget设计思维.doc》

下载本文的Word格式文档,以方便收藏与打印。