I see the Internet this week on the dynamic increase the ListView each item layout, today time to write a, convenient for their future use, this effect is very good, using the adapter notifydatasetchanged () method, When you click on each item, the notification adapter update GetView, the system is notified that the corresponding loading of the layout, the dynamic loading of the item layout effect. Hope to give you a little enlightenment, there are problems or want to code can leave a message, welcome message to talk about the ListView some knowledge, in order to common progress, reproduced please indicate the source:
http://blog.csdn.net/wdaming1986/article/details/6787455
program to open the interface, default check Beibei item, click on the effect of Jingjing interface,
Click the effect of the post-Huanhuan interface: Click on the effect of the post-yingying interface:
Click on the effect of the NI after interface:
In the code, the truth has to look at the code:
In the Listviewrundemo project,
Com.cn.daming the code in the Mainactivity.java class in the package:
Packagecom.cn.daming; Importandroid.app.ListActivity; ImportAndroid.content.Context; ImportAndroid.os.Bundle; Importandroid.view.Gravity; ImportAndroid.view.LayoutInflater; ImportAndroid.view.View; ImportAndroid.view.ViewGroup; ImportAndroid.widget.AdapterView; ImportAndroid.widget.AdapterView.OnItemClickListener; ImportAndroid.widget.BaseAdapter; ImportAndroid.widget.ImageView; Importandroid.widget.LinearLayout; ImportAndroid.widget.TextView; Public classMainactivityextendslistactivity {PrivateListaddlayoutadapter Madapter; @SuppressWarnings ("Unused") PrivateLayoutinflater Linflater; int[] image ={r.drawable.s_beibei, r.drawable.s_jingjing, R.drawable.s_huanhuan, R.drawable.s_yingying, R.drawable.s_nini}; String[] Show_name= { Babe, Jingjing, "Huan Huan", "Welcome", "Nicole." }; Private intID = 0; @SuppressWarnings ("Unused") Private intLast = 0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Madapter=NewListaddlayoutadapter ( This); This. Setlistadapter (Madapter); This. Getlistview (). Setonitemclicklistener (NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> arg0, View arg1,intposition,LongArg3) {ID=position; Madapter.notifydatasetchanged (); Last=position; } }); } Public classListaddlayoutadapterextendsBaseadapter {Privatecontext Context; Privatemainactivity activity; PublicListaddlayoutadapter (Context context) { This. Context =context; This. Activity =(mainactivity) context; Linflater=Activity.getlayoutinflater (); } Public intGetCount () {returnimage.length; } PublicObject GetItem (intarg0) { return NULL; } Public LongGetitemid (intposition) { returnposition; } PublicView GetView (intposition, View arg1, ViewGroup arg2) {LinearLayout layout=Newlinearlayout (context); Layout.setorientation (linearlayout.vertical); Layout.setpadding (0, 8, 0, 8); Layout.addview (Addtitleview (position)); if(id==position) {Layout.addview (Addcustomview (position)); } returnlayout; } PublicView Addtitleview (inti) {LinearLayout layout=Newlinearlayout (context); Layout.setorientation (linearlayout.horizontal); ImageView IV=NewImageView (context); Iv.setimageresource (Image[i]); Layout.addview (iv,NewLinearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); TextView TV=NewTextView (context); Tv.settext (Show_name[i]); Tv.settextsize (18f); Layout.addview (TV,NewLinearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); Layout.setgravity (Gravity.center); returnlayout; } PublicView Addcustomview (inti) {View View=NewView (context); Switch(i) { Case0: ImageView iv1=NewImageView (context); Iv1.setimageresource (R.drawable.beibei); View=Iv1; Break; Case1: ImageView iv2=NewImageView (context); Iv2.setimageresource (r.drawable.jingjing); View=Iv2; Break; Case2: ImageView iv3=NewImageView (context); Iv3.setimageresource (R.drawable.huanhuan); View=Iv3; Break; Case3: ImageView iv4=NewImageView (context); Iv4.setimageresource (r.drawable.yingying); View=iv4; Break; Case4: ImageView iv5=NewImageView (context); Iv5.setimageresource (R.drawable.nini); View=iv5; Break; } returnview; } } }
Note: It is also possible to load layout files dynamically:
It is also possible to write this in the Addcustomview () method:
[Java]View Plaincopyprint?
<style= "FONT-SIZE:16PX;" > Case 1: view = linflater.inflate (R.layout.main, null); break; </ span >
Project pictures did not upload, I hope you can find some pictures on their own: