`

android ListView

阅读更多
ListView

垂直列表显示内容

创建方法:
   使用ListView
   使用ListActivity

常用xml属性
android:divider  设置listView分隔条,可以是coler,drawable
android:dividerHeight 设置分隔条的高度
android:entries  指定一个数字资源,作为listView的内容


1.使用andriod:entries
布局xml
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ball"
        />

资源文件
	<string-array name="ball" >
	    <item>足球</item>
	    <item>篮球</item>
	    <item>网球</item>
	    <item>排球</item>
	</string-array>


2.使用ArrayAdapter

    <ListView
        android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ball"
        />


ListView list2 = (ListView) findViewById(R.id.list2);
	    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, 
	    android.R.layout.simple_list_item_1, 
	    new String[]{"one","two","three"});
list2.setAdapter(arrayAdapter);


3.使用SimpleAdapter

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@android:color/holo_purple"
        android:dividerHeight="5dp"
        />


布局文件
    <LinearLayout 
        android:layout_width="match_parent"
   		android:layout_height="match_parent"
   		android:orientation="horizontal">
        
        <ImageView 
            android:id="@+id/pic"
            android:layout_width="wrap_content"
   		    android:layout_height="wrap_content"/>
        <TextView 
            android:id="@+id/name"
            android:layout_width="wrap_content"
   		    android:layout_height="wrap_content"
   		    android:layout_marginLeft="20dp"/>
        
    </LinearLayout>


String[] from = {"pic","name"};
	int[] to ={R.id.pic,R.id.name};
	List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
ListView listView = (ListView) findViewById(R.id.listview);
		Map<String, Object> m1 = new HashMap<>();
		m1.put("pic", R.drawable.ic_launcher);
		m1.put("name", "bird");
		data.add(m1);
		Map<String, Object> m2 = new HashMap<>();
		m2.put("name", "king");
		m2.put("pic", R.drawable.ic_launcher);
		data.add(m2);
/**
		 * SimpleAdapter 参数
		 * 1.context
		 * 2.data List<Map<String,Object> 结构
		 *    list中的每一个map用来生成listview中一行数据
		 *    每个map中的key值和布局中的组件id对应有参数4,5来确定
		 * 3.布局id
		 * 4.data中的key值
		 * 5.布局中的id值
		 * */
		listView.setAdapter(new SimpleAdapter(this, data, R.layout.list_view1, from, to));


4.使用ListActivity

public class ListActivityTest extends ListActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/**
		 * 不需要setContentView()方法来指定布局,
		 * 若自己指定布局,者在布局文件中必须有一个id为list的ListView
		 */
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
		android.R.layout.simple_list_item_multiple_choice,
				new String[]{"北京","上海","广州"});
		setListAdapter(arrayAdapter);
	}
}

/**
 * 使用程序对ListView进行数据填充,需要用到adapter
 * ListView的setAdapter需要一个ListAdapter参数
 * ListAdapter是个接口,实现类有
 * ArrayAdapter<T>, BaseAdapter, CursorAdapter, 
 * HeaderViewListAdapter, ResourceCursorAdapter, 
 * SimpleAdapter, SimpleCursorAdapter, WrapperListAdapter等
 * 
 * 创建adapter时,需要用布局文件,
 * android.R.layout下已定义的listview的布局有
 * 
 * simple_list_item_1 每个列表项一个普通的TextView
 * simple_list_item_2 每个列表项两个普通的TextView
 * simple_list_item_checked 每个列表项都是一个已勾选的列表项
 * simple_list_item_multiple_choice 带多选框的文本
 * simple_list_item_single_choice   带单选框的文本
 * ......
 */
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics