Swipe getItem(position) in listview

Adam First

Ok i've got a ListView, working fine, getting data from DB. Want to do the ol' swipe item in ListView left to delete.

I've implemented OnSwipeTouchListener - From here https://stackoverflow.com/a/19506010/2446010

Works fine. All happy.

Only problem - getItem(position) - how do I get the position of the item I have swiped on to say remove it?

mNameListView.setOnTouchListener(new OnSwipeTouchListener(this) {
    @Override
    public void onSwipeLeft() {
        WorkoutExercises workoutExercises = mWorkoutExercisesAdapter.getItem(position);
        workoutExercises.deleteInBackground();
        Toast.makeText(getApplicationContext(), "Deleted from " + mWorkoutNameDisplay, Toast.LENGTH_LONG).show();
        getCurrentExercisesInWorkout();
    }
});

What I have done for now is put it inside OnItemClick which takes an int - position as an argument.

public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

Which works and passes the position when clicked on - only problem of course is I have to click first then swipe left. How do I pass/store the position I have swiped on to pass to onSwipeLeft?

Thanks heaps!

Adam First

I've done some research and looked at some other examples on StackOverflow.

I've implemented a Swipe detector class instead:

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 // when no action was detected
}

private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;

public boolean swipeDetected() {
    return mSwipeDetected != Action.None;
}

public Action getAction() {
    return mSwipeDetected;
}

@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_UP:
            upX = event.getX();
            upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            // horizontal swipe detection
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                    Log.i(logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
                    return false;
                }
                if (deltaX > 0) {
                    Log.i(logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
                    return false;
                }
            } else if (Math.abs(deltaY) > MIN_DISTANCE) { // vertical swipe
                // detection
                // top or down
                if (deltaY < 0) {
                    Log.i(logTag, "Swipe Top to Bottom");
                    mSwipeDetected = Action.TB;
                    return false;
                }
                if (deltaY > 0) {
                    Log.i(logTag, "Swipe Bottom to Top");
                    mSwipeDetected = Action.BT;
                    return false;
                }
            }
            return false;
    }
    return false;
}
}

Usage - adding to the desired class's oncreate:

//create a swipe detector for items in the list
    final SwipeDetector swipeDetector = new SwipeDetector();
    //add a touch listener for the list view
    mListView.setOnTouchListener(swipeDetector);
    //also add a click listener and use it to get position in list
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Exercises exercises = new Exercises();
            if (swipeDetector.swipeDetected()){
                    //get the object's position in the list
                    WorkoutExercises workoutExercises = mWorkoutExercisesAdapter.getItem(position);
                    //delete the object from DB
                    workoutExercises.deleteInBackground();
                    //notify user of the removal
                    Toast.makeText(getApplicationContext(), "Deleted from " + mWorkoutNameDisplay, Toast.LENGTH_LONG).show();
                    //update the view without the removed object
                    getCurrentExercisesInWorkout();
            }
            else {
                    //Item click
            }
        }
    });

Decided i'd do swipe left or right to delete instead of just left, but if I wanted to do just one direction i'd use the swipe detector method's getAction:

if(swipeDetector.getAction()== SwipeDetector.Action.RL)
{

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章