android - onTouchListener not working with custom view -
i have custom adapter class extends base adapter. i'm trying implement ontouchlistener on view items. not working. please help.
here code segment
@override public view getview(final int position, view convertview, viewgroup parent) { view cellview=convertview; viewholder viewholder; if(cellview==null) { layoutinflater layoutinflater = (layoutinflater) context. getsystemservice(context.layout_inflater_service); cellview = layoutinflater.inflate(r.layout.grid_topics, parent, false); viewholder = new viewholder(); viewholder.icon = (imageview) cellview.findviewbyid(r.id.cellimageview); viewholder.text = (textview) cellview.findviewbyid(r.id.celltextview); cellview.settag(viewholder); } else { viewholder = (viewholder) cellview.gettag(); } viewholder.icon.setimagebitmap(list.get(position).image); viewholder.text.settext(list.get(position).text); cellview.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_up) { toast.maketext(context, list.get(position).text, toast.length_short).show(); return true; } return false; } }); return cellview; }
try follow approach when dealing baseadapter. see how implementing ontouch listener there in if statement. should work.
public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service); if (convertview == null) { convertview = minflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.txtdesc = (textview) convertview.findviewbyid(r.id.desc); holder.txttitle = (textview) convertview.findviewbyid(r.id.title); holder.imageview = (imageview) convertview.findviewbyid(r.id.icon); convertview.settag(holder); convertview.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event.getaction() == motionevent.action_up) { toast.maketext(context, list.get(position).text, toast.length_short).show(); return true; } return false; } }); } else { holder = (viewholder) convertview.gettag(); } rowitem rowitem = (rowitem) getitem(position); holder.txtdesc.settext(rowitem.getdesc()); holder.txttitle.settext(rowitem.gettitle()); holder.imageview.setimageresource(rowitem.getimageid()); return convertview; }
Comments
Post a Comment