Doigt listitem

Je tiens à mettre en œuvre un geste de balayage pour supprimer des lignes dans une ListView similaire à android notifications.

Dès maintenant tout ce que j'ai est un ListView avec un onTouchListener - cela dit, j'ai déjà balayez vers la détection de travail.

gestureDetector = new GestureDetector(this, new GestureListener());
onTouchListener = new TouchListener();  
listview.setOnTouchListener(onTouchListener);  

Mon GestureListener classe:

protected class GestureListener extends SimpleOnGestureListener
{
private static final int SWIPE_MIN_DISTANCE = 150;
private static final int SWIPE_MAX_OFF_PATH = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
private MotionEvent mLastOnDownEvent = null;
@Override
public boolean onDown(MotionEvent e)
{
mLastOnDownEvent = e;
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if(e1 == null){
e1 = mLastOnDownEvent;
}
if(e1==null || e2==null){
return false;
}
float dX = e2.getX() - e1.getX();
float dY = e1.getY() - e2.getY();
if (Math.abs(dY) < SWIPE_MAX_OFF_PATH && Math.abs(velocityX) >= SWIPE_THRESHOLD_VELOCITY && Math.abs(dX) >= SWIPE_MIN_DISTANCE ) {
if (dX > 0) {
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
}
return true;
}
else if (Math.abs(dX) < SWIPE_MAX_OFF_PATH && Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
if (dY>0) {
Toast.makeText(getApplicationContext(), "Up Swipe", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Down Swipe", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
}

Mon TouchListener classe:

protected class TouchListener implements View.OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent e)
{
if (gestureDetector.onTouchEvent(e)){
return true;
}else{
return false;
}
}
}

Sont là certains des tutoriels et des exemples sur qui?

grâce

OriginalL'auteur marcin | 2012-05-31