Android--ExpandbleView源码学习----ExpandableListPosition类

本文深入探讨了ExpandableListPosition类在Android列表适配器中的使用方式,包括如何获取子项和组项的位置以及通过位操作表示它们的位置。详细解释了类的成员方法和实例化过程,提供了实际应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Class OverView

ExpandbaleListPosition类可以指向group的位置或则child的位置。可通过obtainChildPosition(int,int)和obtainGroupPosition(int)获得实例

/***根据type返回groupPosition或则childPosition的格式为64位的数据**/long getPackedPosition() { if (type == CHILD) return ExpandableListView.getPackedPositionForChild(groupPos, childPos); else return ExpandableListView.getPackedPositionForGroup(groupPos); }//static ExpandableListPosition obtain(int type, int groupPos, int childPos, int flatListPos) { ExpandableListPosition elp = getRecycledOrCreate(); elp.type = type; elp.groupPos = groupPos; elp.childPos = childPos; elp.flatListPos = flatListPos; return elp; }
//初始化对象
private void resetState() { groupPos = 0; childPos = 0; flatListPos = 0; type = 0; }


//返回一个ExpandableListPosition,如果池中有对象则返回池中对象并且移除该对象(将被初始化),如果没有则新new一个对象返回   private static ExpandableListPosition getRecycledOrCreate() {
        ExpandableListPosition elp;
        synchronized (sPool) {
            if (sPool.size() > 0) {
                elp = sPool.remove(0);
            } else {
                return new ExpandableListPosition();
            }
        }
        elp.resetState();
        return elp;
    }//增加一个ExpandableListPosition对象到池中
public void recycle() {
        synchronized (sPool) {
            if (sPool.size() < MAX_POOL_SIZE) {
                sPool.add(this);
            }
        }
    }


//获得一个ExpandableListPosition对象

static ExpandableListPosition obtain(int type, int groupPos, int childPos, int flatListPos) {
    ExpandableListPosition elp = getRecycledOrCreate();
    elp.type = type;
    elp.groupPos = groupPos;
    elp.childPos = childPos;
    elp.flatListPos = flatListPos;
    return elp;
  }
//获得一个group类型的ExpandableListPosition对象static ExpandableListPosition obtainGroupPosition(int groupPosition) {
        return obtain(GROUP, groupPosition, 0, 0);
    }
 
//获得一个child类型的ExpandableListPosition对象   
 static ExpandableListPosition obtainChildPosition(int groupPosition, int childPosition) { return obtain(CHILD, groupPosition, childPosition, 0); }

//根据位置的封装数据得到位置的ExpandbaleListPostion static ExpandableListPosition obtainPosition(long packedPosition) { if (packedPosition == ExpandableListView.PACKED_POSITION_VALUE_NULL) { return null; } ExpandableListPosition elp = getRecycledOrCreate(); elp.groupPos = ExpandableListView.getPackedPositionGroup(packedPosition); if (ExpandableListView.getPackedPositionType(packedPosition) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { elp.type = CHILD; elp.childPos = ExpandableListView.getPackedPositionChild(packedPosition); } else { elp.type = GROUP; } return elp; }








                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值