Skip to content

Commit 7db7da3

Browse files
committedSep 17, 2014
a better auto-close SwipeAdapter(sigle item mode)
1 parent 62742aa commit 7db7da3

File tree

4 files changed

+99
-70
lines changed

4 files changed

+99
-70
lines changed
 

‎demo/src/main/java/com/daimajia/swipedemo/adapter/ListViewAdapter.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.daimajia.androidanimations.library.Techniques;
1010
import com.daimajia.androidanimations.library.YoYo;
11+
import com.daimajia.swipe.SimpleSwipeListener;
1112
import com.daimajia.swipe.SwipeAdapter;
1213
import com.daimajia.swipe.SwipeLayout;
1314
import com.daimajia.swipedemo.R;
@@ -29,26 +30,11 @@ public int getSwipeLayoutResourceId(int position) {
2930
public View generateView(int position, ViewGroup parent) {
3031
View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
3132
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
32-
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
33-
@Override
34-
public void onClose(SwipeLayout layout) {
35-
36-
}
37-
38-
@Override
39-
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
40-
41-
}
42-
33+
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
4334
@Override
4435
public void onOpen(SwipeLayout layout) {
4536
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
4637
}
47-
48-
@Override
49-
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
50-
51-
}
5238
});
5339
return v;
5440
}

‎library/src/main/java/com/daimajia/swipe/SimpleSwipeListener.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.daimajia.swipe;
22

3-
/**
4-
* Created by sbaiget on 29/08/2014.
5-
*/
63
public class SimpleSwipeListener implements SwipeLayout.SwipeListener {
74

5+
@Override
6+
public void onStartOpen(SwipeLayout layout) {
7+
}
88

99
@Override
10-
public void onClose(SwipeLayout layout) {
10+
public void onOpen(SwipeLayout layout) {
1111
}
1212

1313
@Override
14-
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
14+
public void onStartClose(SwipeLayout layout) {
1515
}
1616

1717
@Override
18-
public void onOpen(SwipeLayout layout) {
18+
public void onClose(SwipeLayout layout) {
19+
}
20+
21+
@Override
22+
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
1923
}
2024

2125
@Override

‎library/src/main/java/com/daimajia/swipe/SwipeAdapter.java

+12-21
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,22 @@ public void setPosition(int position){
149149
public void onLayout(SwipeLayout v) {
150150
if(mode == Mode.Multiple){
151151
if(mOpenPositions.contains(position))
152-
v.open(false);
152+
v.open(false, false);
153153
else{
154-
v.close(false);
154+
v.close(false, false);
155155
}
156156
}else{
157157
if(mOpenPosition == position){
158-
v.open(false);
158+
v.open(false, false);
159159
}else{
160-
v.close(false);
160+
v.close(false, false);
161161
}
162162
}
163163
}
164+
164165
}
165166

166-
class SwipeMemory implements SwipeLayout.SwipeListener {
167+
class SwipeMemory extends SimpleSwipeListener{
167168

168169
private int position;
169170

@@ -176,25 +177,16 @@ public void onClose(SwipeLayout layout) {
176177
if(mode == Mode.Multiple)
177178
mOpenPositions.remove(position);
178179
else{
179-
if(position == mOpenPosition){
180+
if (position == mOpenPosition) {
180181
mOpenPosition = INVALID_POSITION;
181182
mPrevious = null;
182183
}
183-
184184
}
185185
}
186186

187-
188187
@Override
189-
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
190-
191-
}
192-
193-
@Override
194-
public void onOpen(SwipeLayout layout) {
195-
if(mode == Mode.Multiple)
196-
mOpenPositions.add(position);
197-
else{
188+
public void onStartOpen(SwipeLayout layout) {
189+
if(mode == Mode.Single) {
198190
if(mOpenPosition != position){
199191
if(mPrevious != null)
200192
mPrevious.close();
@@ -205,15 +197,14 @@ public void onOpen(SwipeLayout layout) {
205197
}
206198

207199
@Override
208-
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
209-
200+
public void onOpen(SwipeLayout layout) {
201+
if(mode == Mode.Multiple)
202+
mOpenPositions.add(position);
210203
}
211204

212205
public void setPosition(int position){
213206
this.position = position;
214207
}
215208
}
216209

217-
218-
219210
}

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

+75-27
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ public SwipeLayout(Context context, AttributeSet attrs, int defStyle) {
7070

7171

7272
public interface SwipeListener{
73+
public void onStartOpen(SwipeLayout layout);
74+
public void onOpen(SwipeLayout layout);
75+
public void onStartClose(SwipeLayout layout);
7376
public void onClose(SwipeLayout layout);
7477
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset);
75-
public void onOpen(SwipeLayout layout);
7678
public void onHandRelease(SwipeLayout layout, float xvel, float yvel);
7779
}
7880

@@ -324,13 +326,11 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i
324326

325327
getSurfaceView().layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight());
326328
}
327-
328-
329329
}
330330

331331
dispatchRevealEvent(evLeft, evTop, evRight, evBottom);
332332

333-
dispatchSwipeEvent(evLeft, evTop);
333+
dispatchSwipeEvent(evLeft, evTop, dx, dy);
334334

335335
invalidate();
336336
}
@@ -441,29 +441,54 @@ protected Rect getRelativePosition(View child){
441441
return r;
442442
}
443443

444-
/**
445-
* dispatch swipe event.
446-
* @param surfaceLeft
447-
* @param surfaceTop
448-
*/
449-
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop){
444+
private int mEventCounter = 0;
445+
446+
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy){
447+
DragEdge edge = getDragEdge();
448+
boolean open = true;
449+
if(edge == DragEdge.Left){
450+
if(dx < 0) open = false;
451+
}else if(edge == DragEdge.Right){
452+
if(dx > 0) open = false;
453+
}else if(edge == DragEdge.Top){
454+
if(dy < 0) open = false;
455+
}else if(edge == DragEdge.Bottom){
456+
if(dy > 0) open = false;
457+
}
458+
459+
dispatchSwipeEvent(surfaceLeft, surfaceTop, open);
460+
}
450461

462+
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open){
451463
safeBottomView();
464+
Status status = getOpenStatus();
452465

453-
if(mSwipeListeners.isEmpty() == false){
466+
if(!mSwipeListeners.isEmpty()){
467+
mEventCounter++;
454468
for(SwipeListener l : mSwipeListeners){
469+
if(mEventCounter == 1){
470+
if(open){
471+
l.onStartOpen(this);
472+
}else{
473+
l.onStartClose(this);
474+
}
475+
}
455476
l.onUpdate(SwipeLayout.this, surfaceLeft - getPaddingLeft(), surfaceTop - getPaddingTop());
456477
}
457478

458-
if(getOpenStatus() == Status.Close){
459-
for(SwipeListener l : mSwipeListeners)
479+
if(status == Status.Close){
480+
for(SwipeListener l : mSwipeListeners){
460481
l.onClose(SwipeLayout.this);
482+
}
483+
mEventCounter = 0;
461484
}
462485

463-
if(getOpenStatus() == Status.Open){
486+
if(status == Status.Open){
464487
getBottomView().setEnabled(true);
465-
for(SwipeListener l : mSwipeListeners)
488+
for(SwipeListener l : mSwipeListeners){
466489
l.onOpen(SwipeLayout.this);
490+
}
491+
mEventCounter = 0;
467492
}
468493
}
469494
}
@@ -1082,22 +1107,34 @@ private void processBottomLayDownMode(float xvel, float yvel){
10821107
* smoothly open surface.
10831108
*/
10841109
public void open(){
1085-
open(true);
1110+
open(true, true);
10861111
}
10871112

10881113
public void open(boolean smooth){
1114+
open(smooth, true);
1115+
}
1116+
1117+
public void open(boolean smooth, boolean notify){
1118+
ViewGroup surface = getSurfaceView(), bottom = getBottomView();
1119+
int dx,dy;
10891120
Rect rect = computeSurfaceLayoutArea(true);
10901121
if(smooth) {
10911122
mDragHelper.smoothSlideViewTo(getSurfaceView(), rect.left, rect.top);
10921123
}
10931124
else{
1094-
getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom);
1125+
dx = rect.left - surface.getLeft();
1126+
dy = rect.top - surface.getTop();
1127+
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
10951128
if(getShowMode() == ShowMode.PullOut){
10961129
Rect bRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect);
1097-
getBottomView().layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
1130+
bottom.layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
1131+
}
1132+
if(notify) {
1133+
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
1134+
dispatchSwipeEvent(rect.left, rect.top, dx, dy);
1135+
}else{
1136+
safeBottomView();
10981137
}
1099-
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
1100-
dispatchSwipeEvent(rect.left, rect.top);
11011138
}
11021139
invalidate();
11031140
}
@@ -1106,27 +1143,38 @@ public void open(boolean smooth){
11061143
* smoothly close surface.
11071144
*/
11081145
public void close(){
1109-
close(true);
1146+
close(true, true);
1147+
}
1148+
1149+
public void close(boolean smooth){
1150+
close(smooth, true);
11101151
}
11111152

11121153
/**
11131154
* close surface
11141155
* @param smooth smoothly or not.
1156+
* @param notify if notify all the listeners.
11151157
*/
1116-
public void close(boolean smooth){
1158+
public void close(boolean smooth, boolean notify){
1159+
ViewGroup surface = getSurfaceView();
1160+
int dx, dy;
11171161
if(smooth)
11181162
mDragHelper.smoothSlideViewTo(getSurfaceView(), getPaddingLeft(), getPaddingTop());
11191163
else {
11201164
Rect rect = computeSurfaceLayoutArea(false);
1121-
getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom);
1122-
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
1123-
dispatchSwipeEvent(rect.left, rect.top);
1165+
dx = rect.left - surface.getLeft();
1166+
dy = rect.top - surface.getTop();
1167+
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
1168+
if(notify) {
1169+
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
1170+
dispatchSwipeEvent(rect.left, rect.top, dx, dy);
1171+
}else{
1172+
safeBottomView();
1173+
}
11241174
}
11251175
invalidate();
11261176
}
11271177

1128-
1129-
11301178
public void toggle(){
11311179
toggle(true);
11321180
}

0 commit comments

Comments
 (0)
Please sign in to comment.