欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android向上滑动隐藏Header头部动画

程序员文章站 2022-01-18 10:04:12
...

Android中、大家都用过ListView、ExpandableListView等、也许你还用过 PinnedHeaderListView、但是如果我说PinnedHeaderExpandableListView、你听过吗?还有可下拉的PinnedHeaderExpandableListView呢?没听过也不要紧、本文就是介绍这个东西的、为了让大家有更直观的了解、先上效果图

通过效果图可以看出、首先它是一个ExpandableListView、但是它的头部可以固定、其次、在它的上面还有一个头部可以来回伸缩、恩、这就是本 文要介绍的自定义view、为了提高复用性、这个效果我分成来了2个view来实现、第一个是 PinnedHeaderExpandableListView来实现头部固定的ExpandableListView、第二个view是 StickyLayout、这个view具有一个可以上下滑动的头部、最后将这2个view组合在一起、就达到了如下的效果

Android向上滑动隐藏Header头部动画


dispatchTouchEvent代码

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int x = (int) ev.getX();
    int y = (int) ev.getY();
    int pos = pointToPosition(x, y);
    if (mHeaderView != null && y >= mHeaderView.getTop() && y <= mHeaderView.getBottom()) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            mTouchTarget = getTouchTarget(mHeaderView, x, y);
            mActionDownHappened = true;
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            View touchTarget = getTouchTarget(mHeaderView, x, y);
            if (touchTarget == mTouchTarget && mTouchTarget.isClickable()) {
                mTouchTarget.performClick();
                invalidate(new Rect(0, 0, mHeaderWidth, mHeaderHeight));
            } else if (mIsHeaderGroupClickable){
                int groupPosition = getPackedPositionGroup(getExpandableListPosition(pos));
                if (groupPosition != INVALID_POSITION && mActionDownHappened) {
                    if (isGroupExpanded(groupPosition)) {
                        collapseGroup(groupPosition);
                    } else {
                        expandGroup(groupPosition);
                    }
                }
            }
            mActionDownHappened = false;
        }
        return true;
    }

    return super.dispatchTouchEvent(ev);
}


setOnHeaderUpdateListener代码

public void setOnHeaderUpdateListener(OnHeaderUpdateListener listener) {
    mHeaderUpdateListener = listener;
    if (listener == null) {
        mHeaderView = null;
        mHeaderWidth = mHeaderHeight = 0;
        return;
    }
    mHeaderView = listener.getPinnedHeader();
    int firstVisiblePos = getFirstVisiblePosition();
    int firstVisibleGroupPos = getPackedPositionGroup(getExpandableListPosition(firstVisiblePos));
    listener.updatePinnedHeader(mHeaderView, firstVisibleGroupPos);
    requestLayout();
    postInvalidate();
}


android滑动隐藏头部源代码下载链接: android 上滑隐藏头部 密码: jcpx