From cc1d1b3b71d9d6d26c049a2ce4ed3e6c12f9ee6b Mon Sep 17 00:00:00 2001 From: linfaxin Date: Wed, 22 Apr 2015 19:11:11 +0800 Subject: [PATCH] Optimize add edge and set drag logic. --- .../java/com/daimajia/swipe/SwipeLayout.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java index 270bc1c..ec1c035 100644 --- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java +++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java @@ -655,10 +655,26 @@ public class SwipeLayout extends FrameLayout { public void removeOnLayoutListener(OnLayout l) { if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l); } + public void clearDragEdge(){ + mDragEdges.clear(); + } + public void setDrag(DragEdge dragEdge, int childId){ + clearDragEdge(); + addDrag(dragEdge, childId); + } + public void setDrag(DragEdge dragEdge, View child){ + clearDragEdge(); + addDrag(dragEdge, child); + } + public void addDrag(DragEdge dragEdge, int childId){ + addDrag(dragEdge, findViewById(childId), null); + } public void addDrag(DragEdge dragEdge, View child){ addDrag(dragEdge, child, null); } public void addDrag(DragEdge dragEdge, View child, ViewGroup.LayoutParams params){ + if(child==null) return; + if(params==null){ params = generateDefaultLayoutParams(); } @@ -679,6 +695,7 @@ public class SwipeLayout extends FrameLayout { } @Override public void addView(View child, int index, ViewGroup.LayoutParams params) { + if(child==null) return; int gravity = Gravity.NO_GRAVITY; try { gravity = (Integer) params.getClass().getField("gravity").get(params); @@ -710,7 +727,7 @@ public class SwipeLayout extends FrameLayout { } } } - if(child==null || child.getParent() == this){ + if(child.getParent() == this){ return; } super.addView(child, index, params); @@ -1429,9 +1446,10 @@ public class SwipeLayout extends FrameLayout { } - /**Deprecated, use {@link #addDrag(DragEdge, View)} */ + /**Deprecated, use {@link #setDrag(DragEdge, View)} */ @Deprecated public void setDragEdge(DragEdge dragEdge) { + clearDragEdge(); if(getChildCount() >= 2){ mDragEdges.put(dragEdge, getChildAt(getChildCount()-2)); } @@ -1455,9 +1473,10 @@ public class SwipeLayout extends FrameLayout { return new ArrayList(mDragEdges.keySet()); } - /**Deprecated, use {@link #addDrag(DragEdge, View)} */ + /**Deprecated, use {@link #setDrag(DragEdge, View)} */ @Deprecated public void setDragEdges(List dragEdges) { + clearDragEdge(); for (int i = 0, size = Math.min(dragEdges.size(), getChildCount() - 1); i < size; i++) { DragEdge dragEdge = dragEdges.get(i); mDragEdges.put(dragEdge, getChildAt(i)); @@ -1472,6 +1491,7 @@ public class SwipeLayout extends FrameLayout { /**Deprecated, use {@link #addDrag(DragEdge, View)} */ @Deprecated public void setDragEdges(DragEdge... mDragEdges) { + clearDragEdge(); setDragEdges(Arrays.asList(mDragEdges)); } /**