Merge pull request #207 from daimajia/fix_relayout

[fix] If requestLayout() call, will reset children bounds.
This commit is contained in:
林法鑫 2015-08-30 09:56:50 +08:00
commit f66ac6942b
1 changed files with 39 additions and 8 deletions

View File

@ -52,6 +52,7 @@ public class SwipeLayout extends FrameLayout {
private List<SwipeDenier> mSwipeDeniers = new ArrayList<>(); private List<SwipeDenier> mSwipeDeniers = new ArrayList<>();
private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<>(); private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<>();
private Map<View, Boolean> mShowEntirely = new HashMap<>(); private Map<View, Boolean> mShowEntirely = new HashMap<>();
private Map<View, Rect> mViewBoundCache = new HashMap<>();//save all children's bound, restore in onLayout
private DoubleClickListener mDoubleClickListener; private DoubleClickListener mDoubleClickListener;
@ -392,9 +393,35 @@ public class SwipeLayout extends FrameLayout {
dispatchSwipeEvent(evLeft, evTop, dx, dy); dispatchSwipeEvent(evLeft, evTop, dx, dy);
invalidate(); invalidate();
captureChildrenBound();
} }
}; };
/**
* save children's bounds, so they can restore the bound in {@link #onLayout(boolean, int, int, int, int)}
*/
private void captureChildrenBound(){
View currentBottomView = getCurrentBottomView();
if(getOpenStatus()==Status.Close){
mViewBoundCache.remove(currentBottomView);
return;
}
View[] views = new View[]{getSurfaceView(), currentBottomView};
for (View child : views) {
Rect rect = mViewBoundCache.get(child);
if(rect==null){
rect = new Rect();
mViewBoundCache.put(child, rect);
}
rect.left = child.getLeft();
rect.top = child.getTop();
rect.right = child.getRight();
rect.bottom = child.getBottom();
}
}
/** /**
* the dispatchRevealEvent method may not always get accurate position, it * the dispatchRevealEvent method may not always get accurate position, it
* makes the view may not always get the event when the view is totally * makes the view may not always get the event when the view is totally
@ -763,30 +790,34 @@ public class SwipeLayout extends FrameLayout {
} }
void layoutPullOut() { void layoutPullOut() {
Rect rect = computeSurfaceLayoutArea(false);
View surfaceView = getSurfaceView(); View surfaceView = getSurfaceView();
Rect surfaceRect = mViewBoundCache.get(surfaceView);
if(surfaceRect == null) surfaceRect = computeSurfaceLayoutArea(false);
if (surfaceView != null) { if (surfaceView != null) {
surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom); surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
bringChildToFront(surfaceView); bringChildToFront(surfaceView);
} }
rect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect);
View currentBottomView = getCurrentBottomView(); View currentBottomView = getCurrentBottomView();
Rect bottomViewRect = mViewBoundCache.get(currentBottomView);
if(bottomViewRect == null) bottomViewRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, surfaceRect);
if (currentBottomView != null) { if (currentBottomView != null) {
currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom); currentBottomView.layout(bottomViewRect.left, bottomViewRect.top, bottomViewRect.right, bottomViewRect.bottom);
} }
} }
void layoutLayDown() { void layoutLayDown() {
Rect rect = computeSurfaceLayoutArea(false);
View surfaceView = getSurfaceView(); View surfaceView = getSurfaceView();
Rect surfaceRect = mViewBoundCache.get(surfaceView);
if(surfaceRect == null) surfaceRect = computeSurfaceLayoutArea(false);
if (surfaceView != null) { if (surfaceView != null) {
surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom); surfaceView.layout(surfaceRect.left, surfaceRect.top, surfaceRect.right, surfaceRect.bottom);
bringChildToFront(surfaceView); bringChildToFront(surfaceView);
} }
rect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, rect);
View currentBottomView = getCurrentBottomView(); View currentBottomView = getCurrentBottomView();
Rect bottomViewRect = mViewBoundCache.get(currentBottomView);
if(bottomViewRect == null) bottomViewRect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, surfaceRect);
if (currentBottomView != null) { if (currentBottomView != null) {
currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom); currentBottomView.layout(bottomViewRect.left, bottomViewRect.top, bottomViewRect.right, bottomViewRect.bottom);
} }
} }