javaandroidtextviewontouchlisteneronfling

getView of element that you're swiping (onFling) for android?


I'm making a list with a linearLayout and adding TextViews depending on how many items I got in the database. Problem is that I want to be able to delete an item when I swipe left on the list. I was just wondering how to get the element (view) that you're swiping? Here's the code I got for adding a TextView.

for(int i = 0; i < nrOfTodos; i++) {
        TextView v = new TextView(this);
        String title = todos.get(i).getTitle();
        System.out.println(title);
        v.setText(title);
        v.setHeight(listItemSize);
        v.setAllCaps(true);
        v.setTextColor(Color.parseColor("#FFD9A7"));
        v.setTextSize(listItemSize/6);
        v.setHorizontallyScrolling(false);
        v.setMaxLines(1);
        v.setEllipsize(TruncateAt.END);
        v.setPadding(30, 50, 0, 0);
        v.setId(i);
        todoViews.add(v);
        v.setOnTouchListener(new OnSwipeTouchListener(this) {
            @Override
            public void onSwipeLeft() {
                /*
                 * 
                 * 
                 * TODO!!! NEXT PART OF MY PLAN TO WORLD DOMINATION!
                 * 
                 * 
                 */
            }
        });
        if(i%2 == 1) {
            v.setBackgroundColor(Color.parseColor("#11FFFFFF"));
        }
        ll.addView(v, i);                       
    }

and I also have this

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new GestureListener());
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_DISTANCE_THRESHOLD = 50;
    private static final int SWIPE_VELOCITY_THRESHOLD = 10;

    @Override
    public boolean onDown(MotionEvent e) {
        //always return true since all gestures always begin with onDown and <br>
        //if this returns false, the framework won't try to pick up onFling for example.
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
            if (distanceX > 0)
                onSwipeRight();
            else
                onSwipeLeft();
            return true;
        }
        return false;
    }
}
}

but I can't figure out how to actually get the view, get the titletext and remove that from the database (I can remove from the database, but I can't get what item was swiped ^^). Any ideas would be greatly appreciated.


Solution

  • You may refer to this sample application Swipe_To_Delete You may use ListView to list your data items which are got from the db. Then modify the getSwipeItem() method in the MainActivity of the example, as follows.

    @Override
    public void getSwipeItem(boolean isRight, int position) {
        [List_Of_Items].remove(position);
        [Your_Adapter].notifyDataSetChanged();
    }