The ListView is one of the more commonly used controls and is summarized here for easy reference.
The program effect is to implement a listview,listview with a title, content and picture, and add click and long press Response.
Steps:
1. First in Activity_main. A listview is defined in 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=" fill_parent "> <listview android:id= "@+id/listview" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "/></linearlayout>
2. Define the layout of each item in the ListView, implemented with Relativelayout: List_items.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:paddingbottom=" 4DP " android:paddingleft= "12DP" android:paddingright= "12DP" > <imageview android:id= "@+id/itemimage" Android:layout_width= "50DP" android:layout_height= "50DP" android:layout_alignparentright= "true" Andro id:paddingtop= "10DP"/> <textview android:id= "@+id/itemtitle" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "TextView01" android:textsize= "20DP"/> <te Xtview android:id= "@+id/itemtext" android:layout_width= "match_parent" android:layout_height= "Wrap_con Tent "android:layout_below=" @+id/itemtitle "android:text=" TextView02 "/></relativelayout>
3. Finally call and join the listener inside the mainactivity:
Package Com.chay.listviewtest;import Java.util.arraylist;import Java.util.hashmap;import android.os.Bundle;import Android.app.activity;import Android.view.contextmenu;import Android.view.menuitem;import Android.view.View;import Android.view.contextmenu.contextmenuinfo;import Android.view.view.oncreatecontextmenulistener;import Android.widget.adapterview;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.adapterview.onitemclicklistener;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /bind layout inside the Listviewlistview list = (ListView) Findviewbyid (R.id.listview);//Generate dynamic array, add data arraylistReference: www.iteye.com/topic/540423
Download: Listviewtest.rar
Android ListView Simple Usage