The ListView and other widgets that can trigger click events do not work together because the ItemClick event of the ListView cannot be triggered and is masked by the Click event of other widgets after the addition of other widgets.
- First, let's explain that each row in the ListView includes the following three items:
A ImageView, a textview, a ImageButton, in turn. The following is the layout of the content, divided into two parts:
Res/layout/lvitem.xml because the listactivity is inherited, the ListView ID is set to "@id/android:list" is required
Attention:
In the <RelativeLayout>
Android:descendantfocusability= "Blocksdescendants"
and <ImageButton>
Android:focusable= "false"
The setting of these two items is critical, and if not set, will cause the ItemClick event of the ListView to fail to fire, which is masked by the ImageButton click event.
Public classLvwithbuttonextextendslistactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //ListView in the associated layoutListView Vnclistview =(ListView) Findviewbyid (Android. R.id.list); //generating dynamic arrays, adding dataarraylist(); for(inti=0;i<10;i++) {HashMap<string, object> map =NewHashmap<string, object>(); Map.put ("Itemimage", R.drawable.firefox);//ID of the image resourceMap.put ("Itemwinname", "Window ID" +i); Map.put ("Itemclosewin", Android. R.drawable.ic_menu_close_clear_cancel); Remotewindowitem.add (map); } //build the adapter's item and the element corresponding to the dynamic arrayLvbuttonadapter Listitemadapter =NewLvbuttonadapter ( This, Remotewindowitem,//Data SourceR.layout.lvitem,//XML implementation of ListItem//the children of the dynamic array corresponding to the Imageitem NewString[] {"Itemimage", "Itemwinname", "Itemclosewin"}, //imageitem XML file inside of a imageview, two TextView ID New int[] {r.id.itemimage,r.id.itemwinname,r.id.itemclosewin}); Vnclistview.setadapter (Listitemadapter); } @Overrideprotected voidOnlistitemclick (ListView L, View V,intPositionLongID) { //TODO auto-generated Method Stub Super. Onlistitemclick (l, V, position, id); L.getitematposition (position); }}
To respond to a button's Click event, first record the position of the button and then set the Clicklistener for the button. Next, let's look at the implementation of Lvbuttonadapter
In the re-implemented GetView () interface, I used the Lvbuttonlistener listener class, which, in the constructor, records the line number so that it can be positioned exactly where the button is located in the OnClick interface, and the corresponding row is processed.
Public classLvbuttonadapterextendsBaseadapter {Private classButtonviewholder {ImageView appIcon; TextView AppName; ImageButton Buttonclose; } PrivateArraylistmapplist; PrivateLayoutinflater Minflater; PrivateContext Mcontext; Privatestring[] keystring; Private int[] valueviewid; PrivateButtonviewholder Holder; PublicLvbuttonadapter (Context C, arraylistintresource, string[] from,int[] to) {Mapplist=applist; Mcontext=C; Minflater=(Layoutinflater) Mcontext.getsystemservice (Context.layout_inflater_service); KeyString=NewString[from.length]; Valueviewid=New int[To.length]; System.arraycopy (From,0, keystring, 0, from.length); System.arraycopy (To,0, Valueviewid, 0, to.length); } @Override Public intGetCount () {returnmapplist.size (); } @Override PublicObject GetItem (intposition) { returnMapplist.get (position); } @Override Public LongGetitemid (intposition) { returnposition; } Public voidRemoveItem (intposition) {mapplist.remove (position); This. notifydatasetchanged (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(Convertview! =NULL) {Holder=(Buttonviewholder) Convertview.gettag (); } Else{Convertview= Minflater.inflate (R.layout.lvitem,NULL); Holder=NewButtonviewholder (); Holder.appicon= (ImageView) Convertview.findviewbyid (valueviewid[0]); Holder.appname= (TextView) Convertview.findviewbyid (valueviewid[1]); Holder.buttonclose= (ImageButton) Convertview.findviewbyid (valueviewid[2]); Convertview.settag (holder); } HashMap<string, object> appInfo =Mapplist.get (position); if(AppInfo! =NULL) {String aname= (String) appinfo.get (keystring[1]); intMid = (Integer) appinfo.get (keystring[0]); intBid = (Integer) appinfo.get (keystring[2]); Holder.appName.setText (Aname); Holder.appIcon.setImageDrawable (Holder.appIcon.getResources (). Getdrawable (mid)); Holder.buttonClose.setImageDrawable (Holder.buttonClose.getResources (). getdrawable (Bid)); Holder.buttonClose.setOnClickListener (NewLvbuttonlistener (position)); } returnConvertview; } classLvbuttonlistenerImplementsOnclicklistener {Private intposition; Lvbuttonlistener (intPOS) {Position=POS; } @Override Public voidOnClick (View v) {intVid=V.getid (); if(vid = =holder.buttonClose.getId ()) RemoveItem (position); } }}
Click on the right button the row will be deleted the following is run: