android - How to dynamically change content in CardView -


i'm new android programming. have been trying make reminder app takes time, date , description inputs.

i want add swipe action card. initially, card shows time , date, when user swipes on card, content in card must change, should show new layout has textview containing description in it.

i have searched everywhere, of them have given removing or moving card in recyclerview.

what want is, how can dynamically change content(or layout) in card animation using swipe action?

this card layout card_view.xml

    <?xml version="1.0" encoding="utf-8"?> <linearlayout `enter code here`xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="170dp">  <android.support.v7.widget.cardview     android:layout_height="150dp"     android:layout_width="320dp"     android:id="@+id/card_view"     card_view:cardcornerradius="16dp"     card_view:cardelevation="2dp"     >       <relativelayout         android:layout_width="fill_parent"         android:layout_height="match_parent"         android:padding="10dp"         android:orientation="vertical"         android:background="@color/blue_color">          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="00:00"             android:fontfamily="sans-serif-light"             android:id="@+id/cardtime"             android:textsize="@dimen/textview_fontsize"             android:textcolor="@color/textview_color"             />          <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="description"             android:fontfamily="sans-serif-light"             android:id="@+id/description"             android:textsize="@dimen/textview_fontsize"             android:textcolor="@color/textview_color"             android:visibility="gone"             />          <textview             android:text=""             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignparenttop="true"             android:layout_toendof="@+id/cardtime"             android:layout_marginstart="12dp"             android:layout_margintop="17dp"             android:id="@+id/cardperiod"             android:textcolor="@color/textview_color"             android:fontfamily="sans-serif-light"/>          <view             android:layout_width="match_parent"             android:layout_height="1dp"             android:background="@color/textview_color"             android:layout_alignbottom="@+id/cardtime"             android:id="@+id/time_line" />           <textview             android:text="09/01/1997"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignparentbottom="true"             android:layout_alignparentstart="true"             android:layout_marginstart="20dp"             android:id="@+id/carddate"             android:textcolor="@color/textview_color"             android:fontfamily="sans-serif-light"             android:textsize="@dimen/date_fontsize"/>       </relativelayout>   </android.support.v7.widget.cardview> 

mainactivity.java

public class mainactivity extends appcompatactivity implements alarmdialog.alarmdialoglistener {  arraylist<cardgen> cardslist = new arraylist<cardgen>(); recyclerview recyclerview; cardadapter cardadapter = new cardadapter(cardslist); linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this);     @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       recyclerview = (recyclerview) findviewbyid(r.id.cardlist);     linearlayoutmanager.setorientation(linearlayoutmanager.vertical);     recyclerview.sethasfixedsize(true);     recyclerview.setlayoutmanager(linearlayoutmanager);     recyclerview.setadapter(cardadapter);         itemtouchhelper.simplecallback simpleitemtouchcallback = new itemtouchhelper.simplecallback(0, itemtouchhelper.left | itemtouchhelper.right) {          @override         public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) {             return false;         }          @override         public void onswiped(recyclerview.viewholder viewholder, int direction) {             int position = viewholder.getadapterposition();             cardadapter.cardviewholder holder = (cardadapter.cardviewholder) viewholder;              if (direction == itemtouchhelper.left){                      holder.timeview.setvisibility(view.gone);                     holder.description.setvisibility(view.visible);              } else {              }         }          @override         public void onchilddraw(canvas c, recyclerview recyclerview, recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) {               if(actionstate == itemtouchhelper.action_state_swipe){               }             super.onchilddraw(c, recyclerview, viewholder, dx, dy, actionstate, iscurrentlyactive);         }     };     itemtouchhelper itemtouchhelper = new itemtouchhelper(simpleitemtouchcallback);     itemtouchhelper.attachtorecyclerview(recyclerview);     }   public void createalarm(view view){      dialogfragment dialogfragment = new alarmdialog();     dialogfragment.show(getfragmentmanager(), "alarmdialog"); }   @override public void ondialogpositiveclick(dialogfragment dialog) {     dialog dialog1 =  dialog.getdialog();     timepicker timepicker = (timepicker) dialog1.findviewbyid(r.id.timepicker);     int hour = timepicker.gethour();     string temphour = integer.tostring(hour);     string minute = integer.tostring(timepicker.getminute());     if(minute.length()==1){minute = "0" + minute;}     toast.maketext(this, temphour, toast.length_short).show();       system.out.println("this adapter begin");      cardgen cardgen = new cardgen();     if(hour>12){temphour = integer.tostring(hour-12);}     else if(hour==0){temphour="12";}      if(temphour.length()==1){temphour = "0"+temphour;}     cardgen.hour = temphour+":"+minute;     if(hour>=12){cardgen.period="pm";}else{cardgen.period="am";}      cardgen.fragmentmanager = getfragmentmanager();     cardadapter.addlist(cardgen);      system.out.println(integer.tostring(cardslist.size()));     system.out.println("this middle");          system.out.println("this end");  }  @override public void ondialognegativeclick(dialogfragment dialog) {  } 

}

recycleradapter (cardadapter.java)

public class cardadapter extends recyclerview.adapter<cardadapter.cardviewholder>  {  private arraylist<cardgen> cardslist;  public cardadapter(arraylist<cardgen> cardslist){     this.cardslist = cardslist;   }  @override public int getitemcount() {     return cardslist.size(); }    @override public void onbindviewholder(final cardviewholder holder, int position) {      final cardgen cardgen = cardslist.get(position);     holder.timeview.settext(cardgen.hour);      holder.periodview.settext(cardgen.period);     final textview temp = holder.dateview;     holder.dateview.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             dateselection newfragment = new dateselection();             newfragment.setelements(temp);               newfragment.show(cardgen.fragmentmanager, "datepicker");         }     });    }     @override public cardviewholder oncreateviewholder(viewgroup parent, int viewtype) {     view itemview = layoutinflater.from(parent.getcontext()).inflate(r.layout.cards, parent, false);     return new cardviewholder(itemview); }   public void addlist(cardgen card){     cardslist.add(card);     notifyiteminserted(cardslist.size()); }  public void removelist(int position){     cardslist.remove(position);     notifyitemremoved(position);     notifyitemrangechanged(position, cardslist.size()); }  public class cardviewholder extends recyclerview.viewholder{      textview timeview,periodview,dateview,description;     button transfer;      public cardviewholder(view v){         super(v);          timeview = (textview) v.findviewbyid(r.id.cardtime);         periodview = (textview) v.findviewbyid(r.id.cardperiod);         dateview = (textview) v.findviewbyid(r.id.carddate);         description = (textview) v.findviewbyid(r.id.description);         transfer = (button) v.findviewbyid(r.id.transfer);      } } 

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.bharath.organiserexample.mainactivity">  <android.support.v7.widget.recyclerview     android:layout_width="match_parent"     android:layout_height="match_parent"     android:id="@+id/cardlist">   </android.support.v7.widget.recyclerview>  <imagebutton     android:layout_width="wrap_content"     android:layout_height="62dp"     android:background="@null"     android:src="@mipmap/add_button"     android:layout_gravity="bottom|right"     android:onclick="createalarm" />   </framelayout> 

there couple options. easiest have cardview contain description set visibility gone initially, when swipe event happens invert initial state gone , description visible. create helper method this. far capturing swipe gesture, take @ answer.

swipe dismiss recyclerview

you need using recyclerview should work if put invert view logic in onswiped method

edit cardview, use viewholder recyclerview

@override     public void onswiped(recyclerview.viewholder viewholder, int swipedir) {         mycustomviewholder vh = (mycustomviewholder) viewholder;         if(vh.isdescriptionshown()){             vh.showdetails()         }else {             vh.showdescription()         }     } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -