a better auto-close SwipeAdapter(sigle item mode)

This commit is contained in:
daimajia 2014-09-17 12:10:31 +08:00
parent 62742aa99c
commit 7db7da3f58
4 changed files with 100 additions and 71 deletions

View File

@ -8,6 +8,7 @@ import android.widget.TextView;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeAdapter;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipedemo.R;
@ -29,26 +30,11 @@ public class ListViewAdapter extends SwipeAdapter {
public View generateView(int position, ViewGroup parent) {
View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
@Override
public void onOpen(SwipeLayout layout) {
YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
return v;
}

View File

@ -1,10 +1,18 @@
package com.daimajia.swipe;
/**
* Created by sbaiget on 29/08/2014.
*/
public class SimpleSwipeListener implements SwipeLayout.SwipeListener {
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onClose(SwipeLayout layout) {
@ -14,10 +22,6 @@ public class SimpleSwipeListener implements SwipeLayout.SwipeListener {
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onOpen(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}

View File

@ -149,21 +149,22 @@ public abstract class SwipeAdapter extends BaseAdapter {
public void onLayout(SwipeLayout v) {
if(mode == Mode.Multiple){
if(mOpenPositions.contains(position))
v.open(false);
v.open(false, false);
else{
v.close(false);
v.close(false, false);
}
}else{
if(mOpenPosition == position){
v.open(false);
v.open(false, false);
}else{
v.close(false);
v.close(false, false);
}
}
}
}
class SwipeMemory implements SwipeLayout.SwipeListener {
class SwipeMemory extends SimpleSwipeListener{
private int position;
@ -176,25 +177,16 @@ public abstract class SwipeAdapter extends BaseAdapter {
if(mode == Mode.Multiple)
mOpenPositions.remove(position);
else{
if(position == mOpenPosition){
if (position == mOpenPosition) {
mOpenPosition = INVALID_POSITION;
mPrevious = null;
}
}
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onOpen(SwipeLayout layout) {
if(mode == Mode.Multiple)
mOpenPositions.add(position);
else{
public void onStartOpen(SwipeLayout layout) {
if(mode == Mode.Single) {
if(mOpenPosition != position){
if(mPrevious != null)
mPrevious.close();
@ -205,8 +197,9 @@ public abstract class SwipeAdapter extends BaseAdapter {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
public void onOpen(SwipeLayout layout) {
if(mode == Mode.Multiple)
mOpenPositions.add(position);
}
public void setPosition(int position){
@ -214,6 +207,4 @@ public abstract class SwipeAdapter extends BaseAdapter {
}
}
}

View File

@ -70,9 +70,11 @@ public class SwipeLayout extends FrameLayout {
public interface SwipeListener{
public void onStartOpen(SwipeLayout layout);
public void onOpen(SwipeLayout layout);
public void onStartClose(SwipeLayout layout);
public void onClose(SwipeLayout layout);
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset);
public void onOpen(SwipeLayout layout);
public void onHandRelease(SwipeLayout layout, float xvel, float yvel);
}
@ -324,13 +326,11 @@ public class SwipeLayout extends FrameLayout {
getSurfaceView().layout(newLeft, newTop, newLeft + getMeasuredWidth(), newTop + getMeasuredHeight());
}
}
dispatchRevealEvent(evLeft, evTop, evRight, evBottom);
dispatchSwipeEvent(evLeft, evTop);
dispatchSwipeEvent(evLeft, evTop, dx, dy);
invalidate();
}
@ -441,29 +441,54 @@ public class SwipeLayout extends FrameLayout {
return r;
}
/**
* dispatch swipe event.
* @param surfaceLeft
* @param surfaceTop
*/
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop){
private int mEventCounter = 0;
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, int dx, int dy){
DragEdge edge = getDragEdge();
boolean open = true;
if(edge == DragEdge.Left){
if(dx < 0) open = false;
}else if(edge == DragEdge.Right){
if(dx > 0) open = false;
}else if(edge == DragEdge.Top){
if(dy < 0) open = false;
}else if(edge == DragEdge.Bottom){
if(dy > 0) open = false;
}
dispatchSwipeEvent(surfaceLeft, surfaceTop, open);
}
protected void dispatchSwipeEvent(int surfaceLeft, int surfaceTop, boolean open){
safeBottomView();
Status status = getOpenStatus();
if(mSwipeListeners.isEmpty() == false){
if(!mSwipeListeners.isEmpty()){
mEventCounter++;
for(SwipeListener l : mSwipeListeners){
if(mEventCounter == 1){
if(open){
l.onStartOpen(this);
}else{
l.onStartClose(this);
}
}
l.onUpdate(SwipeLayout.this, surfaceLeft - getPaddingLeft(), surfaceTop - getPaddingTop());
}
if(getOpenStatus() == Status.Close){
for(SwipeListener l : mSwipeListeners)
if(status == Status.Close){
for(SwipeListener l : mSwipeListeners){
l.onClose(SwipeLayout.this);
}
mEventCounter = 0;
}
if(getOpenStatus() == Status.Open){
if(status == Status.Open){
getBottomView().setEnabled(true);
for(SwipeListener l : mSwipeListeners)
for(SwipeListener l : mSwipeListeners){
l.onOpen(SwipeLayout.this);
}
mEventCounter = 0;
}
}
}
@ -1082,22 +1107,34 @@ public class SwipeLayout extends FrameLayout {
* smoothly open surface.
*/
public void open(){
open(true);
open(true, true);
}
public void open(boolean smooth){
open(smooth, true);
}
public void open(boolean smooth, boolean notify){
ViewGroup surface = getSurfaceView(), bottom = getBottomView();
int dx,dy;
Rect rect = computeSurfaceLayoutArea(true);
if(smooth) {
mDragHelper.smoothSlideViewTo(getSurfaceView(), rect.left, rect.top);
}
else{
getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom);
dx = rect.left - surface.getLeft();
dy = rect.top - surface.getTop();
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
if(getShowMode() == ShowMode.PullOut){
Rect bRect = computeBottomLayoutAreaViaSurface(ShowMode.PullOut, rect);
getBottomView().layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
bottom.layout(bRect.left, bRect.top, bRect.right, bRect.bottom);
}
if(notify) {
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
dispatchSwipeEvent(rect.left, rect.top, dx, dy);
}else{
safeBottomView();
}
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
dispatchSwipeEvent(rect.left, rect.top);
}
invalidate();
}
@ -1106,27 +1143,38 @@ public class SwipeLayout extends FrameLayout {
* smoothly close surface.
*/
public void close(){
close(true);
close(true, true);
}
public void close(boolean smooth){
close(smooth, true);
}
/**
* close surface
* @param smooth smoothly or not.
* @param notify if notify all the listeners.
*/
public void close(boolean smooth){
public void close(boolean smooth, boolean notify){
ViewGroup surface = getSurfaceView();
int dx, dy;
if(smooth)
mDragHelper.smoothSlideViewTo(getSurfaceView(), getPaddingLeft(), getPaddingTop());
else {
Rect rect = computeSurfaceLayoutArea(false);
getSurfaceView().layout(rect.left, rect.top, rect.right, rect.bottom);
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
dispatchSwipeEvent(rect.left, rect.top);
dx = rect.left - surface.getLeft();
dy = rect.top - surface.getTop();
surface.layout(rect.left, rect.top, rect.right, rect.bottom);
if(notify) {
dispatchRevealEvent(rect.left, rect.top, rect.right, rect.bottom);
dispatchSwipeEvent(rect.left, rect.top, dx, dy);
}else{
safeBottomView();
}
}
invalidate();
}
public void toggle(){
toggle(true);
}