onclicklistener - android onclick for several custom Views issue -
onclicklistener - android onclick for several custom Views issue -
good morning, i'm drawing 9 customviews in 1 ralativelayout.
i want assign on click listener each view.
the issue when click on 1 of these view, reference lastly drawed view, if clicked on first one.
here code:
public class mainactivity extends activity { mysurfaceview view; relativelayout layout; list<customcircles> circlesarr; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); layout = (relativelayout) findviewbyid(r.id.relativelayout); viewtreeobserver vto = layout.getviewtreeobserver(); vto.addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { layout.getviewtreeobserver().removeongloballayoutlistener(this); int width = layout.getmeasuredwidth(); int height = layout.getmeasuredheight(); int radius = calculatecircleradius(height); calculatecirclesposition(radius); } }); } int circlesperrow = 3; int rows = 3; private void calculatecirclesposition(int radius) { int index = 0; circlesarr = new arraylist<customcircles>(); (int = 0; < rows; ++i) { int y = radius + ((radius * 2) * i); relativelayout.layoutparams params = null; if(i == 0) { params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); params.addrule(relativelayout.align_parent_top); } else if(i == 1) { params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); params.addrule(relativelayout.below, circlesarr.get(0).id); } else if(i == 2) { params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); params.addrule(relativelayout.below, circlesarr.get(3).id); } (int j = 0; j < circlesperrow; ++j) { int x = radius + ((radius * 2) * j); punto centro = new punto(x, y); cerchio cerchio = new cerchio(centro, radius); cerchio.indexinarray = index; circleshandler.get().getcirclelist().add(cerchio); customcircles circle = new customcircles(this, centro, radius, index++); circle.settag("circle" + index); log.v("jajaja", "setted index "+ index); circlesarr.add(circle); if(j == 0) { params.addrule(relativelayout.align_parent_left); } else { params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); params.addrule(relativelayout.right_of, circlesarr.get(j-1).getid()); } layout.addview(circle, params); } } } private int calculatecircleradius(int height) { homecoming (height / 3) / 2; } }
customcircleview class
public class customcircles extends view implements view.onclicklistener { punto centro; paint paint; int radius; int id; public customcircles(context context, punto centro, int radius, int id) { this(context); this.centro = centro; this.radius = radius; this.id = id; //setid(id); } public customcircles(context context) { super(context); init(); } public customcircles(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public customcircles(context context, attributeset attrs) { super(context, attrs); } private void init() { this.setonclicklistener(this); paint = new paint(); paint.setcolor(color.parsecolor("#000000")); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawcircle(centro.x, centro.y, radius, paint); } @override public void onclick(view v) { log.v("jajaja", "clicked " + this.gettag()); } }
thank time
you putting circles in relativelayout without options place, thats why can have getleft()==0
, gettop()==0
.
for circles phone call view
method setid(index)
, layoutparams
need add together rules:
params.addrule(relativelayout.right_of, prevcircle.getid()); params.addrule(relativelayout.align_top, prevcircle.getid());
or
params.addrule(relativelayout.below, circleabove.getid()); params.addrule(relativelayout.align_left, circleabove.getid());
for new circle in line.
android onclicklistener android-custom-view
Comments
Post a Comment