java - Is there a way to summarize multiple OnClickListener into one function in AndroidStudio? -


i got multiple onclicklistener 8 imageviews same logic behind. onclick variable-details shown. tried summarize them in method , tried add them in oncreate method loop. did not work. have 8 listeners , 8 addlistener @ oncreatemethod.

is there more elegant way?

 private void addlistenercal1arrow() {         ivcalarrow1.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 if (!cal1clicked) {                     cal1clicked = true;                     ivcalarrow1.setimageresource(r.drawable.arrow_symbol_up);                     tvdescription1.setvisibility(view.visible);                 } else {                     cal1clicked = false;                     ivcalarrow1.setimageresource(r.drawable.arrow_symbol_down);                     tvdescription1.setvisibility(view.gone);                 }             }         });     } 

more explanation: got experiment fragment, can add 8 variables max. each variable has several textviews , imageview, holds further information variable. when imageview clicked shall show information. got container class holding widgets of variable, textviews , imageview , description shall displayed when clicked

there 2 level summrize code

1- use 1 onclick() imageviews: involves

1.a implementing onclicklistener , not using anonymous inner class make activity or fragment implements onclicklistener , override onclick()

public class myactivity extends activity implements onclicklistener {     //class implementation       @override     public void onclick(view view){     } } 

use this onclicklister method setonclicklistener():

ivcalarrow1.setonclicklistener(this);//this here refers myactivity ivcalarrow2.setonclicklistener(this);//this here refers myactivity //and on ... 

b. recognize click source (which imageview) generated action) need compare view id 8 imageviews id , execute proper code based on that:

@override public void onclick(view view){     if(view.getid() == ivcalarrow1.getid()){         //do needed on ivcalarrow1     }else if(view.getid() == ivcalarrow2.getid()){         //do needed on ivcalarrow2     }     //and on ... 3 4 5 6 7 8 } 

2- make onclick() general handle click properly: involves using arrays instead of single variables named 1 2 3, cal1clicked cal2clicked ... or tvdescription1, tvdescription2 ...

this can done in several ways, complex understand or maintain, try make clear

you might need map imageview.getid key , value based on need

for example, boolean variables calxclicked may can use hashmap, key identifier calx , value boolean clicked status understanding identifier cal1clicked imageview ivcalarrow1 so:

declare class-scope

hashmap<int, boolean> calclickedstatus = new hashmap(); 

an @ oncreate() add this:

//assuming boolean values false on first create of activity calclickedstatus.put(ivcalarrow1.getid,false); calclickedstatus.put(ivcalarrow2.getid,false); calclickedstatus.put(ivcalarrow3.getid,false); // , on 

now @ onclick() use view.getid key lookup other data needed no need find source of click, because using key (view.getid)

@override public void onclick(view view){     if (!calclickedstatus.get(view.getid())) {         calclickedstatus.put(view.getid(), true);          //the view here clicked imageview, cast , use it, replace         //ivcalarrow1.setimageresource(r.drawable.arrow_symbol_up);         //with         ((imageview)view).setimageresource(r.drawable.arrow_symbol_up);          //now this, may want use array of textview hold tvdescription1, tvdescription2 ...         //and make map link each tvdescriptionx index of licked image         tvdescription1.setvisibility(view.visible);     } else {         //do same changes here         calclickedstatus.put(view.getid(), false);         ivcalarrow1.setimageresource(r.drawable.arrow_symbol_down);         tvdescription1.setvisibility(view.gone);     } } 

as mentioned earlier complex , might hard explain , done in may ways, guide on concept , rest you


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -