Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Swipe on List

Does anyone have a simple example of a ListActivity displaying Textviews in a column and when you swipe left to right you see that row in a new view? This would be to say edit the data for that row or show more detailed info on that row. Please do not reference code shogun or other sites as I have googled and have not seen this answered.

like image 415
JPM Avatar asked Dec 07 '10 04:12

JPM


People also ask

How to add Swipe refresh layout android?

To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView . Remember that SwipeRefreshLayout only supports a single ListView or GridView child. You can also use the SwipeRefreshLayout widget with a ListFragment .

How do I notify a list on Android?

Scroll down and long-press the “Settings” widget, then place it on your home screen. You'll get a list of features that the Settings shortcut can access. Tap “Notification Log.” Tap the widget and scroll through your past notifications.

What is a ListView in android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


1 Answers

I had the same problem and I didn't find my answer here.

I wanted to detect a swipe action in ListView item and mark it as swiped, while continue to support OnItemClick and OnItemLongClick.

Here is me solution:

1st The SwipeDetector class:

import android.util.Log; import android.view.MotionEvent; import android.view.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 // 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;     } } 

2nd I use the swipe detector class in the list view:

    final ListView lv = getListView();     final SwipeDetector swipeDetector = new SwipeDetector();     lv.setOnTouchListener(swipeDetector);     lv.setOnItemClickListener(new OnItemClickListener() {         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 if (swipeDetector.swipeDetected()){                     // do the onSwipe action                  } else {                     // do the onItemClick action                 }             }     });     lv.setOnItemLongClickListener(new OnItemLongClickListener() {         @Override         public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {             if (swipeDetector.swipeDetected()){                 // do the onSwipe action              } else {                 // do the onItemLongClick action             }         }     }); 

This way I can support 3 actions - swipe, click, long click and I can use the ListView item info.

ADDED LATER:

Since ListView catches a scrolling action, it is sometimes hard to swipe. To fix it, I made the following change to SwipeDetector.onTouch:

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) {                     Log.i(logTag, "Swipe Left to Right");                     mSwipeDetected = Action.LR;                     return true;                 }                 if (deltaX > 0) {                     Log.i(logTag, "Swipe Right to Left");                     mSwipeDetected = Action.RL;                     return true;                 }             } else               // vertical swipe detection             if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE) {                 // 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 true;         }     }     return false; } 
like image 73
Asaf Pinhassi Avatar answered Sep 19 '22 06:09

Asaf Pinhassi