I have an Android application in which there is List View Like this one
Now I want to perform two different activities on right/Left swipe of the List Items in the same way How native call log works
Right Swipe of list
I want this type of swipes. Can anyone know how to make it happen. Basically I wan to implement SimpleOnGestureListener
.
I have read a post answered by sir gav Fling gesture detection on grid layout and after that I am successful in implementing left swipe and right swipe detection but the only thing I am not getting on which List Item the swipe has occurred.
Update 24 May 2013
Now I am able to detect swipe action using this code --
SwipeDetector.java
/**
* Class swipe detection to View
*/
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to right
RL, // Right to left
TB, // Top to bottom
BT, // Bottom to top
None // Action not found
}
private static final int HORIZONTAL_MIN_DISTANCE = 30; // The minimum
// distance for
// horizontal swipe
private static final int VERTICAL_MIN_DISTANCE = 80; // The minimum distance
// for vertical
// swipe
private float downX, downY, upX, upY; // Coordinates
private Action mSwipeDetected = Action.None; // Last action
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
/**
* Swipe detection
*/@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
{
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE:
{
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
mSwipeDetected = Action.RL;
return true;
}
} else
// vertical swipe detection
if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {
mSwipeDetected = Action.BT;
return false;
}
}
return true;
}
}
return false;
}
}
Now use it with your ListView in this fashion
// Set the touch listener
final SwipeDetector swipeDetector = new SwipeDetector();
lv.setOnTouchListener(swipeDetector);
In your setOnItemClickListener
you can detect the swipe events like these
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView <? > parent, View view,
int position, long id) {
if (swipeDetector.swipeDetected()) {
if (swipeDetector.getAction() == SwipeDetector.Action.LR) {
Toast.makeText(getApplicationContext(),
"Left to right", Toast.LENGTH_SHORT).show();
}
if (swipeDetector.getAction() == SwipeDetector.Action.RL) {
Toast.makeText(getApplicationContext(),
"Right to left", Toast.LENGTH_SHORT).show();
}
}
}
});
But still I am not able to animate the swipe like these: