PinnedHeaderExpandableListView的一些整理

ExpandableListView api

  1. 获取第一行所属的 groupPos 和 childPos
int firstVisiblePos = getFirstVisiblePosition();
int firstVisibleGroupPos = getPackedPositionGroup(getExpandableListPosition(firstVisiblePos));

//2
int firstVisiblePos = pointToPosition(0,0);
int firstVisibleGroupPos = getPackedPositionGroup(getExpandableListPosition(pos));

2.默认展开所有 group

for (int i = 0, count = expandableListView.getCount(); i < count; i++) {
    expandableListView.expandGroup(i);
}

3.将事件交给 StickLayout 处理

@Override
public boolean giveUpTouchEvent(MotionEvent event) {
    Log.d(TAG, "expandableListView firstvisible pos:" + expandableListView.getFirstVisiblePosition());
    if (expandableListView.getFirstVisiblePosition() == 0) {
        View view = expandableListView.getChildAt(0);
        Log.i(TAG, "firstvisible pos:0,first child top:"
                + view.getTop()
        );
        if (view != null && view.getTop() >= 0) {
            return true;
        }
    }

    Log.i(TAG, "expandableListView.getFirstVisiblePosition() != 0");
    return false;
}

4.根据 MotionEvent 得到点击范围内的 targetView

View mTouchTarget;

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mTouchTarget = getTouchTarget(mHeaderView, x, y);
    }
}

private View getTouchTarget(View view, int x, int y) {
    if (!(view instanceof ViewGroup)) {
        return view;
    }

    ViewGroup parent = (ViewGroup) view;
    int childrenCount = parent.getChildCount();
    final boolean customOrder = isChildrenDrawingOrderEnabled();
    View target = null;
    for (int i = childrenCount - 1; i >= 0; i--) {
        final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i;
        final View child = parent.getChildAt(childIndex);
        if (isTouchPointInView(child, x, y)) {
            target = child;
            break;
        }
    }
    if (target == null) {
        target = parent;
    }

    return target;
}


private boolean isTouchPointInView(View view, int x, int y) {
    if (view.isClickable() && y >= view.getTop() && y <= view.getBottom()
            && x >= view.getLeft() && x <= view.getRight()) {
        return true;
    }
    return false;
}

源码及解析地址

可下拉的PinnedHeaderExpandableListView的实现

推荐阅读更多精彩内容