Skip to content

Commit 6e909b0

Browse files
committed
The bottom and surface views needn't be ViewGroup.
The bottom view can use layout_gravity to indicate the swiping direction
1 parent 8e89db0 commit 6e909b0

File tree

4 files changed

+123
-37
lines changed

4 files changed

+123
-37
lines changed

demo/src/main/java/com/daimajia/swipedemo/MyActivity.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public class MyActivity extends Activity {
2121
protected void onCreate(Bundle savedInstanceState) {
2222
super.onCreate(savedInstanceState);
2323
setContentView(R.layout.main);
24-
SwipeLayout godfatherSwipe = (SwipeLayout) findViewById(R.id.godfather);
25-
godfatherSwipe.setDragEdges(SwipeLayout.DragEdge.Left, SwipeLayout.DragEdge.Right, SwipeLayout.DragEdge.Top, SwipeLayout.DragEdge.Bottom);
26-
godfatherSwipe.setBottomViewIds(R.id.bird_left, R.id.bird_right, R.id.bird_top, R.id.bird_bottom);
2724

2825
// SwipeLayout swipeLayout = (SwipeLayout)findViewById(R.id.godfather);
2926
// swipeLayout.setDragEdge(SwipeLayout.DragEdge.Bottom); // Set in XML

demo/src/main/res/layout/main.xml

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
32
<com.daimajia.swipe.SwipeLayout
43
xmlns:android="http://schemas.android.com/apk/res/android"
5-
xmlns:app="http://schemas.android.com/apk/res-auto"
64
android:id="@+id/godfather"
7-
android:layout_width="match_parent" android:layout_height="match_parent">
8-
<LinearLayout
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
<ImageView
98
android:id="@+id/bird_left"
10-
android:gravity="center"
9+
android:layout_gravity="left"
10+
android:src="@drawable/bird"
1111
android:layout_width="100dp"
12-
android:layout_height="match_parent">
13-
<ImageView
14-
android:src="@drawable/bird"
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content" />
17-
</LinearLayout>
18-
<LinearLayout
12+
android:layout_height="match_parent" />
13+
<ImageView
1914
android:id="@+id/bird_right"
20-
android:gravity="center"
15+
android:layout_gravity="right"
16+
android:src="@drawable/bird"
2117
android:layout_width="100dp"
22-
android:layout_height="match_parent">
23-
<ImageView
24-
android:src="@drawable/bird"
25-
android:layout_width="wrap_content"
26-
android:layout_height="wrap_content" />
27-
</LinearLayout>
18+
android:layout_height="match_parent" />
2819
<LinearLayout
2920
android:id="@+id/bird_top"
21+
android:layout_gravity="top"
3022
android:gravity="center"
3123
android:layout_width="match_parent"
3224
android:layout_height="100dp">
@@ -37,6 +29,7 @@
3729
</LinearLayout>
3830
<LinearLayout
3931
android:id="@+id/bird_bottom"
32+
android:layout_gravity="bottom"
4033
android:gravity="center"
4134
android:layout_width="match_parent"
4235
android:layout_height="match_parent">

demo/src/main/res/layout/sample3.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@
2222
</RelativeLayout>
2323
</LinearLayout>
2424

25-
<LinearLayout
25+
<TextView
2626
android:padding="10dp"
2727
android:background="#ffffff"
28+
android:tag="Hover"
29+
android:text="None is of freedom or of life deserving unless he daily conquers it anew. "
2830
android:layout_width="match_parent"
29-
android:layout_height="match_parent">
30-
<TextView
31-
android:tag="Hover"
32-
android:text="None is of freedom or of life deserving unless he daily conquers it anew. "
33-
android:layout_width="match_parent"
34-
android:layout_height="match_parent" />
35-
</LinearLayout>
31+
android:layout_height="match_parent" />
3632
</com.daimajia.swipe.SwipeLayout>

library/src/main/java/com/daimajia/swipe/SwipeLayout.java

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import android.content.Context;
44
import android.content.res.TypedArray;
55
import android.graphics.Rect;
6+
import android.os.Build;
7+
import android.support.v4.view.GravityCompat;
68
import android.support.v4.view.ViewCompat;
79
import android.support.v4.widget.ViewDragHelper;
810
import android.util.AttributeSet;
911
import android.view.GestureDetector;
12+
import android.view.Gravity;
1013
import android.view.HapticFeedbackConstants;
1114
import android.view.MotionEvent;
1215
import android.view.View;
@@ -19,6 +22,7 @@
1922

2023
import java.lang.reflect.Method;
2124
import java.util.ArrayList;
25+
import java.util.Arrays;
2226
import java.util.HashMap;
2327
import java.util.List;
2428
import java.util.Map;
@@ -118,6 +122,7 @@ public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
118122
int ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal());
119123
mShowMode = ShowMode.values()[ordinal];
120124
a.recycle();
125+
121126
}
122127

123128
public interface SwipeListener {
@@ -660,12 +665,95 @@ public void addOnLayoutListener(OnLayout l) {
660665
public void removeOnLayoutListener(OnLayout l) {
661666
if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l);
662667
}
668+
public void addBottomView(View child, DragEdge dragEdge){
669+
addBottomView(child, null, dragEdge);
670+
}
671+
public void addBottomView(View child, ViewGroup.LayoutParams params, DragEdge dragEdge){
672+
if(params==null){
673+
params = generateDefaultLayoutParams();
674+
}
675+
if(!checkLayoutParams(params)){
676+
params = generateLayoutParams(params);
677+
}
678+
int gravity = -1;
679+
switch (dragEdge){
680+
case Left:gravity = Gravity.LEFT;break;
681+
case Right:gravity = Gravity.RIGHT;break;
682+
case Top:gravity = Gravity.TOP;break;
683+
case Bottom:gravity = Gravity.BOTTOM;break;
684+
}
685+
if(params instanceof FrameLayout.LayoutParams){
686+
((LayoutParams) params).gravity = gravity;
687+
}
688+
super.addView(child, 0, params);
689+
}
690+
@Override
691+
public void addView(View child, int index, ViewGroup.LayoutParams params) {
692+
//the child should be viewGroup, convert child here
693+
if(!(child instanceof ViewGroup)){
694+
WrapGroup childContain = new WrapGroup(getContext());
695+
childContain.addView(child);
696+
child = childContain;
697+
}
698+
699+
int gravity = Gravity.NO_GRAVITY;
700+
try {
701+
gravity = (Integer) params.getClass().getField("gravity").get(params);
702+
} catch (Exception e) {
703+
e.printStackTrace();
704+
}
705+
706+
if(gravity>0){
707+
//declared the layout_gravity, set the child's drag edge
708+
if(child.getId()==View.NO_ID){
709+
if(Build.VERSION.SDK_INT<17){
710+
child.setId(child.hashCode());
711+
}else{
712+
child.setId(View.generateViewId());
713+
}
714+
}
715+
gravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
716+
717+
if(gravity == Gravity.LEFT){
718+
mBottomViewIdsSet = true;
719+
if(!mDragEdges.contains(DragEdge.Left)){
720+
mDragEdges.add(DragEdge.Left);
721+
}
722+
mBottomViewIdMap.put(DragEdge.Left, child.getId());
723+
}
724+
if(gravity == Gravity.RIGHT){
725+
mBottomViewIdsSet = true;
726+
if(!mDragEdges.contains(DragEdge.Right)){
727+
mDragEdges.add(DragEdge.Right);
728+
}
729+
mBottomViewIdMap.put(DragEdge.Right, child.getId());
730+
}
731+
if(gravity == Gravity.TOP){
732+
mBottomViewIdsSet = true;
733+
if(!mDragEdges.contains(DragEdge.Top)){
734+
mDragEdges.add(DragEdge.Top);
735+
}
736+
mBottomViewIdMap.put(DragEdge.Top, child.getId());
737+
}
738+
if(gravity == Gravity.BOTTOM){
739+
mBottomViewIdsSet = true;
740+
if(!mDragEdges.contains(DragEdge.Bottom)){
741+
mDragEdges.add(DragEdge.Bottom);
742+
}
743+
mBottomViewIdMap.put(DragEdge.Bottom, child.getId());
744+
}
745+
populateIndexes();
746+
}
747+
super.addView(child, index, params);
748+
}
663749

664750
@Override
665751
protected void onLayout(boolean changed, int l, int t, int r, int b) {
666752
int childCount = getChildCount();
667753
if (childCount != 1 + mDragEdges.size()) {
668-
throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges");
754+
throw new IllegalStateException("You need to have one surface view plus one view for each of your drag edges." +
755+
" ChildCount:" + childCount +
756+
", mDragEdges.size():"+ mDragEdges.size());
669757
}
670758
for (int i = 0; i < childCount; i++) {
671759
if (!(getChildAt(i) instanceof ViewGroup)) {
@@ -1081,21 +1169,26 @@ public ViewGroup getSurfaceView() {
10811169
return (ViewGroup) getChildAt(getChildCount() - 1);
10821170
}
10831171

1172+
/**
1173+
* @return all bottomViews.
1174+
*/
10841175
public List<ViewGroup> getBottomViews() {
10851176
List<ViewGroup> lvg = new ArrayList<ViewGroup>();
10861177
// If the user has provided a map for views to
10871178
if (mBottomViewIdsSet) {
1179+
lvg.addAll(Arrays.asList(new ViewGroup[mDragEdges.size()]));
1180+
10881181
if (mDragEdges.contains(DragEdge.Left)) {
1089-
lvg.add(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
1090-
}
1091-
if (mDragEdges.contains(DragEdge.Right)) {
1092-
lvg.add(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
1182+
lvg.set(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
10931183
}
10941184
if (mDragEdges.contains(DragEdge.Top)) {
1095-
lvg.add(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
1185+
lvg.set(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
1186+
}
1187+
if (mDragEdges.contains(DragEdge.Right)) {
1188+
lvg.set(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
10961189
}
10971190
if (mDragEdges.contains(DragEdge.Bottom)) {
1098-
lvg.add(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
1191+
lvg.set(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
10991192
}
11001193
}
11011194
// Default behaviour is to simply use the first n-1 children in the order they're listed in the layout
@@ -1524,4 +1617,11 @@ private void updateBottomViews() {
15241617
mOnLayoutListeners.get(i).onLayout(this);
15251618
}
15261619
}
1620+
1621+
//if child is not viewGroup, this group will wrap it
1622+
public class WrapGroup extends FrameLayout{
1623+
public WrapGroup(Context context) {
1624+
super(context);
1625+
}
1626+
}
15271627
}

0 commit comments

Comments
 (0)