An example of this article is about the improved method of ListView packet layout implemented by Android. Share to everyone for your reference, specific as follows:
Because it is reproduced on the Internet an article, here is not much nonsense, first look at the final effect of the picture:
The main code to implement the ListView layout is then:
1, the program main interface Separatelistview.java
Package Whu.iss.wuxianglong;
Import java.util.ArrayList;
Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.TextView;
public class Separatelistview extends activity {ListView ListView;
Myadapter Myadapter;
Public list<string> Listtag = new arraylist<string> (); /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
ListView = (ListView) Findviewbyid (r.id.list); Myadapter = new Myadapter (this, Android.
R.layout.simple_expandable_list_item_1, GetData ());
Listview.setadapter (Myadapter); Private List<string> GetData () {list<string> data = new ARRAYLIST<STRIng> ();
int i = 0;
Data.add ("A");
Listtag.add ("A");
Data.add ("AA test Data" + (i++));
Data.add ("A Trial data" + (i++));
Data.add ("AA test Data" + (i++));
Listtag.add ("B");
Data.add ("B");
Data.add ("BB test Data" + (i++));
Data.add ("B Test Data" + (i++));
Data.add ("B Test Data" + (i++));
Data.add ("B Test Data" + (i++));
Listtag.add ("C");
Data.add ("C");
Data.add ("C Test Data" + (i++));
Data.add ("C Test Data" + (i++));
Listtag.add ("D");
Data.add ("D");
Data.add ("D Test Data" + (i++));
Data.add ("D Test Data" + (i++));
Data.add ("D Test Data" + (i++));
Listtag.add ("E");
Data.add ("E");
Data.add ("E Test Data" + (i++));
Data.add ("E Test Data" + (i++));
Data.add ("E Test Data" + (i++));
Listtag.add ("F");
Data.add ("F");
Data.add ("F Test Data" + (i++));
return data;
Class Myadapter extends Arrayadapter<string> {public Myadapter-context, int textviewresourceid,
List<string> objects) {Super (context, Textviewresourceid, objects);
} @Override public boolean areallitemsenabled () {return false; @Override public boolean isenabled (int position) {//If-begins, the entry is not optional return!listtag.contains (GetItem (
position));
@Override public View getview (int position, view Convertview, ViewGroup parent) {View view = Convertview; A layout template that does not load according to the label type if (Listtag.contains (GetItem (position)) {//If it is a label item view = Layoutinflater.from
(GetContext ()). Inflate (R.layout.group_list_item_tag, NULL);
}else{//Otherwise is the data item view = Layoutinflater.from (GetContext ()). Inflate (R.layout.group_list_item, NULL);
}//Display name TextView TextView = (TextView) View.findviewbyid (R.id.group_list_item_text);
Textview.settext (GetItem (position));
Returns the overridden view return view;
}
}
}
2, the program main interface layout file Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<listview
android:id= "@+id/list" android:layout_width= "Fill_"
Parent "
android:layout_height=" wrap_content ">
</ListView>
</LinearLayout>
3, ListView data section style layout file Group_list_item.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" horizontal "
android:layout_width=" fill_parent "
android:layout_" height= "Wrap_content"
android:padding= "5dip" >
<imageview
android:src= "@drawable/icon"
Android:layout_width= "50px"
android:layout_height= "50px" >
</ImageView>
<textview
android:id= "@+id/group_list_item_text"
android:layout_width= "wrap_content"
android:layout_ height= "Fill_parent"
android:paddingleft= "5dip"
android:gravity= "center_vertical" >
</ Textview>
</LinearLayout>
4. Style layout file for the group flag row in ListView Group_list_item_tag.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" Wrap_content "
android: Background= "#555555"
android:paddingleft= "10dip" >
<textview
android:id= "@+id/group_list_item _text "
android:layout_width=" wrap_content "
android:layout_height=" 20dip "
android:textcolor=" # FFFFFF "
android:gravity=" center_vertical ">
</TextView>
</LinearLayout>
For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"
I hope this article will help you with the Android program.