Directly on the code put, the main is to re-give the item measure height, directly on the code to put
Import Java.util.arraylist;import android.app.activity;import Android.os.bundle;import android.view.LayoutInflater ; Import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.arrayadapter;import Android.widget.baseadapter;import Android.widget.button;import Android.widget.listview;import Android.widget.relativelayout;import Android.widget.textview;public Class Mainactivity extends Activity {private arraylist<mydata> myList = new arraylist<mydata> ();p rivate myadapter Myadapter;private layoutinflater inflater = null;private int oldpostion =-1; @Overrideprotected void OnCreate (Bundle save Dinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); final ListView list = ( ListView) Findviewbyid (r.id.draggable_list); MyData data = new MyData ();d ata.name = "one"; Mylist.add (data);d ata = new MyData ();d ata.name = "", Mylist.add (data);d ATA = new MyData ();d ata.name = "", Mylist.add (data);d ATA = new My Data ();d ata.name = "Mylist.add" (data);d ATA = new MyData ();d ata.name = "", Mylist.add (data);d ATA = new MyData (); Data.name = "Mylist.add" (data);d ATA = new MyData ();d ata.name = "n"; mylist.add (data); inflater = Layoutinflater.from ( this); TextView Header = (TextView) inflater.inflate (r.layout.header, NULL); TextView footer = (TextView) inflater.inflate (r.layout.footer, null);//list.addfooterview (footer);// List.addheaderview (header); myadapter = new Myadapter (); List.setadapter (Myadapter); List.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, view view, int position, long id) {M Ydata data = mylist.get (position), if (oldpostion = = position) {if (Data.expand) {oldpostion =-1;} Data.expand =!data.expand;} Else{oldpostion = Position;data.expand = true;} int totalheight = 0;for (int i=0;i<myadapter.getcount (); i++) {View ViewItem = Myadapter.getview (i, NULL, list);//This is very important, the expanded item of the Measureheight than the other big viewitem.measure (0, 0); Totalheight + = Viewitem.getmeasuredheight ();} Viewgroup.layoutparams params = List.getlayoutparams ();p arams.height = totalheight+ (List.getdividerheight () * ( List.getcount ()-1); List.setlayoutparams (params); myadapter.notifydatasetchanged ();}); Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {}}); Private class Myadapter extends baseadapter{@Overridepublic int GetCount () {return mylist.size ();} @Overridepublic Object getItem (int position) {return mylist.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {MyTag tag = new MyTag (); MyData data = mylist.get (position), if (Convertview = = null) {Convertview = inflater.inflate (r.layout.item_layout, NULL); Tag.item1 = (TextView) Convertview.findviewbyid (R. id.item1); tag.item2 = (relativelayout) Convertview.findviewbyid (R.ID.ITEM2); Convertview.settag (tag);} Else{tag = (MyTag) Convertview.gettag ();} if (Data.expand) {tag.item2.setVisibility (view.visible);} Else{tag.item2.setvisibility (View.gone);} Tag.item1.setText (data.name); return convertview;}} Private class Mytag{private TextView item1;private relativelayout item2;} Private class Mydata{boolean expand; String name;}}
Layout:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:minHe ight= "50DP" > <textview android:id= "@+id/item1" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_margintop= "10DP" android:layout_centerhorizontal= "true" a ndroid:gravity= "center" android:text= "Header" android:textcolor= "#000000" android:textsize= "20SP" ; </TextView> <relativelayout android:id= "@+id/item2" android:layout_below= "@id/item1" Android Oid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:minheight= "35DP" Android : background= "#fffeee" android:visibility= "Gone" > <textview android:layout_width= "Wrap_cont Ent "android:layout_height=" WRap_content "android:layout_centerinparent=" true "android:gravity=" center "android:text=" Expand "android:textcolor=" #000000 "android:textsize=" 16sp "> </TextView> </re Lativelayout></relativelayout>
:
Code: http://download.csdn.net/detail/baidu_nod/7765251