|
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.res.TypedArray; |
5 | 5 | import android.graphics.Rect; |
| 6 | +import android.os.Build; |
| 7 | +import android.support.v4.view.GravityCompat; |
6 | 8 | import android.support.v4.view.ViewCompat; |
7 | 9 | import android.support.v4.widget.ViewDragHelper; |
8 | 10 | import android.util.AttributeSet; |
9 | 11 | import android.view.GestureDetector; |
| 12 | +import android.view.Gravity; |
10 | 13 | import android.view.HapticFeedbackConstants; |
11 | 14 | import android.view.MotionEvent; |
12 | 15 | import android.view.View; |
|
19 | 22 |
|
20 | 23 | import java.lang.reflect.Method; |
21 | 24 | import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
22 | 26 | import java.util.HashMap; |
23 | 27 | import java.util.List; |
24 | 28 | import java.util.Map; |
@@ -118,6 +122,7 @@ public SwipeLayout(Context context, AttributeSet attrs, int defStyle) { |
118 | 122 | int ordinal = a.getInt(R.styleable.SwipeLayout_show_mode, ShowMode.PullOut.ordinal()); |
119 | 123 | mShowMode = ShowMode.values()[ordinal]; |
120 | 124 | a.recycle(); |
| 125 | + |
121 | 126 | } |
122 | 127 |
|
123 | 128 | public interface SwipeListener { |
@@ -660,12 +665,95 @@ public void addOnLayoutListener(OnLayout l) { |
660 | 665 | public void removeOnLayoutListener(OnLayout l) { |
661 | 666 | if (mOnLayoutListeners != null) mOnLayoutListeners.remove(l); |
662 | 667 | } |
| 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 | + } |
663 | 749 |
|
664 | 750 | @Override |
665 | 751 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
666 | 752 | int childCount = getChildCount(); |
667 | 753 | 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()); |
669 | 757 | } |
670 | 758 | for (int i = 0; i < childCount; i++) { |
671 | 759 | if (!(getChildAt(i) instanceof ViewGroup)) { |
@@ -1081,21 +1169,26 @@ public ViewGroup getSurfaceView() { |
1081 | 1169 | return (ViewGroup) getChildAt(getChildCount() - 1); |
1082 | 1170 | } |
1083 | 1171 |
|
| 1172 | + /** |
| 1173 | + * @return all bottomViews. |
| 1174 | + */ |
1084 | 1175 | public List<ViewGroup> getBottomViews() { |
1085 | 1176 | List<ViewGroup> lvg = new ArrayList<ViewGroup>(); |
1086 | 1177 | // If the user has provided a map for views to |
1087 | 1178 | if (mBottomViewIdsSet) { |
| 1179 | + lvg.addAll(Arrays.asList(new ViewGroup[mDragEdges.size()])); |
| 1180 | + |
1088 | 1181 | 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)))); |
1093 | 1183 | } |
1094 | 1184 | 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)))); |
1096 | 1189 | } |
1097 | 1190 | 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)))); |
1099 | 1192 | } |
1100 | 1193 | } |
1101 | 1194 | // 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() { |
1524 | 1617 | mOnLayoutListeners.get(i).onLayout(this); |
1525 | 1618 | } |
1526 | 1619 | } |
| 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 | + } |
1527 | 1627 | } |
0 commit comments