prettify the code and do clean up.
This commit is contained in:
parent
a10e28ded7
commit
6f6498e549
|
|
@ -43,15 +43,15 @@ public class SwipeLayout extends FrameLayout {
|
|||
private ViewDragHelper mDragHelper;
|
||||
|
||||
private int mDragDistance = 0;
|
||||
private LinkedHashMap<DragEdge, View> mDragEdges = new LinkedHashMap<DragEdge, View>();
|
||||
private LinkedHashMap<DragEdge, View> mDragEdges = new LinkedHashMap<>();
|
||||
private ShowMode mShowMode;
|
||||
|
||||
private float[] mEdgeSwipesOffset = new float[4];
|
||||
|
||||
private List<SwipeListener> mSwipeListeners = new ArrayList<SwipeListener>();
|
||||
private List<SwipeDenier> mSwipeDeniers = new ArrayList<SwipeDenier>();
|
||||
private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<View, ArrayList<OnRevealListener>>();
|
||||
private Map<View, Boolean> mShowEntirely = new HashMap<View, Boolean>();
|
||||
private List<SwipeListener> mSwipeListeners = new ArrayList<>();
|
||||
private List<SwipeDenier> mSwipeDeniers = new ArrayList<>();
|
||||
private Map<View, ArrayList<OnRevealListener>> mRevealListeners = new HashMap<>();
|
||||
private Map<View, Boolean> mShowEntirely = new HashMap<>();
|
||||
|
||||
private DoubleClickListener mDoubleClickListener;
|
||||
|
||||
|
|
@ -59,14 +59,14 @@ public class SwipeLayout extends FrameLayout {
|
|||
private boolean[] mSwipesEnabled = new boolean[]{true, true, true, true};
|
||||
private boolean mClickToClose = false;
|
||||
|
||||
public static enum DragEdge {
|
||||
public enum DragEdge {
|
||||
Left,
|
||||
Top,
|
||||
Right,
|
||||
Bottom
|
||||
}
|
||||
|
||||
public static enum ShowMode {
|
||||
public enum ShowMode {
|
||||
LayDown,
|
||||
PullOut
|
||||
}
|
||||
|
|
@ -111,17 +111,17 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
public interface SwipeListener {
|
||||
public void onStartOpen(SwipeLayout layout);
|
||||
void onStartOpen(SwipeLayout layout);
|
||||
|
||||
public void onOpen(SwipeLayout layout);
|
||||
void onOpen(SwipeLayout layout);
|
||||
|
||||
public void onStartClose(SwipeLayout layout);
|
||||
void onStartClose(SwipeLayout layout);
|
||||
|
||||
public void onClose(SwipeLayout layout);
|
||||
void onClose(SwipeLayout layout);
|
||||
|
||||
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset);
|
||||
void onUpdate(SwipeLayout layout, int leftOffset, int topOffset);
|
||||
|
||||
public void onHandRelease(SwipeLayout layout, float xvel, float yvel);
|
||||
void onHandRelease(SwipeLayout layout, float xvel, float yvel);
|
||||
}
|
||||
|
||||
public void addSwipeListener(SwipeListener l) {
|
||||
|
|
@ -136,7 +136,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
mSwipeListeners.clear();
|
||||
}
|
||||
|
||||
public static interface SwipeDenier {
|
||||
public interface SwipeDenier {
|
||||
/*
|
||||
* Called in onInterceptTouchEvent Determines if this swipe event should
|
||||
* be denied Implement this interface if you are using views with swipe
|
||||
|
|
@ -144,7 +144,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
*
|
||||
* @return true deny false allow
|
||||
*/
|
||||
public boolean shouldDenySwipe(MotionEvent ev);
|
||||
boolean shouldDenySwipe(MotionEvent ev);
|
||||
}
|
||||
|
||||
public void addSwipeDenier(SwipeDenier denier) {
|
||||
|
|
@ -160,7 +160,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
public interface OnRevealListener {
|
||||
public void onReveal(View child, DragEdge edge, float fraction, int distance);
|
||||
void onReveal(View child, DragEdge edge, float fraction, int distance);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -280,7 +280,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
} else {
|
||||
View surfaceView = getSurfaceView();
|
||||
int surfaceViewTop = surfaceView==null?0:surfaceView.getTop();
|
||||
int surfaceViewTop = surfaceView == null ? 0 : surfaceView.getTop();
|
||||
switch (mCurrentDragEdge) {
|
||||
case Left:
|
||||
case Right:
|
||||
|
|
@ -313,7 +313,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
@Override
|
||||
public boolean tryCaptureView(View child, int pointerId) {
|
||||
boolean result = child == getSurfaceView() || getBottomViews().contains(child);
|
||||
if(result){
|
||||
if (result) {
|
||||
isCloseBeforeDrag = getOpenStatus() == Status.Close;
|
||||
}
|
||||
return result;
|
||||
|
|
@ -330,11 +330,12 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
boolean isCloseBeforeDrag = true;
|
||||
|
||||
@Override
|
||||
public void onViewReleased(View releasedChild, float xvel, float yvel) {
|
||||
super.onViewReleased(releasedChild, xvel, yvel);
|
||||
processHandRelease(xvel, yvel, isCloseBeforeDrag);
|
||||
for (SwipeListener l : mSwipeListeners){
|
||||
for (SwipeListener l : mSwipeListeners) {
|
||||
l.onHandRelease(SwipeLayout.this, xvel, yvel);
|
||||
}
|
||||
|
||||
|
|
@ -344,16 +345,16 @@ public class SwipeLayout extends FrameLayout {
|
|||
@Override
|
||||
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
|
||||
View surfaceView = getSurfaceView();
|
||||
if(surfaceView==null) return;
|
||||
if (surfaceView == null) return;
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
int evLeft = surfaceView.getLeft(),
|
||||
evRight = surfaceView.getRight(),
|
||||
evTop = surfaceView.getTop(),
|
||||
int evLeft = surfaceView.getLeft(),
|
||||
evRight = surfaceView.getRight(),
|
||||
evTop = surfaceView.getTop(),
|
||||
evBottom = surfaceView.getBottom();
|
||||
if (changedView == surfaceView) {
|
||||
|
||||
if (mShowMode == ShowMode.PullOut && currentBottomView!=null) {
|
||||
if (mCurrentDragEdge == DragEdge.Left || mCurrentDragEdge == DragEdge.Right){
|
||||
if (mShowMode == ShowMode.PullOut && currentBottomView != null) {
|
||||
if (mCurrentDragEdge == DragEdge.Left || mCurrentDragEdge == DragEdge.Right) {
|
||||
currentBottomView.offsetLeftAndRight(dx);
|
||||
} else {
|
||||
currentBottomView.offsetTopAndBottom(dy);
|
||||
|
|
@ -367,7 +368,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
surfaceView.offsetTopAndBottom(dy);
|
||||
} else {
|
||||
Rect rect = computeBottomLayDown(mCurrentDragEdge);
|
||||
if(currentBottomView!=null){
|
||||
if (currentBottomView != null) {
|
||||
currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
|
||||
}
|
||||
|
||||
|
|
@ -527,7 +528,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
if (status == Status.Open) {
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
if(currentBottomView!=null){
|
||||
if (currentBottomView != null) {
|
||||
currentBottomView.setEnabled(true);
|
||||
}
|
||||
for (SwipeListener l : mSwipeListeners) {
|
||||
|
|
@ -547,13 +548,13 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
if (status == Status.Close) {
|
||||
for (View bottom : bottoms) {
|
||||
if (bottom!=null && bottom.getVisibility() != INVISIBLE){
|
||||
if (bottom != null && bottom.getVisibility() != INVISIBLE) {
|
||||
bottom.setVisibility(INVISIBLE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
if (currentBottomView!=null && currentBottomView.getVisibility() != VISIBLE){
|
||||
if (currentBottomView != null && currentBottomView.getVisibility() != VISIBLE) {
|
||||
currentBottomView.setVisibility(VISIBLE);
|
||||
}
|
||||
}
|
||||
|
|
@ -646,7 +647,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
* to support it from API 8.
|
||||
*/
|
||||
public interface OnLayout {
|
||||
public void onLayout(SwipeLayout v);
|
||||
void onLayout(SwipeLayout v);
|
||||
}
|
||||
|
||||
private List<OnLayout> mOnLayoutListeners;
|
||||
|
|
@ -659,47 +660,62 @@ public class SwipeLayout extends FrameLayout {
|
|||
public void removeOnLayoutListener(OnLayout l) {
|
||||
if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l);
|
||||
}
|
||||
public void clearDragEdge(){
|
||||
|
||||
public void clearDragEdge() {
|
||||
mDragEdges.clear();
|
||||
}
|
||||
public void setDrag(DragEdge dragEdge, int childId){
|
||||
|
||||
public void setDrag(DragEdge dragEdge, int childId) {
|
||||
clearDragEdge();
|
||||
addDrag(dragEdge, childId);
|
||||
}
|
||||
public void setDrag(DragEdge dragEdge, View child){
|
||||
|
||||
public void setDrag(DragEdge dragEdge, View child) {
|
||||
clearDragEdge();
|
||||
addDrag(dragEdge, child);
|
||||
}
|
||||
public void addDrag(DragEdge dragEdge, int childId){
|
||||
|
||||
public void addDrag(DragEdge dragEdge, int childId) {
|
||||
addDrag(dragEdge, findViewById(childId), null);
|
||||
}
|
||||
public void addDrag(DragEdge dragEdge, View child){
|
||||
|
||||
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){
|
||||
public void addDrag(DragEdge dragEdge, View child, ViewGroup.LayoutParams params) {
|
||||
if (child == null) return;
|
||||
|
||||
if (params == null) {
|
||||
params = generateDefaultLayoutParams();
|
||||
}
|
||||
if(!checkLayoutParams(params)){
|
||||
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;
|
||||
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){
|
||||
if (params instanceof FrameLayout.LayoutParams) {
|
||||
((LayoutParams) params).gravity = gravity;
|
||||
}
|
||||
addView(child, 0, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addView(View child, int index, ViewGroup.LayoutParams params) {
|
||||
if(child==null) return;
|
||||
if (child == null) return;
|
||||
int gravity = Gravity.NO_GRAVITY;
|
||||
try {
|
||||
gravity = (Integer) params.getClass().getField("gravity").get(params);
|
||||
|
|
@ -707,31 +723,31 @@ public class SwipeLayout extends FrameLayout {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(gravity>0){
|
||||
if (gravity > 0) {
|
||||
gravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
|
||||
|
||||
if((gravity & Gravity.LEFT) == Gravity.LEFT){
|
||||
if ((gravity & Gravity.LEFT) == Gravity.LEFT) {
|
||||
mDragEdges.put(DragEdge.Left, child);
|
||||
}
|
||||
if((gravity & Gravity.RIGHT) == Gravity.RIGHT){
|
||||
if ((gravity & Gravity.RIGHT) == Gravity.RIGHT) {
|
||||
mDragEdges.put(DragEdge.Right, child);
|
||||
}
|
||||
if((gravity & Gravity.TOP) == Gravity.TOP){
|
||||
if ((gravity & Gravity.TOP) == Gravity.TOP) {
|
||||
mDragEdges.put(DragEdge.Top, child);
|
||||
}
|
||||
if((gravity & Gravity.BOTTOM) == Gravity.BOTTOM){
|
||||
if ((gravity & Gravity.BOTTOM) == Gravity.BOTTOM) {
|
||||
mDragEdges.put(DragEdge.Bottom, child);
|
||||
}
|
||||
}else{
|
||||
for(Map.Entry<DragEdge, View> entry : mDragEdges.entrySet()){
|
||||
if(entry.getValue() == null){
|
||||
} else {
|
||||
for (Map.Entry<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.getParent() == this){
|
||||
if (child.getParent() == this) {
|
||||
return;
|
||||
}
|
||||
super.addView(child, index, params);
|
||||
|
|
@ -749,13 +765,13 @@ public class SwipeLayout extends FrameLayout {
|
|||
void layoutPullOut() {
|
||||
Rect rect = computeSurfaceLayoutArea(false);
|
||||
View surfaceView = getSurfaceView();
|
||||
if(surfaceView!=null){
|
||||
if (surfaceView != null) {
|
||||
surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom);
|
||||
bringChildToFront(surfaceView);
|
||||
}
|
||||
rect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect);
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
if(currentBottomView!=null){
|
||||
if (currentBottomView != null) {
|
||||
currentBottomView.layout(rect.left, rect.top, rect.right, rect.bottom);
|
||||
}
|
||||
}
|
||||
|
|
@ -763,21 +779,22 @@ public class SwipeLayout extends FrameLayout {
|
|||
void layoutLayDown() {
|
||||
Rect rect = computeSurfaceLayoutArea(false);
|
||||
View surfaceView = getSurfaceView();
|
||||
if(surfaceView!=null){
|
||||
if (surfaceView != null) {
|
||||
surfaceView.layout(rect.left, rect.top, rect.right, rect.bottom);
|
||||
bringChildToFront(surfaceView);
|
||||
}
|
||||
rect = computeBottomLayoutAreaViaSurface(ShowMode.LayDown, rect);
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
if(currentBottomView!=null){
|
||||
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()==Status.Middle){
|
||||
|
||||
private void checkCanDrag(MotionEvent ev) {
|
||||
if (mIsBeingDragged) return;
|
||||
if (getOpenStatus() == Status.Middle) {
|
||||
mIsBeingDragged = true;
|
||||
return;
|
||||
}
|
||||
|
|
@ -793,14 +810,14 @@ public class SwipeLayout extends FrameLayout {
|
|||
dragEdge = DragEdge.Left;
|
||||
} else if (distanceX < 0 && isRightSwipeEnabled()) {
|
||||
dragEdge = DragEdge.Right;
|
||||
}else return;
|
||||
} else return;
|
||||
|
||||
} else {
|
||||
if (distanceY > 0 && isTopSwipeEnabled()) {
|
||||
dragEdge = DragEdge.Top;
|
||||
} else if (distanceY < 0 && isBottomSwipeEnabled()) {
|
||||
dragEdge = DragEdge.Bottom;
|
||||
}else return;
|
||||
} else return;
|
||||
}
|
||||
setCurrentDragEdge(dragEdge);
|
||||
}
|
||||
|
|
@ -847,12 +864,13 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
mIsBeingDragged = !doNothing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
if (!isSwipeEnabled()) {
|
||||
return false;
|
||||
}
|
||||
if(mClickToClose && getOpenStatus() == Status.Open && isTouchOnSurface(ev)){
|
||||
if (mClickToClose && getOpenStatus() == Status.Open && isTouchOnSurface(ev)) {
|
||||
return true;
|
||||
}
|
||||
for (SwipeDenier denier : mSwipeDeniers) {
|
||||
|
|
@ -868,7 +886,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
sX = ev.getRawX();
|
||||
sY = ev.getRawY();
|
||||
//if the swipe is in middle state(scrolling), should intercept the touch
|
||||
if(getOpenStatus() == Status.Middle){
|
||||
if (getOpenStatus() == Status.Middle) {
|
||||
mIsBeingDragged = true;
|
||||
}
|
||||
break;
|
||||
|
|
@ -877,11 +895,11 @@ public class SwipeLayout extends FrameLayout {
|
|||
checkCanDrag(ev);
|
||||
if (mIsBeingDragged) {
|
||||
ViewParent parent = getParent();
|
||||
if(parent!=null){
|
||||
if (parent != null) {
|
||||
parent.requestDisallowInterceptTouchEvent(true);
|
||||
}
|
||||
}
|
||||
if(!beforeCheck && mIsBeingDragged){
|
||||
if (!beforeCheck && mIsBeingDragged) {
|
||||
//let children has one chance to catch the touch, and request the swipe not intercept
|
||||
//useful when swipeLayout wrap a swipeLayout or other gestural layout
|
||||
return false;
|
||||
|
|
@ -918,7 +936,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
case MotionEvent.ACTION_MOVE: {
|
||||
//the drag state and the direction are already judged at onInterceptTouchEvent
|
||||
checkCanDrag(event);
|
||||
if(mIsBeingDragged){
|
||||
if (mIsBeingDragged) {
|
||||
getParent().requestDisallowInterceptTouchEvent(true);
|
||||
mDragHelper.processTouchEvent(event);
|
||||
}
|
||||
|
|
@ -936,6 +954,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
|
||||
}
|
||||
|
||||
public boolean isClickToClose() {
|
||||
return mClickToClose;
|
||||
}
|
||||
|
|
@ -964,7 +983,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
public boolean isRightSwipeEnabled() {
|
||||
View bottomView = mDragEdges.get(DragEdge.Right);
|
||||
return bottomView!=null && bottomView.getParent()==this
|
||||
return bottomView != null && bottomView.getParent() == this
|
||||
&& bottomView != getSurfaceView() && mSwipesEnabled[DragEdge.Right.ordinal()];
|
||||
}
|
||||
|
||||
|
|
@ -974,7 +993,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
public boolean isTopSwipeEnabled() {
|
||||
View bottomView = mDragEdges.get(DragEdge.Top);
|
||||
return bottomView!=null && bottomView.getParent()==this
|
||||
return bottomView != null && bottomView.getParent() == this
|
||||
&& bottomView != getSurfaceView() && mSwipesEnabled[DragEdge.Top.ordinal()];
|
||||
}
|
||||
|
||||
|
|
@ -984,13 +1003,14 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
public boolean isBottomSwipeEnabled() {
|
||||
View bottomView = mDragEdges.get(DragEdge.Bottom);
|
||||
return bottomView!=null && bottomView.getParent()==this
|
||||
return bottomView != null && bottomView.getParent() == this
|
||||
&& bottomView != getSurfaceView() && mSwipesEnabled[DragEdge.Bottom.ordinal()];
|
||||
}
|
||||
|
||||
public void setBottomSwipeEnabled(boolean bottomSwipeEnabled) {
|
||||
this.mSwipesEnabled[DragEdge.Bottom.ordinal()] = bottomSwipeEnabled;
|
||||
}
|
||||
|
||||
private boolean insideAdapterView() {
|
||||
return getAdapterView() != null;
|
||||
}
|
||||
|
|
@ -1004,19 +1024,20 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
private void performAdapterViewItemClick() {
|
||||
if(getOpenStatus()!= Status.Close) return;
|
||||
if (getOpenStatus() != Status.Close) return;
|
||||
ViewParent t = getParent();
|
||||
if (t instanceof AdapterView) {
|
||||
AdapterView view = (AdapterView) t;
|
||||
int p = view.getPositionForView(SwipeLayout.this);
|
||||
if (p != AdapterView.INVALID_POSITION){
|
||||
if (p != AdapterView.INVALID_POSITION) {
|
||||
view.performItemClick(view.getChildAt(p - view.getFirstVisiblePosition()), p, view
|
||||
.getAdapter().getItemId(p));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean performAdapterViewItemLongClick() {
|
||||
if(getOpenStatus()!= Status.Close) return false;
|
||||
if (getOpenStatus() != Status.Close) return false;
|
||||
ViewParent t = getParent();
|
||||
if (t instanceof AdapterView) {
|
||||
AdapterView view = (AdapterView) t;
|
||||
|
|
@ -1043,11 +1064,12 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
if(insideAdapterView()){
|
||||
if(clickListener==null){
|
||||
if (insideAdapterView()) {
|
||||
if (clickListener == null) {
|
||||
setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
|
@ -1055,7 +1077,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
});
|
||||
}
|
||||
if(longClickListener==null){
|
||||
if (longClickListener == null) {
|
||||
setOnLongClickListener(new OnLongClickListener() {
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
|
|
@ -1066,13 +1088,17 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
OnClickListener clickListener;
|
||||
|
||||
@Override
|
||||
public void setOnClickListener(OnClickListener l) {
|
||||
super.setOnClickListener(l);
|
||||
clickListener = l;
|
||||
}
|
||||
|
||||
OnLongClickListener longClickListener;
|
||||
|
||||
@Override
|
||||
public void setOnLongClickListener(OnLongClickListener l) {
|
||||
super.setOnLongClickListener(l);
|
||||
|
|
@ -1080,34 +1106,37 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
private Rect hitSurfaceRect;
|
||||
private boolean isTouchOnSurface(MotionEvent ev){
|
||||
|
||||
private boolean isTouchOnSurface(MotionEvent ev) {
|
||||
View surfaceView = getSurfaceView();
|
||||
if(surfaceView==null){
|
||||
if (surfaceView == null) {
|
||||
return false;
|
||||
}
|
||||
if(hitSurfaceRect == null){
|
||||
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 SwipeDetector());
|
||||
|
||||
class SwipeDetector extends GestureDetector.SimpleOnGestureListener {
|
||||
@Override
|
||||
public boolean onSingleTapUp(MotionEvent e) {
|
||||
if(mClickToClose && isTouchOnSurface(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()
|
||||
if (bottom != null && e.getX() > bottom.getLeft() && e.getX() < bottom.getRight()
|
||||
&& e.getY() > bottom.getTop() && e.getY() < bottom.getBottom()) {
|
||||
target = bottom;
|
||||
} else {
|
||||
|
|
@ -1155,27 +1184,32 @@ public class SwipeLayout extends FrameLayout {
|
|||
return mShowMode;
|
||||
}
|
||||
|
||||
/**return null if there is no surface view(no children) */
|
||||
/**
|
||||
* return null if there is no surface view(no children)
|
||||
*/
|
||||
public View getSurfaceView() {
|
||||
if(getChildCount()==0) return null;
|
||||
if (getChildCount() == 0) return null;
|
||||
return getChildAt(getChildCount() - 1);
|
||||
}
|
||||
|
||||
/**return null if there is no bottom view */
|
||||
/**
|
||||
* return null if there is no bottom view
|
||||
*/
|
||||
@Nullable
|
||||
public View getCurrentBottomView(){
|
||||
public View getCurrentBottomView() {
|
||||
List<View> bottoms = getBottomViews();
|
||||
if(mCurrentDragEdge.ordinal() < bottoms.size()){
|
||||
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(DragEdge dragEdge : DragEdge.values()){
|
||||
for (DragEdge dragEdge : DragEdge.values()) {
|
||||
bottoms.add(mDragEdges.get(dragEdge));
|
||||
}
|
||||
return bottoms;
|
||||
|
|
@ -1195,7 +1229,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
*/
|
||||
public Status getOpenStatus() {
|
||||
View surfaceView = getSurfaceView();
|
||||
if(surfaceView==null){
|
||||
if (surfaceView == null) {
|
||||
return Status.Close;
|
||||
}
|
||||
int surfaceLeft = surfaceView.getLeft();
|
||||
|
|
@ -1213,48 +1247,48 @@ public class SwipeLayout extends FrameLayout {
|
|||
/**
|
||||
* Process the surface release event.
|
||||
*
|
||||
* @param xvel xVelocity
|
||||
* @param yvel yVelocity
|
||||
* @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();
|
||||
DragEdge currentDragEdge = mCurrentDragEdge;
|
||||
if(currentDragEdge == null || surfaceView == null){
|
||||
if (currentDragEdge == null || surfaceView == null) {
|
||||
return;
|
||||
}
|
||||
float willOpenPercent = (isCloseBeforeDragged ? .25f : .75f);
|
||||
if(currentDragEdge == DragEdge.Left){
|
||||
if(xvel > minVelocity) open();
|
||||
else if(xvel < -minVelocity) close();
|
||||
else{
|
||||
if (currentDragEdge == DragEdge.Left) {
|
||||
if (xvel > minVelocity) open();
|
||||
else if (xvel < -minVelocity) close();
|
||||
else {
|
||||
float openPercent = 1f * getSurfaceView().getLeft() / mDragDistance;
|
||||
if(openPercent > willOpenPercent ) open();
|
||||
if (openPercent > willOpenPercent) open();
|
||||
else close();
|
||||
}
|
||||
}else if(currentDragEdge == DragEdge.Right){
|
||||
if(xvel > minVelocity) close();
|
||||
else if(xvel < -minVelocity) open();
|
||||
else{
|
||||
} else if (currentDragEdge == DragEdge.Right) {
|
||||
if (xvel > minVelocity) close();
|
||||
else if (xvel < -minVelocity) open();
|
||||
else {
|
||||
float openPercent = 1f * (-getSurfaceView().getLeft()) / mDragDistance;
|
||||
if(openPercent > willOpenPercent ) open();
|
||||
if (openPercent > willOpenPercent) open();
|
||||
else close();
|
||||
}
|
||||
}else if(currentDragEdge == DragEdge.Top){
|
||||
if(yvel > minVelocity) open();
|
||||
else if(yvel < -minVelocity) close();
|
||||
else{
|
||||
} else if (currentDragEdge == DragEdge.Top) {
|
||||
if (yvel > minVelocity) open();
|
||||
else if (yvel < -minVelocity) close();
|
||||
else {
|
||||
float openPercent = 1f * getSurfaceView().getTop() / mDragDistance;
|
||||
if(openPercent > willOpenPercent ) open();
|
||||
if (openPercent > willOpenPercent) open();
|
||||
else close();
|
||||
}
|
||||
}else if(currentDragEdge == DragEdge.Bottom){
|
||||
if(yvel > minVelocity) close();
|
||||
else if(yvel < -minVelocity) open();
|
||||
else{
|
||||
} else if (currentDragEdge == DragEdge.Bottom) {
|
||||
if (yvel > minVelocity) close();
|
||||
else if (yvel < -minVelocity) open();
|
||||
else {
|
||||
float openPercent = 1f * (-getSurfaceView().getTop()) / mDragDistance;
|
||||
if(openPercent > willOpenPercent ) open();
|
||||
if (openPercent > willOpenPercent) open();
|
||||
else close();
|
||||
}
|
||||
}
|
||||
|
|
@ -1273,7 +1307,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
public void open(boolean smooth, boolean notify) {
|
||||
View surface = getSurfaceView(), bottom = getCurrentBottomView();
|
||||
if(surface == null){
|
||||
if (surface == null) {
|
||||
return;
|
||||
}
|
||||
int dx, dy;
|
||||
|
|
@ -1286,7 +1320,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
|
||||
if (getShowMode() == ShowMode.PullOut) {
|
||||
Rect bRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect);
|
||||
if(bottom!=null){
|
||||
if (bottom != null) {
|
||||
bottom.layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
|
||||
}
|
||||
}
|
||||
|
|
@ -1334,7 +1368,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
*/
|
||||
public void close(boolean smooth, boolean notify) {
|
||||
View surface = getSurfaceView();
|
||||
if(surface==null){
|
||||
if (surface == null) {
|
||||
return;
|
||||
}
|
||||
int dx, dy;
|
||||
|
|
@ -1368,6 +1402,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
/**
|
||||
* 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) {
|
||||
|
|
@ -1400,9 +1435,9 @@ public class SwipeLayout extends FrameLayout {
|
|||
|
||||
if (mCurrentDragEdge == DragEdge.Left || mCurrentDragEdge == DragEdge.Right) {
|
||||
bb = rect.bottom;
|
||||
br = bl + (bottomView == null? 0 :bottomView.getMeasuredWidth());
|
||||
br = bl + (bottomView == null ? 0 : bottomView.getMeasuredWidth());
|
||||
} else {
|
||||
bb = bt + (bottomView == null? 0 :bottomView.getMeasuredHeight());
|
||||
bb = bt + (bottomView == null ? 0 : bottomView.getMeasuredHeight());
|
||||
br = rect.right;
|
||||
}
|
||||
} else if (mode == ShowMode.LayDown) {
|
||||
|
|
@ -1442,7 +1477,7 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
public interface DoubleClickListener {
|
||||
public void onDoubleClick(SwipeLayout layout, boolean surface);
|
||||
void onDoubleClick(SwipeLayout layout, boolean surface);
|
||||
}
|
||||
|
||||
private int dp2px(float dp) {
|
||||
|
|
@ -1450,34 +1485,41 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
|
||||
/**Deprecated, use {@link #setDrag(DragEdge, View)} */
|
||||
/**
|
||||
* Deprecated, use {@link #setDrag(DragEdge, View)}
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDragEdge(DragEdge dragEdge) {
|
||||
clearDragEdge();
|
||||
if(getChildCount() >= 2){
|
||||
mDragEdges.put(dragEdge, getChildAt(getChildCount()-2));
|
||||
if (getChildCount() >= 2) {
|
||||
mDragEdges.put(dragEdge, getChildAt(getChildCount() - 2));
|
||||
}
|
||||
setCurrentDragEdge(dragEdge);
|
||||
}
|
||||
|
||||
protected void onViewRemoved(View child) {
|
||||
for(Map.Entry<DragEdge, View> entry : new HashMap<DragEdge, View>(mDragEdges).entrySet()){
|
||||
if(entry.getValue() == child){
|
||||
for (Map.Entry<DragEdge, View> entry : new HashMap<DragEdge, View>(mDragEdges).entrySet()) {
|
||||
if (entry.getValue() == child) {
|
||||
mDragEdges.remove(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
public Map<DragEdge, View> getDragEdgeMap(){
|
||||
|
||||
public Map<DragEdge, View> getDragEdgeMap() {
|
||||
return mDragEdges;
|
||||
}
|
||||
|
||||
/**Deprecated, use {@link #getDragEdgeMap()} */
|
||||
/**
|
||||
* Deprecated, use {@link #getDragEdgeMap()}
|
||||
*/
|
||||
@Deprecated
|
||||
public List<DragEdge> getDragEdges() {
|
||||
return new ArrayList<DragEdge>(mDragEdges.keySet());
|
||||
}
|
||||
|
||||
/**Deprecated, use {@link #setDrag(DragEdge, View)} */
|
||||
/**
|
||||
* Deprecated, use {@link #setDrag(DragEdge, View)}
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDragEdges(List<DragEdge> dragEdges) {
|
||||
clearDragEdge();
|
||||
|
|
@ -1485,19 +1527,22 @@ public class SwipeLayout extends FrameLayout {
|
|||
DragEdge dragEdge = dragEdges.get(i);
|
||||
mDragEdges.put(dragEdge, getChildAt(i));
|
||||
}
|
||||
if(dragEdges.size()==0 || dragEdges.contains(DefaultDragEdge)){
|
||||
if (dragEdges.size() == 0 || dragEdges.contains(DefaultDragEdge)) {
|
||||
setCurrentDragEdge(DefaultDragEdge);
|
||||
}else{
|
||||
} else {
|
||||
setCurrentDragEdge(dragEdges.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
/**Deprecated, use {@link #addDrag(DragEdge, View)} */
|
||||
/**
|
||||
* Deprecated, use {@link #addDrag(DragEdge, View)}
|
||||
*/
|
||||
@Deprecated
|
||||
public void setDragEdges(DragEdge... mDragEdges) {
|
||||
clearDragEdge();
|
||||
setDragEdges(Arrays.asList(mDragEdges));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deprecated, use {@link #addDrag(DragEdge, View)}
|
||||
* When using multiple drag edges it's a good idea to pass the ids of the views that
|
||||
|
|
@ -1512,27 +1557,30 @@ public class SwipeLayout extends FrameLayout {
|
|||
}
|
||||
|
||||
private float getCurrentOffset() {
|
||||
if(mCurrentDragEdge==null) return 0;
|
||||
if (mCurrentDragEdge == null) return 0;
|
||||
return mEdgeSwipesOffset[mCurrentDragEdge.ordinal()];
|
||||
}
|
||||
|
||||
private void setCurrentDragEdge(DragEdge dragEdge){
|
||||
private void setCurrentDragEdge(DragEdge dragEdge) {
|
||||
mCurrentDragEdge = dragEdge;
|
||||
updateBottomViews();
|
||||
}
|
||||
|
||||
private void updateBottomViews() {
|
||||
View currentBottomView = getCurrentBottomView();
|
||||
if(currentBottomView!=null){
|
||||
if (currentBottomView != null) {
|
||||
if (mCurrentDragEdge == DragEdge.Left || mCurrentDragEdge == DragEdge.Right) {
|
||||
mDragDistance = currentBottomView.getMeasuredWidth() - dp2px(getCurrentOffset());
|
||||
} else {
|
||||
mDragDistance = currentBottomView.getMeasuredHeight() - dp2px(getCurrentOffset());
|
||||
}
|
||||
else mDragDistance = currentBottomView.getMeasuredHeight() - dp2px(getCurrentOffset());
|
||||
}
|
||||
|
||||
if (mShowMode == ShowMode.PullOut)
|
||||
if (mShowMode == ShowMode.PullOut) {
|
||||
layoutPullOut();
|
||||
else if (mShowMode == ShowMode.LayDown) layoutLayDown();
|
||||
} else if (mShowMode == ShowMode.LayDown) {
|
||||
layoutLayDown();
|
||||
}
|
||||
|
||||
safeBottomView();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue