android - SwipeListener on ListView and ClickListener on ListView's items -


this old question. time have provided code well.

i have listview different types of rows. row may contain text, image, video or else. if click on imageview (inside row) go activity show image in full screen, if click on video (inside row) go activity play video.

i have implemented right left swipe listener on listview. if start listview swipe empty space in listview, swipe works (first , second row in below image). if start listview swipe listview row's item, swipe doesn't work (third , fourth row in below image). if remove click events imageview , video swipe works if start swipe listview row's item i.e. in case swipe works on whole listview, no matter on row swipe.

enter image description here

how can rid on problem? think can achieved if disable click events on items inside listview. how can so? there other way?

i want both swipe on listview , click on listview's item.

swipedetector.java - detecting swipes on listview

public class swipedetector implements view.ontouchlistener {  private swipelistener swipelistener; private listview mlistview; private int hundred; private boolean motioninterceptdisallowed = false;  public static enum action {     lr, // left right     rl, // right left     tb, // top bottom     bt, // bottom top     none // action not found }  private static final int horizontal_min_distance = 30; // minimum // distance // horizontal swipe private static final int vertical_min_distance = 80; // minimum distance // vertical // swipe private float downx, downy, upx, upy; // coordinates private action mswipedetected = action.none; // last action  public swipedetector(context context, listview listview) {     hundred = (int) context.getresources().getdimension(r.dimen.hundred);     mlistview = listview; }  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 click processed         }         case motionevent.action_move:         {             upx = event.getx();             upy = event.gety();              float deltax = downx - upx;             float deltay = downy - upy;              float absx = math.abs(deltax);             float absy = math.abs(deltay);              if((absx >= (3 * absy)) && absx > horizontal_min_distance && mlistview != null && !motioninterceptdisallowed) {                 mlistview.requestdisallowintercepttouchevent(true);                 motioninterceptdisallowed = true;             }              if((absx >= (3 * absy)) && absx <= hundred) {                 if (deltax > 0) {                     mswipedetected = action.rl;                     swipelistener.onswipe(motionevent.action_move, action.rl, absx);                 }             }               return false;         }         case motionevent.action_up:             swipelistener.onswipe(motionevent.action_up, action.bt, 0);             if (mlistview != null) {                 mlistview.requestdisallowintercepttouchevent(false);                 motioninterceptdisallowed = false;             }             return false;         case motionevent.action_cancel:             return true;     }     return false; }  /**  * set chat send listener  * @param listener  */ public void setswipelistener(swipelistener listener) {     swipelistener = listener; }  public interface swipelistener {     void onswipe(int event, action action, float x); }  } 

this how setting swipedetector on listview

final swipedetector swipedetector = new swipedetector(swipeactivity.this, mlistview);     swipedetector.setswipelistener(mswipelistener);     mlistview.setontouchlistener(swipedetector); 

my activity implements swipedetector.swipelistener

below overridden onswipe() method

@override public void onswipe(int event, swipedetector.action action, float x) {     switch (event) {             case motionevent.action_move:                 system.out.println("list move");                 break;             case motionevent.action_up:                 system.out.println("list up");                 break;         } } 

in adapter setting onclicklistener() on textview, imageview , videoview

holder.mtextview.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             system.out.println("item textview click");         }     });  holder.mimageview.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             system.out.println("item imageview click");         }     });  holder.mvideoview.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             system.out.println("item videoview click");         }     }); 

when start swipe empty area of listview, listview's swipe listener captures it. however, when start swipe textview/imageview/videoview view's onclicklistener fired instead of parent's (listview's) ontouchlistener().

how can implement same? want when click on listview's item item's onclick should fired , when swipe on listview's item listview's onswipe should fired.

thats not possible both onclicklistener listitem , swipelistener list,because gets ambiguity between view consider on touch

you use onswipetouchlistener implements ontouchlistener

onswipetouchlistener.java

import android.view.gesturedetector; import android.view.gesturedetector.simpleongesturelistener; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener;  public class onswipetouchlistener implements ontouchlistener {      private final gesturedetector gesturedetector;      public onswipetouchlistener (context ctx){         gesturedetector = new gesturedetector(ctx, new gesturelistener());     }      private final class gesturelistener extends simpleongesturelistener {          private static final int swipe_threshold = 100;         private static final int swipe_velocity_threshold = 100;          @override         public boolean ondown(motionevent e) {             return true;         }          @override         public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {             boolean result = false;             try {                 float diffy = e2.gety() - e1.gety();                 float diffx = e2.getx() - e1.getx();                 if (math.abs(diffx) > math.abs(diffy)) {                     if (math.abs(diffx) > swipe_threshold && math.abs(velocityx) > swipe_velocity_threshold) {                         if (diffx > 0) {                             onswiperight();                         } else {                             onswipeleft();                         }                     }                     result = true;                 }                  else if (math.abs(diffy) > swipe_threshold && math.abs(velocityy) > swipe_velocity_threshold) {                         if (diffy > 0) {                             onswipebottom();                         } else {                             onswipetop();                         }                     }                     result = true;              } catch (exception exception) {                 exception.printstacktrace();             }             return result;         }     }      public void onswiperight() {     }      public void onswipeleft() {     }      public void onswipetop() {     }      public void onswipebottom() {     } } 

and use onswipetouchlistener on listitem

  listitem.setontouchlistener(new onswipetouchlistener() {          public void onswipeleft() {             //stuff list view left swipe              toast.maketext(myactivity.this, "left", toast.length_short).show();         }           public boolean ontouch(view v, motionevent event) {             //stuff on list item click             return gesturedetector.ontouchevent(event);         }     });  }); //to click events  

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -