Skip to content

Commit b460d4d

Browse files
committed
Optimize the implement when surface or bottom not a GroupView
1 parent 8fea955 commit b460d4d

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,12 @@
9191
</RelativeLayout>
9292
</LinearLayout>
9393

94-
<LinearLayout
94+
<TextView
9595
android:padding="10dp"
9696
android:background="@drawable/white"
97+
android:tag="Hover"
98+
android:text="要有最樸素的生活和最遙遠的夢想,即使明天天寒地凍,山高水遠,路遠馬亡。"
9799
android:layout_width="match_parent"
98-
android:layout_height="match_parent">
99-
100-
<TextView
101-
android:tag="Hover"
102-
android:text="要有最樸素的生活和最遙遠的夢想,即使明天天寒地凍,山高水遠,路遠馬亡。"
103-
android:layout_width="match_parent"
104-
android:layout_height="match_parent" />
105-
</LinearLayout>
100+
android:layout_height="match_parent" />
106101

107102
</com.daimajia.swipe.SwipeLayout>

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

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,10 @@ protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open)
560560
*/
561561
private void safeBottomView() {
562562
Status status = getOpenStatus();
563-
List<ViewGroup> bottoms = getBottomViews();
563+
List<View> bottoms = getBottomViews();
564564

565565
if (status == Status.Close) {
566-
for (ViewGroup bottom : bottoms) {
566+
for (View bottom : bottoms) {
567567
if (bottom.getVisibility() != INVISIBLE) bottom.setVisibility(INVISIBLE);
568568
}
569569
} else {
@@ -696,13 +696,6 @@ public void addBottomView(View child, ViewGroup.LayoutParams params, DragEdge dr
696696
}
697697
@Override
698698
public void addView(View child, int index, ViewGroup.LayoutParams params) {
699-
//the child should be viewGroup, convert child here
700-
if(!(child instanceof ViewGroup)){
701-
WrapGroup childContain = new WrapGroup(getContext());
702-
childContain.addView(child);
703-
child = childContain;
704-
}
705-
706699
int gravity = Gravity.NO_GRAVITY;
707700
try {
708701
gravity = (Integer) params.getClass().getField("gravity").get(params);
@@ -762,11 +755,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
762755
" ChildCount:" + childCount +
763756
", mDragEdges.size():"+ mDragEdges.size());
764757
}
765-
for (int i = 0; i < childCount; i++) {
766-
if (!(getChildAt(i) instanceof ViewGroup)) {
767-
throw new IllegalArgumentException("All the children in SwipeLayout must be an instance of ViewGroup");
768-
}
769-
}
770758

771759
if (mShowMode == ShowMode.PullOut)
772760
layoutPullOut();
@@ -1134,8 +1122,8 @@ public boolean onSingleTapUp(MotionEvent e) {
11341122
public boolean onDoubleTap(MotionEvent e) {
11351123
if (mDoubleClickListener != null) {
11361124
View target;
1137-
ViewGroup bottom = getBottomViews().get(mCurrentDirectionIndex);
1138-
ViewGroup surface = getSurfaceView();
1125+
View bottom = getBottomViews().get(mCurrentDirectionIndex);
1126+
View surface = getSurfaceView();
11391127
if (e.getX() > bottom.getLeft() && e.getX() < bottom.getRight() && e.getY() > bottom.getTop()
11401128
&& e.getY() < bottom.getBottom()) {
11411129
target = bottom;
@@ -1193,37 +1181,37 @@ public ShowMode getShowMode() {
11931181
return mShowMode;
11941182
}
11951183

1196-
public ViewGroup getSurfaceView() {
1197-
return (ViewGroup) getChildAt(getChildCount() - 1);
1184+
public View getSurfaceView() {
1185+
return (View) getChildAt(getChildCount() - 1);
11981186
}
11991187

12001188
/**
12011189
* @return all bottomViews.
12021190
*/
1203-
public List<ViewGroup> getBottomViews() {
1204-
List<ViewGroup> lvg = new ArrayList<ViewGroup>();
1191+
public List<View> getBottomViews() {
1192+
List<View> lvg = new ArrayList<View>();
12051193
// If the user has provided a map for views to
12061194
if (mBottomViewIdsSet) {
1207-
lvg.addAll(Arrays.asList(new ViewGroup[mDragEdges.size()]));
1195+
lvg.addAll(Arrays.asList(new View[mDragEdges.size()]));
12081196

12091197
if (mDragEdges.contains(DragEdge.Left)) {
1210-
lvg.set(mLeftIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
1198+
lvg.set(mLeftIndex, ((View) findViewById(mBottomViewIdMap.get(DragEdge.Left))));
12111199
}
12121200
if (mDragEdges.contains(DragEdge.Top)) {
1213-
lvg.set(mTopIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
1201+
lvg.set(mTopIndex, ((View) findViewById(mBottomViewIdMap.get(DragEdge.Top))));
12141202
}
12151203
if (mDragEdges.contains(DragEdge.Right)) {
1216-
lvg.set(mRightIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
1204+
lvg.set(mRightIndex, ((View) findViewById(mBottomViewIdMap.get(DragEdge.Right))));
12171205
}
12181206
if (mDragEdges.contains(DragEdge.Bottom)) {
1219-
lvg.set(mBottomIndex, ((ViewGroup) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
1207+
lvg.set(mBottomIndex, ((View) findViewById(mBottomViewIdMap.get(DragEdge.Bottom))));
12201208
}
12211209
}
12221210
// Default behaviour is to simply use the first n-1 children in the order they're listed in the layout
12231211
// and return them in
12241212
else {
12251213
for (int i = 0; i < (getChildCount() - 1); i++) {
1226-
lvg.add((ViewGroup) getChildAt(i));
1214+
lvg.add((View) getChildAt(i));
12271215
}
12281216
}
12291217
return lvg;
@@ -1418,7 +1406,7 @@ public void open(boolean smooth) {
14181406
}
14191407

14201408
public void open(boolean smooth, boolean notify) {
1421-
ViewGroup surface = getSurfaceView(), bottom = getBottomViews().get(mCurrentDirectionIndex);
1409+
View surface = getSurfaceView(), bottom = getBottomViews().get(mCurrentDirectionIndex);
14221410
int dx, dy;
14231411
Rect rect = computeSurfaceLayoutArea(true);
14241412
if (smooth) {
@@ -1501,7 +1489,7 @@ public void close(boolean smooth) {
15011489
* @param notify if notify all the listeners.
15021490
*/
15031491
public void close(boolean smooth, boolean notify) {
1504-
ViewGroup surface = getSurfaceView();
1492+
View surface = getSurfaceView();
15051493
int dx, dy;
15061494
if (smooth)
15071495
mDragHelper.smoothSlideViewTo(getSurfaceView(), getPaddingLeft(), getPaddingTop());
@@ -1666,11 +1654,4 @@ private void updateBottomViews() {
16661654
mOnLayoutListeners.get(i).onLayout(this);
16671655
}
16681656
}
1669-
1670-
//if child is not viewGroup, this group will wrap it
1671-
public class WrapGroup extends FrameLayout{
1672-
public WrapGroup(Context context) {
1673-
super(context);
1674-
}
1675-
}
16761657
}

0 commit comments

Comments
 (0)