-2

I want to move any normal view in my app along with swipe.

I am able to detect swipe using OnTouchListener in onTouch method. But am not able to move the view along with the swipe.

I want to move the view along with the swipe.

I think so that something i need to implement inside onTouch method to move the view or item along with swipe, but what i don't know.

Any help will be highly appreciated.

kanudo
  • 2,119
  • 1
  • 17
  • 33
  • see `ItemTouchHelper` – pskink Aug 06 '17 at 06:37
  • Possible duplicate of [Swipe to Dismiss for RecyclerView](https://stackoverflow.com/questions/27293960/swipe-to-dismiss-for-recyclerview) – azizbekian Aug 06 '17 at 06:56
  • @azizbekian and @pskink it don't want to swipe it only for `RecyclerView` but also for `ListView` and any normal View. – kanudo Aug 06 '17 at 07:04
  • 2
    so change your title to: "How to Simply Swipe items of ListView **and** RecyclerView **and** any normal View without third party library" – pskink Aug 06 '17 at 07:12
  • @pskink I made the changes to the question and made it more clear. – kanudo Aug 06 '17 at 09:53

1 Answers1

1

You just need to use view.setTranslationX(diffX) for X coordinate and view.setTranslationY(diffY) for Y coordinate while its MotionEvent.ACTION_MOVE under onTouch() method.

view.setOnTouchListener(new View.OnTouchListener() {

    float downX, downY, nowX, nowY, diffX, diffY;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                downX = event.getRawX();
                downY = event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:

                nowX = event.getRawX();
                nowY = event.getRawY();

                diffX = nowX - downX;
                diffY = nowY - downY;

                v.setTranslationX(diffX);
                v.setTranslationY(diffY);

                break;

            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }
});
TrackingToon
  • 93
  • 1
  • 8