The ListView can display several types of entry layouts, where the two layouts are shown, and other similar
This is Mainactivity:,mainactivity's layout is a ListView
Public classMainactivityextendsActivity {PrivateListView LV; PrivateList<people>lists; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (r.id.lv); Lists=NewArraylist<>(); //0 for students, 1 for teachersPeople people1 =NewPeople (0, "10");//Type, money people people2=NewPeople (1, "100"); People people3=NewPeople (1, "100"); People people4=NewPeople (0, "10"); Lists.add (PEOPLE1); Lists.add (People2); Lists.add (PEOPLE3); Lists.add (PEOPLE4); Lv.setadapter (NewMyadapter ()); } classMyadapterextendsbaseadapter{@Override Public intGetCount () {returnlists.size (); } @Override PublicObject GetItem (inti) {returnLists.get (i); } @Override Public LongGetitemid (inti) {returni; } @Override Public intGetitemviewtype (intposition) { if(Lists.get (position). GetType () ==0) {//The type of the current JavaBean object return0;//Student Type}Else if(Lists.get (position). GetType () ==1){ return1;//Teacher Type}Else { return100; }} @Override Public intGetviewtypecount () {return2;//There are a total of two types} @Override PublicView GetView (intposition, View Convertview, ViewGroup viewgroup) { intCurrenttype = Getitemviewtype (position);//Current Type if(currenttype==0) {//Student TypeStudentviewholder Studentviewholder; if(convertview==NULL) {Studentviewholder=NewStudentviewholder (); Convertview=view.inflate (mainactivity. This, R.layout.item_lv_student,NULL); Studentviewholder.tv0=(TextView) Convertview.findviewbyid (R.id.num_money_stu); Convertview.settag (Studentviewholder); }Else{Studentviewholder=(Studentviewholder) Convertview.gettag (); } //Data PopulationStudentViewHolder.tv0.setText (lists.get (position). Getmoney ()); }Else if(currenttype==1) {//Teacher TypeTeacherviewholder Teacherviewholder; if(convertview==NULL) {Teacherviewholder=NewTeacherviewholder (); Convertview=view.inflate (mainactivity. This, R.layout.item_lv_teacher,NULL); TEACHERVIEWHOLDER.TV1=(TextView) Convertview.findviewbyid (R.id.num_money_teacher); Convertview.settag (Teacherviewholder); }Else{Teacherviewholder=(Teacherviewholder) Convertview.gettag (); } //Data PopulationTeacherViewHolder.tv1.setText (lists.get (position). Getmoney ()); } returnConvertview; } } /**holder of the student item*/ classStudentviewholder {TextView tv0; } /**the holder of the teacher's item*/ classTeacherviewholder {TextView TV1; }}
JavaBean object:
/**the JSON object to imitate, the JSON object must take the type, distinguishes student and teacher * Created by Xhj on 16-1-8.*/ Public classpeople {/**type, 0 means student, 1 means teacher*/ Public inttype; PublicString money; PublicPeople (inttype, String money) { This. Type =type; This. Money =Money ; } Public intGetType () {returntype; } Public voidSetType (inttype) { This. Type =type; } PublicString Getmoney () {returnMoney ; } Public voidSetmoney (String money) { This. Money =Money ; }}
The layout of the ListView student item:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"Android:background= "#00ff00" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Student's money:"/> <TextView Android:id= "@+id/num_money_stu"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Ten"/> <Button android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Student Layout"Android:id= "@+id/button"/></linearlayout>
The layout of the ListView teacher item:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"Android:background= "#f9a5b2" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Teacher's money:"/> <TextView Android:id= "@+id/num_money_teacher"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "/></linearlayout>"
Result diagram:
ListView displays multiple types of item