android - onTouchListener detecting wrong dynamically created cardview -


i have learnt how create cardview dynamically. i'm using button press create cardview , have set ontouchlistener it. when i'm creating 1 card ontouchlistener working i'm creating 1 more card ontouchlistener isn't working expected. when have 2 cards , try move 1st card it's moving 2nd 1 instead of 1st.

public class mainactivity extends appcompatactivity     implements navigationview.onnavigationitemselectedlistener {  context context; imageview imageview; relativelayout relativelayout; static int pos=0;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      context=this;     relativelayout=(relativelayout)findviewbyid(r.id.rl_layout);      drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     actionbardrawertoggle toggle = new actionbardrawertoggle(             this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close);     drawer.setdrawerlistener(toggle);     toggle.syncstate();      navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);     navigationview.setnavigationitemselectedlistener(this); }    @override public void onbackpressed() {     drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);     if (drawer.isdraweropen(gravitycompat.start)) {         drawer.closedrawer(gravitycompat.start);     } else {         super.onbackpressed();     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {          createcardviewwithoutimageview();          return true;     }      return super.onoptionsitemselected(item); }  @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) {     return true; }  public void createcardviewwithoutimageview(){      final cardview cardview=new cardview(context);      cardview.setid(++pos);     cardview.setlayoutparams((new cardview.layoutparams(600, 600)));     cardview.setcontentpadding(25,25,25,25);     cardview.setcardbackgroundcolor(color.magenta);      relativelayout.addview(cardview);  }  private final view.ontouchlistener mlistener = new view.ontouchlistener() {     @override     public boolean ontouch(view card, motionevent event) {         // view (v) here cardview ontouchlistener set         relativelayout.layoutparams layoutparamsrl = (relativelayout.layoutparams) ((cardview) card).getlayoutparams();         float dx = 0, dy = 0, x = 0, y = 0;          switch (event.getaction()) {             case motionevent.action_down: {                 card.bringtofront();                 dx = event.getrawx() - layoutparamsrl.leftmargin;                 dy = event.getrawy() - layoutparamsrl.topmargin;             }             break;             case motionevent.action_move: {                 x = event.getrawx();                 y = event.getrawy();                 layoutparamsrl.leftmargin = (int) (x - dx);                 layoutparamsrl.topmargin = (int) (y - dy);                 card.setlayoutparams(layoutparamsrl);             }             break;             case motionevent.action_up: {              }             break;         }         return true;     } }; 

}

it's because cardview variable global. each time create new cardview it's replaced listener using last cardview created. should like:

// note: way, should not use capital letter name function public void createcardviewwithoutimageview(){      //cardview     final cardview cardview = new cardview(context);      //....  } 

you going have same issue layoutparamsrl should local too.

edit

    // context context; // don't need store context here in activity //    cardview cardview; // don't need global //    relativelayout.layoutparams layoutparamsrl; // don't need global     imageview imageview;     relativelayout relativelayout;     static int pos = 0; // sure needs static field?      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);  //        context=this;         relativelayout = (relativelayout) findviewbyid(r.id.rl_layout);          drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         actionbardrawertoggle toggle = new actionbardrawertoggle(                 this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close);         drawer.setdrawerlistener(toggle);         toggle.syncstate();          navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view);         navigationview.setnavigationitemselectedlistener(this);     }       @override     public void onbackpressed() {         drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);         if (drawer.isdraweropen(gravitycompat.start)) {             drawer.closedrawer(gravitycompat.start);         } else {             super.onbackpressed();         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {              createcardviewwithoutimageview();              return true;         }          return super.onoptionsitemselected(item);     }      @suppresswarnings("statementwithemptybody")     @override     public boolean onnavigationitemselected(menuitem item) {         return true;     }      public void createcardviewwithoutimageview() {          //cardview         cardview cardview = new cardview(mainactivity.this);          cardview.setid(++pos);         cardview.setlayoutparams((new cardview.layoutparams(600, 600)));         cardview.setcontentpadding(25, 25, 25, 25);         cardview.setcardbackgroundcolor(color.magenta);         cardview.setontouchlistener(mlistener);          relativelayout.addview(cardview);     }      private final view.ontouchlistener mlistener = new view.ontouchlistener() {          float dx, dy; // moved outside ontouch method in order them keep value.          @override         public boolean ontouch(view card, motionevent event) {             log.d("ontouchlistener", "don't touch me :p");              // view (v) here cardview ontouchlistener set             relativelayout.layoutparams layoutparamsrl = (relativelayout.layoutparams) ((cardview) card).getlayoutparams();             float x = 0, y = 0;              switch (event.getaction()) {                 case motionevent.action_down: {                     card.bringtofront();                     dx = event.getrawx() - layoutparamsrl.leftmargin;                     dy = event.getrawy() - layoutparamsrl.topmargin;                 }                 break;                 case motionevent.action_move: {                     x = event.getrawx();                     y = event.getrawy();                     layoutparamsrl.leftmargin = (int) (x - dx);                     layoutparamsrl.topmargin = (int) (y - dy);                     card.setlayoutparams(layoutparamsrl);                 }                 break;                 case motionevent.action_up: {                  }                 break;             }             return true;         }     }; 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -