daimajia / AndroidSwipeLayout

The Most Powerful Swipe Layout!
MIT License
12.38k stars 2.67k forks source link

Recycle of SwipeLayout with match parent width corrupts layout of bottom view #238

Open fprt77 opened 8 years ago

fprt77 commented 8 years ago

I suggest you to add measure call to beginning of onLayout function (before bottom views are getting updated).

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        /* Call measure previous to any getMeasuredWidth/getMeasuredHeight calls to avoid
        problems with "wrap_content" or "match_parent" layout params */
        measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        updateBottomViews();

        if (mOnLayoutListeners != null) for (int i = 0; i < mOnLayoutListeners.size(); i++) {
            mOnLayoutListeners.get(i).onLayout(this);
        }
    }

Without this call, update process will get wrong values from getMeasuredWidth in computeSurfaceLayoutArea when width of SwipeLayout is either "match_parent" or "wrap_content" and SwipeLayout is recycled.

    private Rect computeSurfaceLayoutArea(boolean open) {
        int l = getPaddingLeft(), t = getPaddingTop();
        if (open) {
            if (mCurrentDragEdge == DragEdge.Left)
                l = getPaddingLeft() + mDragDistance;
            else if (mCurrentDragEdge == DragEdge.Right)
                l = getPaddingLeft() - mDragDistance;
            else if (mCurrentDragEdge == DragEdge.Top)
                t = getPaddingTop() + mDragDistance;
            else t = getPaddingTop() - mDragDistance;
        }
        /* without measure being called previously, getMeasuredWidth will return 16777214 when
        wrap_content is specified, or 16777215 when match_parent is specified*/
        return new Rect(l, t, l + getMeasuredWidth(), t + getMeasuredHeight());
    }
    private Rect computeBottomLayoutAreaViaSurface(ShowMode mode, Rect surfaceArea) {
        Rect rect = surfaceArea;
        View bottomView = getCurrentBottomView();
        /* When incorrect surfaceArea is passed (rect.right value 16777214 or 16777215),
        then left is computed from this incorrect value, what results in bottom view being
        laid out too far away from visible part of screen. */
        int bl = rect.left, bt = rect.top, br = rect.right, bb = rect.bottom;
        /* rest of function*/
    }