daimajia / AndroidSwipeLayout

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

swipeLayout.addSwipeListener causing deletion of multiple items in BaseSwipeAdapter #408

Open RitwickVerma opened 7 years ago

RitwickVerma commented 7 years ago

I have extended BaseSwipeAdapter and in one similar issue, I read the problem was caused by NOT using setSwipeAdapter and using addSwipeAdapter instead. That function is a part of SwipeLayout and not BaseSwipeAdapter. Another solution was using swipeLayout.removeAllSwipeListeners, which again wasn't available. Also, swipeLayout.close() isn't working either. Now even after using a runnable. Here's the code:

` class schedulecontactsAdapter extends BaseSwipeAdapter {

Context context;
View convertView;

private ArrayList<Bundle> schedinfo;
private TextView listemptytext;
private String number = "",name = "",photo = "",time="",date="",dialnumber="";
private long timeinmills;
private ArrayList<String> diffnums;

private static LayoutInflater inflater = null;

schedulecontactsAdapter(Context context, ArrayList<Bundle> schedinfo, TextView listemptytext)
{
    // TODO Auto-generated constructor stub
    this.context = context;
    this.schedinfo=schedinfo;
    this.listemptytext=listemptytext;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getSwipeLayoutResourceId(int position)
{
    return R.id.swipe;
}

@Override
public View generateView(final int position, final ViewGroup parent)
{
        //vi = inflater.inflate(R.layout.row_in_schedulelist,parent,false);

    View vi = LayoutInflater.from(context).inflate(R.layout.row_in_schedulelist, parent,false);

    return vi;

}

public void fillValues(final int position, View convertView)
{
    View vi=convertView;
    final SwipeLayout swipeLayout = (SwipeLayout) vi.findViewById(R.id.swipe);

    TextView schedinfotextview=(TextView) vi.findViewById(R.id.schedinfotextview);
    ImageView schcontactphoto=(ImageView) vi.findViewById(R.id.schlistcontactphotopic);

    Bundle b=schedinfo.get(position);
    if(b!=null)
    {
        name = b.getString("name");
        diffnums = b.getStringArrayList("numbers");
        dialnumber = b.getString("dialnumber");
        photo = b.getString("photo");
        time = b.getString("time");
        date = b.getString("date");
        timeinmills = b.getLong("timeinmills");

        String temp = "Will call " + name + "\nat " + time + " of " + date + " on\nnumber " + dialnumber;
        schedinfotextview.setText(temp);

        schcontactphoto.setImageURI(Uri.parse(photo));
    }

    swipeLayout.addSwipeListener(new SimpleSwipeListener() {
        @Override
        public void onOpen(SwipeLayout layout)
        {
            final Bundle temp=schedinfo.get(position);
            schedinfo.remove(position);
            notifyDataSetChanged();
            Snackbar.make(swipeLayout,"Schedule cancelled", Snackbar.LENGTH_INDEFINITE)
                    .setAction("Undo", new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            schedinfo.add(position,temp);
                            notifyDataSetChanged();
                            ((SwipeLayout)v).close(true);
                        }
                    })
                    .setActionTextColor(Color.YELLOW)
                    .show();
        }

    });

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return schedinfo.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return schedinfo.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

} `

vaibhavjain93 commented 7 years ago

I have created my own CustomSwipeLayout Class with a method removeAllSwipeListeners()

RitwickVerma commented 7 years ago

Can you post the code because I really didn't get it in the v1.2

vaibhavjain93 commented 7 years ago
public class CustomSwipeLayout extends FrameLayout {
@Deprecated
public static final int EMPTY_LAYOUT = -1;
private static final int DRAG_LEFT = 1;
private static final int DRAG_RIGHT = 2;
private static final int DRAG_TOP = 4;
private static final int DRAG_BOTTOM = 8;
private static final CustomSwipeLayout.DragEdge DefaultDragEdge = CustomSwipeLayout.DragEdge.Right;

private int mTouchSlop;

private CustomSwipeLayout.DragEdge mCurrentDragEdge = DefaultDragEdge;
private ViewDragHelper mDragHelper;

private int mDragDistance = 0;
private LinkedHashMap<CustomSwipeLayout.DragEdge, View> mDragEdges = new LinkedHashMap<CustomSwipeLayout.DragEdge, View>();
private CustomSwipeLayout.ShowMode mShowMode;

private float[] mEdgeSwipesOffset = new float[4];

private List<CustomSwipeLayout.SwipeListener> mSwipeListeners = new ArrayList<CustomSwipeLayout.SwipeListener>();
private List<CustomSwipeLayout.SwipeDenier> mSwipeDeniers = new ArrayList<CustomSwipeLayout.SwipeDenier>();
private Map<View, ArrayList<CustomSwipeLayout.OnRevealListener>> mRevealListeners = new HashMap<View, ArrayList<CustomSwipeLayout.OnRevealListener>>();
private Map<View, Boolean> mShowEntirely = new HashMap<View, Boolean>();

private CustomSwipeLayout.DoubleClickListener mDoubleClickListener;

private boolean mSwipeEnabled = true;
private boolean[] mSwipesEnabled = new boolean[]{true, true, true, true};
private boolean mClickToClose = false;

public static enum DragEdge {
    Left,
    Top,
    Right,
    Bottom
}

public static enum ShowMode {
    LayDown,
    PullOut
}

public CustomSwipeLayout(Context context) {
    this(context, null);
}

public CustomSwipeLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomSwipeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
    mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

    TypedArray a = context.obtainStyledAttributes(attrs, com.daimajia.swipe.R.styleable.SwipeLayout);
    int dragEdgeChoices = a.getInt(com.daimajia.swipe.R.styleable.SwipeLayout_drag_edge, DRAG_RIGHT);
    mEdgeSwipesOffset[CustomSwipeLayout.DragEdge.Left.ordinal()] = a.getDimension(com.daimajia.swipe.R.styleable.SwipeLayout_leftEdgeSwipeOffset, 0);
    mEdgeSwipesOffset[CustomSwipeLayout.DragEdge.Right.ordinal()] = a.getDimension(com.daimajia.swipe.R.styleable.SwipeLayout_rightEdgeSwipeOffset, 0);
    mEdgeSwipesOffset[CustomSwipeLayout.DragEdge.Top.ordinal()] = a.getDimension(com.daimajia.swipe.R.styleable.SwipeLayout_topEdgeSwipeOffset, 0);
    mEdgeSwipesOffset[CustomSwipeLayout.DragEdge.Bottom.ordinal()] = a.getDimension(com.daimajia.swipe.R.styleable.SwipeLayout_bottomEdgeSwipeOffset, 0);
    setClickToClose(a.getBoolean(com.daimajia.swipe.R.styleable.SwipeLayout_clickToClose, mClickToClose));

    if ((dragEdgeChoices & DRAG_LEFT) == DRAG_LEFT) {
        mDragEdges.put(CustomSwipeLayout.DragEdge.Left, null);
    }
    if ((dragEdgeChoices & DRAG_TOP) == DRAG_TOP) {
        mDragEdges.put(CustomSwipeLayout.DragEdge.Top, null);
    }
    if ((dragEdgeChoices & DRAG_RIGHT) == DRAG_RIGHT) {
        mDragEdges.put(CustomSwipeLayout.DragEdge.Right, null);
    }
    if ((dragEdgeChoices & DRAG_BOTTOM) == DRAG_BOTTOM) {
        mDragEdges.put(CustomSwipeLayout.DragEdge.Bottom, null);
    }
    int ordinal = a.getInt(com.daimajia.swipe.R.styleable.SwipeLayout_show_mode, CustomSwipeLayout.ShowMode.PullOut.ordinal());
    mShowMode = CustomSwipeLayout.ShowMode.values()[ordinal];
    a.recycle();

}

public interface SwipeListener {
    public void onStartOpen(CustomSwipeLayout layout);

    public void onOpen(CustomSwipeLayout layout);

    public void onStartClose(CustomSwipeLayout layout);

    public void onClose(CustomSwipeLayout layout);

    public void onUpdate(CustomSwipeLayout layout, int leftOffset, int topOffset);

    public void onHandRelease(CustomSwipeLayout layout, float xvel, float yvel);
}

public void addSwipeListener(CustomSwipeLayout.SwipeListener l) {
    mSwipeListeners.add(l);
}

public void removeAllSwipeListener() {
    mSwipeListeners.clear();
}
public void removeSwipeListener(CustomSwipeLayout.SwipeListener l) {
    mSwipeListeners.remove(l);
}

public static interface SwipeDenier {
    /*
     * Called in onInterceptTouchEvent Determines if this swipe event should
     * be denied Implement this interface if you are using views with swipe
     * gestures As a child of CustomSwipeLayout
     * 
     * @return true deny false allow
     */
    public boolean shouldDenySwipe(MotionEvent ev);
}

public void addSwipeDenier(CustomSwipeLayout.SwipeDenier denier) {
    mSwipeDeniers.add(denier);
}

public void removeSwipeDenier(CustomSwipeLayout.SwipeDenier denier) {
    mSwipeDeniers.remove(denier);
}

public void removeAllSwipeDeniers() {
    mSwipeDeniers.clear();
}

public interface OnRevealListener {
    public void onReveal(View child, CustomSwipeLayout.DragEdge edge, float fraction, int distance);
}

/**
 * bind a view with a specific
 * {@link CustomSwipeLayout.OnRevealListener}
 *
 * @param childId the view id.
 * @param l       the target
 *                {@link CustomSwipeLayout.OnRevealListener}
 */
public void addRevealListener(int childId, CustomSwipeLayout.OnRevealListener l) {
    View child = findViewById(childId);
    if (child == null) {
        throw new IllegalArgumentException("Child does not belong to SwipeListener.");
    }

    if (!mShowEntirely.containsKey(child)) {
        mShowEntirely.put(child, false);
    }
    if (mRevealListeners.get(child) == null)
        mRevealListeners.put(child, new ArrayList<CustomSwipeLayout.OnRevealListener>());

    mRevealListeners.get(child).add(l);
}

/**
 * bind multiple views with an
 * {@link CustomSwipeLayout.OnRevealListener}.
 *
 * @param childIds the view id.
 * @param l        the {@link CustomSwipeLayout.OnRevealListener}
 */
public void addRevealListener(int[] childIds, CustomSwipeLayout.OnRevealListener l) {
    for (int i : childIds)
        addRevealListener(i, l);
}

public void removeRevealListener(int childId, CustomSwipeLayout.OnRevealListener l) {
    View child = findViewById(childId);

    if (child == null) return;

    mShowEntirely.remove(child);
    if (mRevealListeners.containsKey(child)) mRevealListeners.get(child).remove(l);
}

public void removeAllRevealListeners(int childId) {
    View child = findViewById(childId);
    if (child != null) {
        mRevealListeners.remove(child);
        mShowEntirely.remove(child);
    }
}

private ViewDragHelper.Callback mDragHelperCallback = new ViewDragHelper.Callback() {

    @Override
    public int clampViewPositionHorizontal(View child, int left, int dx) {
        if (child == getSurfaceView()) {
            switch (mCurrentDragEdge) {
                case Top:
                case Bottom:
                    return getPaddingLeft();
                case Left:
                    if (left < getPaddingLeft()) return getPaddingLeft();
                    if (left > getPaddingLeft() + mDragDistance)
                        return getPaddingLeft() + mDragDistance;
                    break;
                case Right:
                    if (left > getPaddingLeft()) return getPaddingLeft();
                    if (left < getPaddingLeft() - mDragDistance)
                        return getPaddingLeft() - mDragDistance;
                    break;
            }
        } else if (getCurrentBottomView() == child) {

            switch (mCurrentDragEdge) {
                case Top:
                case Bottom:
                    return getPaddingLeft();
                case Left:
                    if (mShowMode == CustomSwipeLayout.ShowMode.PullOut) {
                        if (left > getPaddingLeft()) return getPaddingLeft();
                    }
                    break;
                case Right:
                    if (mShowMode == CustomSwipeLayout.ShowMode.PullOut) {
                        if (left < getMeasuredWidth() - mDragDistance) {
                            return getMeasuredWidth() - mDragDistance;
                        }
                    }
                    break;
            }
        }
        return left;
    }

    @Override
    public int clampViewPositionVertical(View child, int top, int dy) {
        if (child == getSurfaceView()) {
            switch (mCurrentDragEdge) {
                case Left:
                case Right:
                    return getPaddingTop();
                case Top:
                    if (top < getPaddingTop()) return getPaddingTop();
                    if (top > getPaddingTop() + mDragDistance)
                        return getPaddingTop() + mDragDistance;
                    break;
                case Bottom:
                    if (top < getPaddingTop() - mDragDistance) {
                        return getPaddingTop() - mDragDistance;
                    }
                    if (top > getPaddingTop()) {
                        return getPaddingTop();
                    }
            }
        } else {
            View surfaceView = getSurfaceView();
            int surfaceViewTop = surfaceView==null?0:surfaceView.getTop();
            switch (mCurrentDragEdge) {
                case Left:
                case Right:
                    return getPaddingTop();
                case Top:
                    if (mShowMode == CustomSwipeLayout.ShowMode.PullOut) {
                        if (top > getPaddingTop()) return getPaddingTop();
                    } else {
                        if (surfaceViewTop + dy < getPaddingTop())
                            return getPaddingTop();
                        if (surfaceViewTop + dy > getPaddingTop() + mDragDistance)
                            return getPaddingTop() + mDragDistance;
                    }
                    break;
                case Bottom:
                    if (mShowMode == CustomSwipeLayout.ShowMode.PullOut) {
                        if (top < getMeasuredHeight() - mDragDistance)
                            return getMeasuredHeight() - mDragDistance;
                    } else {
                        if (surfaceViewTop + dy >= getPaddingTop())
                            return getPaddingTop();
                        if (surfaceViewTop + dy <= getPaddingTop() - mDragDistance)
                            return getPaddingTop() - mDragDistance;
                    }
            }
        }
        return top;
    }

    @Override
    public boolean tryCaptureView(View child, int pointerId) {
        boolean result = child == getSurfaceView() || getBottomViews().contains(child);
        if(result){
            isCloseBeforeDrag = getOpenStatus() == CustomSwipeLayout.Status.Close;
        }
        return result;
    }

    @Override
    public int getViewHorizontalDragRange(View child) {
        return mDragDistance;
    }

    @Override
    public int getViewVerticalDragRange(View child) {
        return mDragDistance;
    }

    boolean isCloseBeforeDrag = true;
    @Override
    public void onViewReleased(View releasedChild, float xvel, float yvel) {
        super.onViewReleased(releasedChild, xvel, yvel);
        for (CustomSwipeLayout.SwipeListener l : mSwipeListeners){
            l.onHandRelease(CustomSwipeLayout.this, xvel, yvel);
        }
        processHandRelease(xvel, yvel, isCloseBeforeDrag);

        invalidate();
    }

    @Override
    public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
        View surfaceView = getSurfaceView();
        if(surfaceView==null) return;
        View currentBottomView = getCurrentBottomView();
        int evLeft = surfaceView.getLeft(),
                evRight = surfaceView.getRight(),
                evTop = surfaceView.getTop(),
                evBottom = surfaceView.getBottom();
        if (changedView == surfaceView) {

            if (mShowMode == CustomSwipeLayout.ShowMode.PullOut && currentBottomView!=null) {
                if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left || mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right){
                    currentBottomView.offsetLeftAndRight(dx);
                } else {
                    currentBottomView.offsetTopAndBottom(dy);
                }
            }

        } else if (getBottomViews().contains(changedView)) {

            if (mShowMode == CustomSwipeLayout.ShowMode.PullOut) {
                surfaceView.offsetLeftAndRight(dx);
                surfaceView.offsetTopAndBottom(dy);
            } else {
                Rect rect = computeBottomLayDown(mCurrentDragEdge);
                if(currentBottomView!=null){
                    currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
                }

                int newLeft = surfaceView.getLeft() + dx, newTop = surfaceView.getTop() + dy;

                if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left && newLeft < getPaddingLeft())
                    newLeft = getPaddingLeft();
                else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right && newLeft > getPaddingLeft())
                    newLeft = getPaddingLeft();
                else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Top && newTop < getPaddingTop())
                    newTop = getPaddingTop();
                else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Bottom && newTop > getPaddingTop())
                    newTop = getPaddingTop();

                surfaceView.layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight());
            }
        }

        dispatchRevealEvent(evLeft, evTop, evRight, evBottom);

        dispatchSwipeEvent(evLeft, evTop, dx, dy);

        invalidate();
    }
};

/**
 * the dispatchRevealEvent method may not always get accurate position, it
 * makes the view may not always get the event when the view is totally
 * show( fraction = 1), so , we need to calculate every time.
 */
protected boolean isViewTotallyFirstShowed(View child, Rect relativePosition, CustomSwipeLayout.DragEdge edge, int surfaceLeft,
                                           int surfaceTop, int surfaceRight, int surfaceBottom) {
    if (mShowEntirely.get(child)) return false;
    int childLeft = relativePosition.left;
    int childRight = relativePosition.right;
    int childTop = relativePosition.top;
    int childBottom = relativePosition.bottom;
    boolean r = false;
    if (getShowMode() == CustomSwipeLayout.ShowMode.LayDown) {
        if ((edge == CustomSwipeLayout.DragEdge.Right && surfaceRight <= childLeft)
                || (edge == CustomSwipeLayout.DragEdge.Left && surfaceLeft >= childRight)
                || (edge == CustomSwipeLayout.DragEdge.Top && surfaceTop >= childBottom)
                || (edge == CustomSwipeLayout.DragEdge.Bottom && surfaceBottom <= childTop)) r = true;
    } else if (getShowMode() == CustomSwipeLayout.ShowMode.PullOut) {
        if ((edge == CustomSwipeLayout.DragEdge.Right && childRight <= getWidth())
                || (edge == CustomSwipeLayout.DragEdge.Left && childLeft >= getPaddingLeft())
                || (edge == CustomSwipeLayout.DragEdge.Top && childTop >= getPaddingTop())
                || (edge == CustomSwipeLayout.DragEdge.Bottom && childBottom <= getHeight())) r = true;
    }
    return r;
}

protected boolean isViewShowing(View child, Rect relativePosition, CustomSwipeLayout.DragEdge availableEdge, int surfaceLeft,
                                int surfaceTop, int surfaceRight, int surfaceBottom) {
    int childLeft = relativePosition.left;
    int childRight = relativePosition.right;
    int childTop = relativePosition.top;
    int childBottom = relativePosition.bottom;
    if (getShowMode() == CustomSwipeLayout.ShowMode.LayDown) {
        switch (availableEdge) {
            case Right:
                if (surfaceRight > childLeft && surfaceRight <= childRight) {
                    return true;
                }
                break;
            case Left:
                if (surfaceLeft < childRight && surfaceLeft >= childLeft) {
                    return true;
                }
                break;
            case Top:
                if (surfaceTop >= childTop && surfaceTop < childBottom) {
                    return true;
                }
                break;
            case Bottom:
                if (surfaceBottom > childTop && surfaceBottom <= childBottom) {
                    return true;
                }
                break;
        }
    } else if (getShowMode() == CustomSwipeLayout.ShowMode.PullOut) {
        switch (availableEdge) {
            case Right:
                if (childLeft <= getWidth() && childRight > getWidth()) return true;
                break;
            case Left:
                if (childRight >= getPaddingLeft() && childLeft < getPaddingLeft()) return true;
                break;
            case Top:
                if (childTop < getPaddingTop() && childBottom >= getPaddingTop()) return true;
                break;
            case Bottom:
                if (childTop < getHeight() && childTop >= getPaddingTop()) return true;
                break;
        }
    }
    return false;
}

protected Rect getRelativePosition(View child) {
    View t = child;
    Rect r = new Rect(t.getLeft(), t.getTop(), 0, 0);
    while (t.getParent() != null && t != getRootView()) {
        t = (View) t.getParent();
        if (t == this) break;
        r.left += t.getLeft();
        r.top += t.getTop();
    }
    r.right = r.left + child.getMeasuredWidth();
    r.bottom = r.top + child.getMeasuredHeight();
    return r;
}

private int mEventCounter = 0;

protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy) {
    CustomSwipeLayout.DragEdge edge = getDragEdge();
    boolean open = true;
    if (edge == CustomSwipeLayout.DragEdge.Left) {
        if (dx < 0) open = false;
    } else if (edge == CustomSwipeLayout.DragEdge.Right) {
        if (dx > 0) open = false;
    } else if (edge == CustomSwipeLayout.DragEdge.Top) {
        if (dy < 0) open = false;
    } else if (edge == CustomSwipeLayout.DragEdge.Bottom) {
        if (dy > 0) open = false;
    }

    dispatchSwipeEvent(surfaceLeft, surfaceTop, open);
}

protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open) {
    safeBottomView();
    CustomSwipeLayout.Status status = getOpenStatus();

    if (!mSwipeListeners.isEmpty()) {
        mEventCounter++;
        for (CustomSwipeLayout.SwipeListener l : mSwipeListeners) {
            if (mEventCounter == 1) {
                if (open) {
                    l.onStartOpen(this);
                } else {
                    l.onStartClose(this);
                }
            }
            l.onUpdate(CustomSwipeLayout.this, surfaceLeft - getPaddingLeft(), surfaceTop - getPaddingTop());
        }

        if (status == CustomSwipeLayout.Status.Close) {
            for (CustomSwipeLayout.SwipeListener l : mSwipeListeners) {
                l.onClose(CustomSwipeLayout.this);
            }
            mEventCounter = 0;
        }

        if (status == CustomSwipeLayout.Status.Open) {
            View currentBottomView = getCurrentBottomView();
            if(currentBottomView!=null){
                currentBottomView.setEnabled(true);
            }
            for (CustomSwipeLayout.SwipeListener l : mSwipeListeners) {
                l.onOpen(CustomSwipeLayout.this);
            }
            mEventCounter = 0;
        }
    }
}

/**
 * prevent bottom view get any touch event. Especially in LayDown mode.
 */
private void safeBottomView() {
    CustomSwipeLayout.Status status = getOpenStatus();
    List<View> bottoms = getBottomViews();

    if (status == CustomSwipeLayout.Status.Close) {
        for (View bottom : bottoms) {
            if (bottom!=null && bottom.getVisibility() != INVISIBLE){
                bottom.setVisibility(INVISIBLE);
            }
        }
    } else {
        View currentBottomView = getCurrentBottomView();
        if (currentBottomView!=null && currentBottomView.getVisibility() != VISIBLE){
            currentBottomView.setVisibility(VISIBLE);
        }
    }
}

protected void dispatchRevealEvent(final int surfaceLeft, final int surfaceTop, final int surfaceRight,
                                   final int surfaceBottom) {
    if (mRevealListeners.isEmpty()) return;
    for (Map.Entry<View, ArrayList<CustomSwipeLayout.OnRevealListener>> entry : mRevealListeners.entrySet()) {
        View child = entry.getKey();
        Rect rect = getRelativePosition(child);
        if (isViewShowing(child, rect, mCurrentDragEdge, surfaceLeft, surfaceTop,
                surfaceRight, surfaceBottom)) {
            mShowEntirely.put(child, false);
            int distance = 0;
            float fraction = 0f;
            if (getShowMode() == CustomSwipeLayout.ShowMode.LayDown) {
                switch (mCurrentDragEdge) {
                    case Left:
                        distance = rect.left - surfaceLeft;
                        fraction = distance / (float) child.getWidth();
                        break;
                    case Right:
                        distance = rect.right - surfaceRight;
                        fraction = distance / (float) child.getWidth();
                        break;
                    case Top:
                        distance = rect.top - surfaceTop;
                        fraction = distance / (float) child.getHeight();
                        break;
                    case Bottom:
                        distance = rect.bottom - surfaceBottom;
                        fraction = distance / (float) child.getHeight();
                        break;
                }
            } else if (getShowMode() == CustomSwipeLayout.ShowMode.PullOut) {
                switch (mCurrentDragEdge) {
                    case Left:
                        distance = rect.right - getPaddingLeft();
                        fraction = distance / (float) child.getWidth();
                        break;
                    case Right:
                        distance = rect.left - getWidth();
                        fraction = distance / (float) child.getWidth();
                        break;
                    case Top:
                        distance = rect.bottom - getPaddingTop();
                        fraction = distance / (float) child.getHeight();
                        break;
                    case Bottom:
                        distance = rect.top - getHeight();
                        fraction = distance / (float) child.getHeight();
                        break;
                }
            }

            for (CustomSwipeLayout.OnRevealListener l : entry.getValue()) {
                l.onReveal(child, mCurrentDragEdge, Math.abs(fraction), distance);
                if (Math.abs(fraction) == 1) {
                    mShowEntirely.put(child, true);
                }
            }
        }

        if (isViewTotallyFirstShowed(child, rect, mCurrentDragEdge, surfaceLeft, surfaceTop,
                surfaceRight, surfaceBottom)) {
            mShowEntirely.put(child, true);
            for (CustomSwipeLayout.OnRevealListener l : entry.getValue()) {
                if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left
                        || mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right)
                    l.onReveal(child, mCurrentDragEdge, 1, child.getWidth());
                else
                    l.onReveal(child, mCurrentDragEdge, 1, child.getHeight());
            }
        }

    }
}

@Override
public void computeScroll() {
    super.computeScroll();
    if (mDragHelper.continueSettling(true)) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

/**
 * {@link android.view.View.OnLayoutChangeListener} added in API 11. I need
 * to support it from API 8.
 */
public interface OnLayout {
    public void onLayout(CustomSwipeLayout v);
}

private List<CustomSwipeLayout.OnLayout> mOnLayoutListeners;

public void addOnLayoutListener(CustomSwipeLayout.OnLayout l) {
    if (mOnLayoutListeners == null) mOnLayoutListeners = new ArrayList<CustomSwipeLayout.OnLayout>();
    mOnLayoutListeners.add(l);
}

public void removeOnLayoutListener(CustomSwipeLayout.OnLayout l) {
    if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l);
}
public void addDrag(CustomSwipeLayout.DragEdge dragEdge, View child){
    addDrag(dragEdge, child, null);
}
public void addDrag(CustomSwipeLayout.DragEdge dragEdge, View child, ViewGroup.LayoutParams params){
    if(params==null){
        params = generateDefaultLayoutParams();
    }
    if(!checkLayoutParams(params)){
        params = generateLayoutParams(params);
    }
    int gravity = -1;
    switch (dragEdge){
        case Left:gravity = Gravity.LEFT;break;
        case Right:gravity = Gravity.RIGHT;break;
        case Top:gravity = Gravity.TOP;break;
        case Bottom:gravity = Gravity.BOTTOM;break;
    }
    if(params instanceof FrameLayout.LayoutParams){
        ((LayoutParams) params).gravity = gravity;
    }
    addView(child, 0, params);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    int gravity = Gravity.NO_GRAVITY;
    try {
        gravity = (Integer) params.getClass().getField("gravity").get(params);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if(gravity>0){
        gravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));

        if((gravity & Gravity.LEFT) == Gravity.LEFT){
            mDragEdges.put(CustomSwipeLayout.DragEdge.Left, child);
        }
        if((gravity & Gravity.RIGHT) == Gravity.RIGHT){
            mDragEdges.put(CustomSwipeLayout.DragEdge.Right, child);
        }
        if((gravity & Gravity.TOP) == Gravity.TOP){
            mDragEdges.put(CustomSwipeLayout.DragEdge.Top, child);
        }
        if((gravity & Gravity.BOTTOM) == Gravity.BOTTOM){
            mDragEdges.put(CustomSwipeLayout.DragEdge.Bottom, child);
        }
    }else{
        for(Map.Entry<CustomSwipeLayout.DragEdge, View> entry : mDragEdges.entrySet()){
            if(entry.getValue() == null){
                //means used the drag_edge attr, the no gravity child should be use set
                mDragEdges.put(entry.getKey(), child);
                break;
            }
        }
    }
    if(child==null || child.getParent() == this){
        return;
    }
    super.addView(child, index, params);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    updateBottomViews();

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

void layoutPullOut() {
    Rect rect = computeSurfaceLayoutArea(false);
    View surfaceView = getSurfaceView();
    if(surfaceView!=null){
        surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom);
        bringChildToFront(surfaceView);
    }
    rect = computeBottomLayoutAreaViaSurface(CustomSwipeLayout.ShowMode.PullOut, rect);
    View currentBottomView = getCurrentBottomView();
    if(currentBottomView!=null){
        currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
    }
}

void layoutLayDown() {
    Rect rect = computeSurfaceLayoutArea(false);
    View surfaceView = getSurfaceView();
    if(surfaceView!=null){
        surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom);
        bringChildToFront(surfaceView);
    }
    rect = computeBottomLayoutAreaViaSurface(CustomSwipeLayout.ShowMode.LayDown, rect);
    View currentBottomView = getCurrentBottomView();
    if(currentBottomView!=null){
        currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
    }
}

private boolean mIsBeingDragged;
private void checkCanDrag(MotionEvent ev){
    if(mIsBeingDragged) return;
    if(getOpenStatus()== CustomSwipeLayout.Status.Middle){
        mIsBeingDragged = true;
        return;
    }
    CustomSwipeLayout.Status status = getOpenStatus();
    float distanceX = ev.getRawX() - sX;
    float distanceY = ev.getRawY() - sY;
    float angle = Math.abs(distanceY / distanceX);
    angle = (float) Math.toDegrees(Math.atan(angle));
    if (getOpenStatus() == CustomSwipeLayout.Status.Close) {
        CustomSwipeLayout.DragEdge dragEdge;
        if (angle < 45) {
            if (distanceX > 0 && isLeftSwipeEnabled()) {
                dragEdge = CustomSwipeLayout.DragEdge.Left;
            } else if (distanceX < 0 && isRightSwipeEnabled()) {
                dragEdge = CustomSwipeLayout.DragEdge.Right;
            }else return;

        } else {
            if (distanceY > 0 && isTopSwipeEnabled()) {
                dragEdge = CustomSwipeLayout.DragEdge.Top;
            } else if (distanceY < 0 && isBottomSwipeEnabled()) {
                dragEdge = CustomSwipeLayout.DragEdge.Bottom;
            }else return;
        }
        setCurrentDragEdge(dragEdge);
    }

    boolean doNothing = false;
    if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right) {
        boolean suitable = (status == CustomSwipeLayout.Status.Open && distanceX > mTouchSlop)
                || (status == CustomSwipeLayout.Status.Close && distanceX < -mTouchSlop);
        suitable = suitable || (status == CustomSwipeLayout.Status.Middle);

        if (angle > 30 || !suitable) {
            doNothing = true;
        }
    }

    if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left) {
        boolean suitable = (status == CustomSwipeLayout.Status.Open && distanceX < -mTouchSlop)
                || (status == CustomSwipeLayout.Status.Close && distanceX > mTouchSlop);
        suitable = suitable || status == CustomSwipeLayout.Status.Middle;

        if (angle > 30 || !suitable) {
            doNothing = true;
        }
    }

    if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Top) {
        boolean suitable = (status == CustomSwipeLayout.Status.Open && distanceY < -mTouchSlop)
                || (status == CustomSwipeLayout.Status.Close && distanceY > mTouchSlop);
        suitable = suitable || status == CustomSwipeLayout.Status.Middle;

        if (angle < 60 || !suitable) {
            doNothing = true;
        }
    }

    if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Bottom) {
        boolean suitable = (status == CustomSwipeLayout.Status.Open && distanceY > mTouchSlop)
                || (status == CustomSwipeLayout.Status.Close && distanceY < -mTouchSlop);
        suitable = suitable || status == CustomSwipeLayout.Status.Middle;

        if (angle < 60 || !suitable) {
            doNothing = true;
        }
    }
    mIsBeingDragged = !doNothing;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isSwipeEnabled()) {
        return false;
    }
    if(mClickToClose && getOpenStatus() == CustomSwipeLayout.Status.Open && isTouchOnSurface(ev)){
        return true;
    }
    for (CustomSwipeLayout.SwipeDenier denier : mSwipeDeniers) {
        if (denier != null && denier.shouldDenySwipe(ev)) {
            return false;
        }
    }

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDragHelper.processTouchEvent(ev);
            mIsBeingDragged = false;
            sX = ev.getRawX();
            sY = ev.getRawY();
            //if the swipe is in middle state(scrolling), should intercept the touch
            if(getOpenStatus() == CustomSwipeLayout.Status.Middle){
                mIsBeingDragged = true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            boolean beforeCheck = mIsBeingDragged;
            checkCanDrag(ev);
            if (mIsBeingDragged) {
                ViewParent parent = getParent();
                if(parent!=null){
                    parent.requestDisallowInterceptTouchEvent(true);
                }
            }
            if(!beforeCheck && mIsBeingDragged){
                //let children has one chance to catch the touch, and request the swipe not intercept
                //useful when CustomSwipeLayout wrap a CustomSwipeLayout or other gestural layout
                return false;
            }
            break;

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mIsBeingDragged = false;
            mDragHelper.processTouchEvent(ev);
            break;
        default://handle other action, such as ACTION_POINTER_DOWN/UP
            mDragHelper.processTouchEvent(ev);
    }
    return mIsBeingDragged;
}

private float sX = -1, sY = -1;

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isSwipeEnabled()) return super.onTouchEvent(event);

    int action = event.getActionMasked();
    gestureDetector.onTouchEvent(event);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mDragHelper.processTouchEvent(event);
            sX = event.getRawX();
            sY = event.getRawY();

        case MotionEvent.ACTION_MOVE: {
            //the drag state and the direction are already judged at onInterceptTouchEvent
            checkCanDrag(event);
            if(mIsBeingDragged){
                getParent().requestDisallowInterceptTouchEvent(true);
                mDragHelper.processTouchEvent(event);
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mDragHelper.processTouchEvent(event);
            break;

        default://handle other action, such as ACTION_POINTER_DOWN/UP
            mDragHelper.processTouchEvent(event);
    }

    return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
}
public boolean isClickToClose() {
    return mClickToClose;
}

public void setClickToClose(boolean mClickToClose) {
    this.mClickToClose = mClickToClose;
}

public void setSwipeEnabled(boolean enabled) {
    mSwipeEnabled = enabled;
}

public boolean isSwipeEnabled() {
    return mSwipeEnabled;
}

public boolean isLeftSwipeEnabled() {
    View bottomView = mDragEdges.get(CustomSwipeLayout.DragEdge.Left);
    return bottomView != null && bottomView.getParent() == this
            && bottomView != getSurfaceView() && mSwipesEnabled[CustomSwipeLayout.DragEdge.Left.ordinal()];
}

public void setLeftSwipeEnabled(boolean leftSwipeEnabled) {
    this.mSwipesEnabled[CustomSwipeLayout.DragEdge.Left.ordinal()] = leftSwipeEnabled;
}

public boolean isRightSwipeEnabled() {
    View bottomView = mDragEdges.get(CustomSwipeLayout.DragEdge.Right);
    return bottomView!=null && bottomView.getParent()==this
            && bottomView != getSurfaceView() && mSwipesEnabled[CustomSwipeLayout.DragEdge.Right.ordinal()];
}

public void setRightSwipeEnabled(boolean rightSwipeEnabled) {
    this.mSwipesEnabled[CustomSwipeLayout.DragEdge.Right.ordinal()] = rightSwipeEnabled;
}

public boolean isTopSwipeEnabled() {
    View bottomView = mDragEdges.get(CustomSwipeLayout.DragEdge.Top);
    return bottomView!=null && bottomView.getParent()==this
            && bottomView != getSurfaceView() && mSwipesEnabled[CustomSwipeLayout.DragEdge.Top.ordinal()];
}

public void setTopSwipeEnabled(boolean topSwipeEnabled) {
    this.mSwipesEnabled[CustomSwipeLayout.DragEdge.Top.ordinal()] = topSwipeEnabled;
}

public boolean isBottomSwipeEnabled() {
    View bottomView = mDragEdges.get(CustomSwipeLayout.DragEdge.Bottom);
    return bottomView!=null && bottomView.getParent()==this
            && bottomView != getSurfaceView() && mSwipesEnabled[CustomSwipeLayout.DragEdge.Bottom.ordinal()];
}

public void setBottomSwipeEnabled(boolean bottomSwipeEnabled) {
    this.mSwipesEnabled[CustomSwipeLayout.DragEdge.Bottom.ordinal()] = bottomSwipeEnabled;
}
private boolean insideAdapterView() {
    return getAdapterView() != null;
}

private AdapterView getAdapterView() {
    ViewParent t = getParent();
    if (t instanceof AdapterView) {
        return (AdapterView) t;
    }
    return null;
}

private void performAdapterViewItemClick() {
    if(getOpenStatus()!= CustomSwipeLayout.Status.Close) return;
    ViewParent t = getParent();
    if (t instanceof AdapterView) {
        AdapterView view = (AdapterView) t;
        int p = view.getPositionForView(CustomSwipeLayout.this);
        if (p != AdapterView.INVALID_POSITION){
            view.performItemClick(view.getChildAt(p - view.getFirstVisiblePosition()), p, view
                    .getAdapter().getItemId(p));
        }
    }
}
private boolean performAdapterViewItemLongClick() {
    if(getOpenStatus()!= CustomSwipeLayout.Status.Close) return false;
    ViewParent t = getParent();
    if (t instanceof AdapterView) {
        AdapterView view = (AdapterView) t;
        int p = view.getPositionForView(CustomSwipeLayout.this);
        if (p == AdapterView.INVALID_POSITION) return false;
        long vId = view.getItemIdAtPosition(p);
        boolean handled = false;
        try {
            Method m = AbsListView.class.getDeclaredMethod("performLongPress", View.class, int.class, long.class);
            m.setAccessible(true);
            handled = (boolean) m.invoke(view, CustomSwipeLayout.this, p, vId);

        } catch (Exception e) {
            e.printStackTrace();

            if (view.getOnItemLongClickListener() != null) {
                handled = view.getOnItemLongClickListener().onItemLongClick(view, CustomSwipeLayout.this, p, vId);
            }
            if (handled) {
                view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            }
        }
        return handled;
    }
    return false;
}
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    if(insideAdapterView()){
        if(clickListener==null){
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    performAdapterViewItemClick();
                }
            });
        }
        if(longClickListener==null){
            setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    performAdapterViewItemLongClick();
                    return true;
                }
            });
        }
    }
}
OnClickListener clickListener;
@Override
public void setOnClickListener(OnClickListener l) {
    super.setOnClickListener(l);
    clickListener = l;
}
OnLongClickListener longClickListener;
@Override
public void setOnLongClickListener(OnLongClickListener l) {
    super.setOnLongClickListener(l);
    longClickListener = l;
}

private Rect hitSurfaceRect;
private boolean isTouchOnSurface(MotionEvent ev){
    View surfaceView = getSurfaceView();
    if(surfaceView==null){
        return false;
    }
    if(hitSurfaceRect == null){
        hitSurfaceRect = new Rect();
    }
    surfaceView.getHitRect(hitSurfaceRect);
    return hitSurfaceRect.contains((int) ev.getX(), (int) ev.getY());
}
private GestureDetector gestureDetector = new GestureDetector(getContext(), new CustomSwipeLayout.SwipeDetector());

class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        if(mClickToClose && isTouchOnSurface(e)){
            close();
        }
        return super.onSingleTapUp(e);
    }
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if (mDoubleClickListener != null) {
            View target;
            View bottom = getCurrentBottomView();
            View surface = getSurfaceView();
            if (bottom!=null && e.getX() > bottom.getLeft() && e.getX() < bottom.getRight()
                    && e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()) {
                target = bottom;
            } else {
                target = surface;
            }
            mDoubleClickListener.onDoubleClick(CustomSwipeLayout.this, target == surface);
        }
        return true;
    }
}

/**
 * set the drag distance, it will force set the bottom view's width or
 * height via this value.
 *
 * @param max max distance in dp unit
 */
public void setDragDistance(int max) {
    if (max < 0) max = 0;
    mDragDistance = dp2px(max);
    requestLayout();
}

/**
 * There are 2 diffirent show mode.
 * {@link CustomSwipeLayout.ShowMode}.PullOut and
 * {@link CustomSwipeLayout.ShowMode}.LayDown.
 *
 * @param mode
 */
public void setShowMode(CustomSwipeLayout.ShowMode mode) {
    mShowMode = mode;
    requestLayout();
}

public CustomSwipeLayout.DragEdge getDragEdge() {
    return mCurrentDragEdge;
}

public int getDragDistance() {
    return mDragDistance;
}

public CustomSwipeLayout.ShowMode getShowMode() {
    return mShowMode;
}

/**return null if there is no surface view(no children) */
public View getSurfaceView() {
    if(getChildCount()==0) return null;
    return getChildAt(getChildCount() - 1);
}

/**return null if there is no bottom view */
@Nullable
public View getCurrentBottomView(){
    List<View> bottoms = getBottomViews();
    if(mCurrentDragEdge.ordinal() < bottoms.size()){
        return bottoms.get(mCurrentDragEdge.ordinal());
    }
    return null;
}
/**
 * @return all bottomViews: left, top, right, bottom (may null if the edge is not set)
 */
public List<View> getBottomViews() {
    ArrayList<View> bottoms = new ArrayList<View>();
    for(CustomSwipeLayout.DragEdge dragEdge : CustomSwipeLayout.DragEdge.values()){
        bottoms.add(mDragEdges.get(dragEdge));
    }
    return bottoms;
}

public enum Status {
    Middle,
    Open,
    Close
}

/**
 * get the open status.
 *
 * @return {@link CustomSwipeLayout.Status} Open , Close or
 * Middle.
 */
public CustomSwipeLayout.Status getOpenStatus() {
    View surfaceView = getSurfaceView();
    if(surfaceView==null){
        return CustomSwipeLayout.Status.Close;
    }
    int surfaceLeft = surfaceView.getLeft();
    int surfaceTop = surfaceView.getTop();
    if (surfaceLeft == getPaddingLeft() && surfaceTop == getPaddingTop()) return CustomSwipeLayout.Status.Close;

    if (surfaceLeft == (getPaddingLeft() - mDragDistance) || surfaceLeft == (getPaddingLeft() + mDragDistance)
            || surfaceTop == (getPaddingTop() - mDragDistance) || surfaceTop == (getPaddingTop() + mDragDistance))
        return CustomSwipeLayout.Status.Open;

    return CustomSwipeLayout.Status.Middle;
}

/**
 * Process the surface release event.
 *
 * @param xvel xVelocity
 * @param yvel yVelocity
 * @param isCloseBeforeDragged the open state before drag
 */
protected void processHandRelease(float xvel, float yvel, boolean isCloseBeforeDragged) {
    float minVelocity = mDragHelper.getMinVelocity();
    View surfaceView = getSurfaceView();
    CustomSwipeLayout.DragEdge currentDragEdge = mCurrentDragEdge;
    if(currentDragEdge == null || surfaceView == null){
        return;
    }
    float willOpenPercent = (isCloseBeforeDragged ? .25f : .75f);
    if(currentDragEdge == CustomSwipeLayout.DragEdge.Left){
        if(xvel > minVelocity) open();
        else if(xvel < -minVelocity) close();
        else{
            float openPercent = 1f * getSurfaceView().getLeft() / mDragDistance;
            if(openPercent > willOpenPercent ) open();
            else close();
        }
    }else if(currentDragEdge == CustomSwipeLayout.DragEdge.Right){
        if(xvel > minVelocity) close();
        else if(xvel < -minVelocity) open();
        else{
            float openPercent = 1f * (-getSurfaceView().getLeft()) / mDragDistance;
            if(openPercent > willOpenPercent ) open();
            else close();
        }
    }else if(currentDragEdge == CustomSwipeLayout.DragEdge.Top){
        if(yvel > minVelocity) open();
        else if(yvel < -minVelocity) close();
        else{
            float openPercent = 1f * getSurfaceView().getTop() / mDragDistance;
            if(openPercent > willOpenPercent ) open();
            else close();
        }
    }else if(currentDragEdge == CustomSwipeLayout.DragEdge.Bottom){
        if(yvel > minVelocity) close();
        else if(yvel < -minVelocity) open();
        else{
            float openPercent = 1f * (-getSurfaceView().getTop()) / mDragDistance;
            if(openPercent > willOpenPercent ) open();
            else close();
        }
    }
}

/**
 * smoothly open surface.
 */
public void open() {
    open(true, true);
}

public void open(boolean smooth) {
    open(smooth, true);
}

public void open(boolean smooth, boolean notify) {
    View surface = getSurfaceView(), bottom = getCurrentBottomView();
    if(surface == null){
        return;
    }
    int dx, dy;
    Rect rect = computeSurfaceLayoutArea(true);
    if (smooth) {
        mDragHelper.smoothSlideViewTo(surface, rect.left, rect.top);
    } else {
        dx = rect.left - surface.getLeft();
        dy = rect.top - surface.getTop();
        surface.layout(rect.left, rect.top, rect.right, rect.bottom);
        if (getShowMode() == CustomSwipeLayout.ShowMode.PullOut) {
            Rect bRect = computeBottomLayoutAreaViaSurface(CustomSwipeLayout.ShowMode.PullOut, rect);
            if(bottom!=null){
                bottom.layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
            }
        }
        if (notify) {
            dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
            dispatchSwipeEvent(rect.left, rect.top, dx, dy);
        } else {
            safeBottomView();
        }
    }
    invalidate();
}

public void open(CustomSwipeLayout.DragEdge edge) {
    setCurrentDragEdge(edge);
    open(true, true);
}

public void open(boolean smooth, CustomSwipeLayout.DragEdge edge) {
    setCurrentDragEdge(edge);
    open(smooth, true);
}

public void open(boolean smooth, boolean notify, CustomSwipeLayout.DragEdge edge) {
    setCurrentDragEdge(edge);
    open(smooth, notify);
}

/**
 * smoothly close surface.
 */
public void close() {
    close(true, true);
}

public void close(boolean smooth) {
    close(smooth, true);
}

/**
 * close surface
 *
 * @param smooth smoothly or not.
 * @param notify if notify all the listeners.
 */
public void close(boolean smooth, boolean notify) {
    View surface = getSurfaceView();
    if(surface==null){
        return;
    }
    int dx, dy;
    if (smooth)
        mDragHelper.smoothSlideViewTo(getSurfaceView(), getPaddingLeft(), getPaddingTop());
    else {
        Rect rect = computeSurfaceLayoutArea(false);
        dx = rect.left - surface.getLeft();
        dy = rect.top - surface.getTop();
        surface.layout(rect.left, rect.top, rect.right, rect.bottom);
        if (notify) {
            dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
            dispatchSwipeEvent(rect.left, rect.top, dx, dy);
        } else {
            safeBottomView();
        }
    }
    invalidate();
}

public void toggle() {
    toggle(true);
}

public void toggle(boolean smooth) {
    if (getOpenStatus() == CustomSwipeLayout.Status.Open)
        close(smooth);
    else if (getOpenStatus() == CustomSwipeLayout.Status.Close) open(smooth);
}

/**
 * a helper function to compute the Rect area that surface will hold in.
 * @param open open status or close status.
 */
private Rect computeSurfaceLayoutArea(boolean open) {
    int l = getPaddingLeft(), t = getPaddingTop();
    if (open) {
        if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left)
            l = getPaddingLeft() + mDragDistance;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right)
            l = getPaddingLeft() - mDragDistance;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Top)
            t = getPaddingTop() + mDragDistance;
        else t = getPaddingTop() - mDragDistance;
    }
    return new Rect(l, t, l + getMeasuredWidth(), t + getMeasuredHeight());
}

private Rect computeBottomLayoutAreaViaSurface(CustomSwipeLayout.ShowMode mode, Rect surfaceArea) {
    Rect rect = surfaceArea;
    View bottomView = getCurrentBottomView();

    int bl = rect.left, bt = rect.top, br = rect.right, bb = rect.bottom;
    if (mode == CustomSwipeLayout.ShowMode.PullOut) {
        if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left)
            bl = rect.left - mDragDistance;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right)
            bl = rect.right;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Top)
            bt = rect.top - mDragDistance;
        else bt = rect.bottom;

        if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left || mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right) {
            bb = rect.bottom;
            br = bl + (bottomView == null? 0 :bottomView.getMeasuredWidth());
        } else {
            bb = bt + (bottomView == null? 0 :bottomView.getMeasuredHeight());
            br = rect.right;
        }
    } else if (mode == CustomSwipeLayout.ShowMode.LayDown) {
        if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left)
            br = bl + mDragDistance;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right)
            bl = br - mDragDistance;
        else if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Top)
            bb = bt + mDragDistance;
        else bt = bb - mDragDistance;

    }
    return new Rect(bl, bt, br, bb);

}

private Rect computeBottomLayDown(CustomSwipeLayout.DragEdge dragEdge) {
    int bl = getPaddingLeft(), bt = getPaddingTop();
    int br, bb;
    if (dragEdge == CustomSwipeLayout.DragEdge.Right) {
        bl = getMeasuredWidth() - mDragDistance;
    } else if (dragEdge == CustomSwipeLayout.DragEdge.Bottom) {
        bt = getMeasuredHeight() - mDragDistance;
    }
    if (dragEdge == CustomSwipeLayout.DragEdge.Left || dragEdge == CustomSwipeLayout.DragEdge.Right) {
        br = bl + mDragDistance;
        bb = bt + getMeasuredHeight();
    } else {
        br = bl + getMeasuredWidth();
        bb = bt + mDragDistance;
    }
    return new Rect(bl, bt, br, bb);
}

public void setOnDoubleClickListener(CustomSwipeLayout.DoubleClickListener doubleClickListener) {
    mDoubleClickListener = doubleClickListener;
}

public interface DoubleClickListener {
    public void onDoubleClick(CustomSwipeLayout layout, boolean surface);
}

private int dp2px(float dp) {
    return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
}

/**Deprecated, use {@link #addDrag(CustomSwipeLayout.DragEdge, View)} */
@Deprecated
public void setDragEdge(CustomSwipeLayout.DragEdge dragEdge) {
    if(getChildCount() >= 2){
        mDragEdges.put(dragEdge, getChildAt(getChildCount()-2));
    }
    setCurrentDragEdge(dragEdge);
}

public void onViewRemoved(View child) {
    for(Map.Entry<CustomSwipeLayout.DragEdge, View> entry : new HashMap<CustomSwipeLayout.DragEdge, View>(mDragEdges).entrySet()){
        if(entry.getValue() == child){
            mDragEdges.remove(entry.getKey());
        }
    }
}
public Map<CustomSwipeLayout.DragEdge, View> getDragEdgeMap(){
    return mDragEdges;
}

/**Deprecated, use {@link #getDragEdgeMap()} */
@Deprecated
public List<CustomSwipeLayout.DragEdge> getDragEdges() {
    return new ArrayList<CustomSwipeLayout.DragEdge>(mDragEdges.keySet());
}

/**Deprecated, use {@link #addDrag(CustomSwipeLayout.DragEdge, View)} */
@Deprecated
public void setDragEdges(List<CustomSwipeLayout.DragEdge> dragEdges) {
    for (int i = 0, size = Math.min(dragEdges.size(), getChildCount() - 1); i < size; i++) {
        CustomSwipeLayout.DragEdge dragEdge = dragEdges.get(i);
        mDragEdges.put(dragEdge, getChildAt(i));
    }
    if(dragEdges.size()==0 || dragEdges.contains(DefaultDragEdge)){
        setCurrentDragEdge(DefaultDragEdge);
    }else{
        setCurrentDragEdge(dragEdges.get(0));
    }
}

/**Deprecated, use {@link #addDrag(CustomSwipeLayout.DragEdge, View)} */
@Deprecated
public void setDragEdges(CustomSwipeLayout.DragEdge... mDragEdges) {
    setDragEdges(Arrays.asList(mDragEdges));
}
/**
 * Deprecated, use {@link #addDrag(CustomSwipeLayout.DragEdge, View)}
 * When using multiple drag edges it's a good idea to pass the ids of the views that
 * you're using for the left, right, top bottom views (-1 if you're not using a particular view)
 */
@Deprecated
public void setBottomViewIds(int leftId, int rightId, int topId, int bottomId) {
    addDrag(CustomSwipeLayout.DragEdge.Left, findViewById(leftId));
    addDrag(CustomSwipeLayout.DragEdge.Right, findViewById(rightId));
    addDrag(CustomSwipeLayout.DragEdge.Top, findViewById(topId));
    addDrag(CustomSwipeLayout.DragEdge.Bottom, findViewById(bottomId));
}

private float getCurrentOffset() {
    if(mCurrentDragEdge==null) return 0;
    return mEdgeSwipesOffset[mCurrentDragEdge.ordinal()];
}

private void setCurrentDragEdge(CustomSwipeLayout.DragEdge dragEdge){
    if(mCurrentDragEdge != dragEdge){
        mCurrentDragEdge = dragEdge;
        updateBottomViews();
    }
}

private void updateBottomViews() {
    View currentBottomView = getCurrentBottomView();
    if(currentBottomView!=null){
        if (mCurrentDragEdge == CustomSwipeLayout.DragEdge.Left || mCurrentDragEdge == CustomSwipeLayout.DragEdge.Right) {
            mDragDistance = currentBottomView.getMeasuredWidth() - dp2px(getCurrentOffset());
        }
        else mDragDistance = currentBottomView.getMeasuredHeight() - dp2px(getCurrentOffset());
    }

    if (mShowMode == CustomSwipeLayout.ShowMode.PullOut)
        layoutPullOut();
    else if (mShowMode == CustomSwipeLayout.ShowMode.LayDown) layoutLayDown();

    safeBottomView();
}

}
dobrowins commented 7 years ago

@vaibhavjain93 May you please share on how to use it? Where to put removal of swipe listeners and when to initialize them again. This library is handy, but I can't use it if several items are removed. Thanks