From 6e909b0343c03d95f76a16496b89459905ee54da Mon Sep 17 00:00:00 2001
From: linfaxin <673636904@qq.com>
Date: Tue, 17 Mar 2015 18:33:37 +0800
Subject: [PATCH] The bottom and surface views needn't be ViewGroup. The bottom
view can use layout_gravity to indicate the swiping direction
---
.../com/daimajia/swipedemo/MyActivity.java | 3 -
demo/src/main/res/layout/main.xml | 31 ++---
demo/src/main/res/layout/sample3.xml | 12 +-
.../java/com/daimajia/swipe/SwipeLayout.java | 114 ++++++++++++++++--
4 files changed, 123 insertions(+), 37 deletions(-)
diff --git a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java
index d65c6d9..d66656a 100644
--- a/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java
+++ b/demo/src/main/java/com/daimajia/swipedemo/MyActivity.java
@@ -21,9 +21,6 @@ public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
- SwipeLayout godfatherSwipe = (SwipeLayout) findViewById(R.id.godfather);
- godfatherSwipe.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right, SwipeLayout.DragEdge.Top, SwipeLayout.DragEdge.Bottom);
- godfatherSwipe.setBottomViewIds(R.id.bird_left, R.id.bird_right, R.id.bird_top, R.id.bird_bottom);
// SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.godfather);
// swipeLayout.setDragEdge(SwipeLayout.DragEdge.Bottom); // Set in XML
diff --git a/demo/src/main/res/layout/main.xml b/demo/src/main/res/layout/main.xml
index 4ba646a..55963be 100644
--- a/demo/src/main/res/layout/main.xml
+++ b/demo/src/main/res/layout/main.xml
@@ -1,32 +1,24 @@
-
-
+
-
-
-
+
-
-
+ android:layout_height="match_parent" />
@@ -37,6 +29,7 @@
diff --git a/demo/src/main/res/layout/sample3.xml b/demo/src/main/res/layout/sample3.xml
index dbad987..199c59e 100644
--- a/demo/src/main/res/layout/sample3.xml
+++ b/demo/src/main/res/layout/sample3.xml
@@ -22,15 +22,11 @@
-
-
-
+ android:layout_height="match_parent" />
\ No newline at end of file
diff --git a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java
index d6b58f0..fb98b5f 100644
--- a/library/src/main/java/com/daimajia/swipe/SwipeLayout.java
+++ b/library/src/main/java/com/daimajia/swipe/SwipeLayout.java
@@ -3,10 +3,13 @@ package com.daimajia.swipe;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
+import android.os.Build;
+import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.GestureDetector;
+import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.View;
@@ -19,6 +22,7 @@ import android.widget.FrameLayout;
import java.lang.reflect.Method;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -118,6 +122,7 @@ public class SwipeLayout extends FrameLayout {
int ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal());
mShowMode = ShowMode.values()[ordinal];
a.recycle();
+
}
public interface SwipeListener {
@@ -660,12 +665,95 @@ public class SwipeLayout extends FrameLayout {
public void removeOnLayoutListener(OnLayout l) {
if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l);
}
+ public void addBottomView(View child, DragEdge dragEdge){
+ addBottomView(child, null, dragEdge);
+ }
+ public void addBottomView(View child, ViewGroup.LayoutParams params, DragEdge dragEdge){
+ 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;
+ }
+ super.addView(child, 0, params);
+ }
+ @Override
+ public void addView(View child, int index, ViewGroup.LayoutParams params) {
+ //the child should be viewGroup, convert child here
+ if(!(child instanceof ViewGroup)){
+ WrapGroup childContain = new WrapGroup(getContext());
+ childContain.addView(child);
+ child = childContain;
+ }
+
+ int gravity = Gravity.NO_GRAVITY;
+ try {
+ gravity = (Integer) params.getClass().getField("gravity").get(params);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ if(gravity>0){
+ //declared the layout_gravity, set the child's drag edge
+ if(child.getId()==View.NO_ID){
+ if(Build.VERSION.SDK_INT<17){
+ child.setId(child.hashCode());
+ }else{
+ child.setId(View.generateViewId());
+ }
+ }
+ gravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
+
+ if(gravity == Gravity.LEFT){
+ mBottomViewIdsSet = true;
+ if(!mDragEdges.contains(DragEdge.Left)){
+ mDragEdges.add(DragEdge.Left);
+ }
+ mBottomViewIdMap.put(DragEdge.Left, child.getId());
+ }
+ if(gravity == Gravity.RIGHT){
+ mBottomViewIdsSet = true;
+ if(!mDragEdges.contains(DragEdge.Right)){
+ mDragEdges.add(DragEdge.Right);
+ }
+ mBottomViewIdMap.put(DragEdge.Right, child.getId());
+ }
+ if(gravity == Gravity.TOP){
+ mBottomViewIdsSet = true;
+ if(!mDragEdges.contains(DragEdge.Top)){
+ mDragEdges.add(DragEdge.Top);
+ }
+ mBottomViewIdMap.put(DragEdge.Top, child.getId());
+ }
+ if(gravity == Gravity.BOTTOM){
+ mBottomViewIdsSet = true;
+ if(!mDragEdges.contains(DragEdge.Bottom)){
+ mDragEdges.add(DragEdge.Bottom);
+ }
+ mBottomViewIdMap.put(DragEdge.Bottom, child.getId());
+ }
+ populateIndexes();
+ }
+ super.addView(child, index, params);
+ }
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
if (childCount != 1 + mDragEdges.size()) {
- throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges");
+ throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges." +
+ " ChildCount:" + childCount +
+ ", mDragEdges.size():"+ mDragEdges.size());
}
for (int i = 0; i < childCount; i++) {
if (!(getChildAt(i) instanceof ViewGroup)) {
@@ -1081,21 +1169,26 @@ public class SwipeLayout extends FrameLayout {
return (ViewGroup) getChildAt(getChildCount() - 1);
}
+ /**
+ * @return all bottomViews.
+ */
public List getBottomViews() {
List lvg = new ArrayList();
// If the user has provided a map for views to
if (mBottomViewIdsSet) {
+ lvg.addAll(Arrays.asList(new ViewGroup[mDragEdges.size()]));
+
if (mDragEdges.contains(DragEdge.Left)) {
- lvg.add(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
- }
- if (mDragEdges.contains(DragEdge.Right)) {
- lvg.add(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
+ lvg.set(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
}
if (mDragEdges.contains(DragEdge.Top)) {
- lvg.add(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
+ lvg.set(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
+ }
+ if (mDragEdges.contains(DragEdge.Right)) {
+ lvg.set(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
}
if (mDragEdges.contains(DragEdge.Bottom)) {
- lvg.add(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
+ lvg.set(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
}
}
// Default behaviour is to simply use the first n-1 children in the order they're listed in the layout
@@ -1524,4 +1617,11 @@ public class SwipeLayout extends FrameLayout {
mOnLayoutListeners.get(i).onLayout(this);
}
}
+
+ //if child is not viewGroup, this group will wrap it
+ public class WrapGroup extends FrameLayout{
+ public WrapGroup(Context context) {
+ super(context);
+ }
+ }
}