How to add swipe functionality on Android CardView? -


i have single cardview contains 2 textviews , 2 imageviews. want able swipe left , right "dismiss". want swipe right send intent can done later. now, want able dismiss cardview swiping left or right. want animation of swiping.

i tried using romannurik's swipedismisstouchlistener cardview not respond swiping.

if has better solution romannurik's custom listener, please share.

here's cardview layout:

<android.support.v7.widget.cardview     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_below="@id/generate"     map:cardcornerradius="@dimen/_3sdp"     map:cardelevation="@dimen/_8sdp"     android:id="@+id/restaurantcontainer">      <linearlayout         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:orientation="vertical">          <linearlayout             android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:orientation="horizontal">              <linearlayout                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:orientation="vertical">                  <imageview                     android:id="@+id/thumbnail"                     android:layout_width="@dimen/_75sdp"                     android:layout_height="@dimen/_75sdp"                     android:layout_margin="@dimen/_5sdp" />                  <imageview                     android:id="@+id/rating"                     android:layout_width="@dimen/_75sdp"                     android:layout_height="@dimen/_15sdp"                     android:layout_margin="@dimen/_5sdp" />             </linearlayout>              <linearlayout                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 android:orientation="vertical">                  <textview                     android:id="@+id/name"                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:layout_margin="@dimen/_5sdp"                     android:gravity="top"                     android:textappearance="?android:attr/textappearancelarge" />                  <textview                     android:id="@+id/categories"                     android:layout_width="fill_parent"                     android:layout_height="fill_parent"                     android:layout_margin="@dimen/_5sdp"                     android:textappearance="?android:attr/textappearancemedium" />             </linearlayout>          </linearlayout>          <linearlayout             android:layout_width="fill_parent"             android:layout_height="fill_parent">              <com.google.android.gms.maps.mapview                 android:id="@+id/mapview"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent"                 map:camerazoom="14"                 map:litemode="true"                 map:maptype="normal" />          </linearlayout>      </linearlayout>  </android.support.v7.widget.cardview> 

how setup swipedismisstouchlistener:

relativelayout rootlayout; cardview restaurantcardview;  ...  public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {          rootlayout = (relativelayout) inflater.inflate(r.layout.fragment_main, container, false);          restaurantcardview = (cardview) rootlayout.findviewbyid(r.id.restaurantcontainer);          restaurantcardview.setontouchlistener(new swipedismisstouchlistener(restaurantcardview, null, new swipedismisstouchlistener.dismisscallbacks() {             @override             public boolean candismiss(object token) {                 log.d("chris", "candismiss() called with: " + "token = [" + token + "]");                 return true;             }              @override             public void ondismiss(view view, object token) {                 log.d("chris", "ondismiss() called with: " + "view = [" + view + "], token = [" + token + "]");             }         }));     ....      return rootlayout; 

i able see log candismiss(...) not ondismiss(...).

you'll have put inside recyclerview (and cardview item there)

then, use itemtouchhelper.simplecallback itemtouchhelper.attachtorecyclerview(recyclerview);

it give animations , in

@override public void onswiped(recyclerview.viewholder viewholder, int swipedir) { } 

you can specify particular action based on swipe direction.

see full instruction here: swipe dismiss recyclerview

also, you'll have disable vertical scroll in recyclerview:

public class unscrollablelinearlayoutmanager extends linearlayoutmanager {     public unscrollablelinearlayoutmanager(context context) {         super(context);     }      @override     public boolean canscrollvertically() {         return false;     } }  .....  recyclerview recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); recyclerview.setlayoutmanager(new unscrollablelinearlayoutmanager(this)); recyclerview.setadapter(new restaurantcardadapter()); 

otherwise - once you'll try scroll or down - you'd see recyclerview's end-of-list animations.

upd:

here's recyclerview.adapter used test:

private class restaurantcardadapter extends recyclerview.adapter {     @override     public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) {         return new restaurantviewholder(new restaurantcard(parent.getcontext()));     }      @override     public void onbindviewholder(recyclerview.viewholder holder, int position) {}      @override     public int getitemcount() {         return 1;     }      private class restaurantviewholder extends recyclerview.viewholder {         public restaurantviewholder(view itemview) {             super(itemview);         }     } } 

restaurantcard - custom view (extends cardview in our case):

public class restaurantcard extends cardview {     public restaurantcard(context context) {         super(context);         initialize(context);     }      public restaurantcard(context context, attributeset attrs) {         super(context, attrs);         initialize(context);     }      private void initialize(context context){         layoutinflater.from(context).inflate(r.layout.card, this);         // imageview imageview = (imageview)getview.findviewbyid(...);     } } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -