In the previous tutorial, we implemented a custom ListView using Simpleadapter, but it was inconvenient to have a different style setting for each of the ListView item options, which would be cumbersome, Because the Simpleadapter uses a uniform style style. If you want to implement different item styles using different styles, then you need to integrate baseadapter to implement them.
First, ActivityMainactivity.java Source:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//step1string[] Contentarray = new String[]{"Content 1", "Content 2 "," Content 3 "," Content 4 "," Content 5 "}; ListView LV = (ListView) Findviewbyid (r.id.lv);//step2arraylist
Second, XML layout file
(1) Mainactivity main interface layout source code activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" vertical "> <listview Android:id= "@+id/lv" android:layout_width= "match_parent" android:layout_height= "Wrap_content"/></ Linearlayout>
(2) The layout of the ListView List Unit source code list_item.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:layout_height=" Match_parent "android:layout_width=" match_parent "android:gravity=" Center_ Vertical "android:orientation=" horizontal "> <linearlayout android:layout_width=" 0DP "Android:lay out_height= "Wrap_content" android:layout_weight= "2" android:gravity= "center_vertical" android:orientation= " Vertical "> <textview android:id=" @+id/starttimetext "android:layout_height=" Wrap_ Content "android:layout_width=" match_parent "/> <textview android:id=" @+id/endtimetext " android:layout_height= "Wrap_content" android:layout_width= "match_parent"/> </LinearLayout> < ImageView android:id= "@+id/separator" android:layout_weight= "1" android:layout_width= "0DP" android:layout_height = "Wrap_content"/> <textview android:id= "@+id/content" Android:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_weight= "/> </LinearLayout" >
Iii. Related ResourcesFiles under res/drawable-mdpi/:
Separator1.png separator2.png separator3.png separator4.png separator5.png
Four, the effect
Android Development most common examples of finishing----(3) Customizing the ListView (Custom Baseadapter Implementation)