Pinned groups in ExpandableListView

前端 未结 5 510
Happy的楠姐
Happy的楠姐 2020-12-24 04:10

Is there a standard way to pin group item to the top of screen while scrolling group\'s items. I saw similar examples with ListView. What interfaces should I implement or wh

5条回答
  •  抹茶落季
    2020-12-24 04:24

    in Homo Incognito's answer, child view in pinned head view can't click and receive the click event, but i found a way. I put the code at: https://github.com/chenjishi/PinnedHeadListView

    private final Rect mRect = new Rect();
    private final int[] mLocation = new int[2];
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mHeaderView == null) return super.dispatchTouchEvent(ev);
    
        if (mHeaderViewVisible) {
            final int x = (int) ev.getX();
            final int y = (int) ev.getY();
            mHeaderView.getLocationOnScreen(mLocation);
            mRect.left = mLocation[0];
            mRect.top = mLocation[1];
            mRect.right = mLocation[0] + mHeaderView.getWidth();
            mRect.bottom = mLocation[1] + mHeaderView.getHeight();
    
            if (mRect.contains(x, y)) {
                if (ev.getAction() == MotionEvent.ACTION_UP) {
                    performViewClick(x, y);
                }
                return true;
            } else {
                return super.dispatchTouchEvent(ev);
            }
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }
    
    private void performViewClick(int x, int y) {
        if (null == mHeaderView) return;
    
        final ViewGroup container = (ViewGroup) mHeaderView;
        for (int i = 0; i < container.getChildCount(); i++) {
            View view = container.getChildAt(i);
    
            /**
             * transform coordinate to find the child view we clicked
             * getGlobalVisibleRect used for android 2.x, getLocalVisibleRect
             * user for 3.x or above, maybe it's a bug
             */
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
                view.getGlobalVisibleRect(mRect);
            } else {
                view.getLocalVisibleRect(mRect);
                int width = mRect.right - mRect.left;
                mRect.left = Math.abs(mRect.left);
                mRect.right = mRect.left + width;
            }
    
            if (mRect.contains(x, y)) {
                view.performClick();
                break;
            }
        }
    }
    

    this is the way i handle the click event in pinned view, override dispatchTouchEvent.

    enter image description here

提交回复
热议问题