`
chenxu_8456
  • 浏览: 40508 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Android中ListView控件的简单使用

阅读更多
在android中ListView是一个经常使用到的控件,该控件是android众多列表控件中的一种,以垂直的方式显示一组项,对于ListView的使用包含以下三部分:
1)建立一个包含ListView的布局文件和一个针对ListView中每一个项的布局文件;
2)创建一个Activity(最简单的方式是继承ListActivity);
3)创建一个ListAdapter,填充所需的数据后通过addListAdapter添加ListAdapter至该Activity;
在下面的代码中给出的只是最简单的方式用以演示最基本的使用方式:
1)建立所需的布局文件:
/res/layout/listview.xml
<?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="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

注意:ListView的id要使用android内置的id:@android:id/list
/res/layout/listview_item.xml
<?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" >

    <CheckBox
        android:id="@+id/checked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00FFFF"
        android:textStyle="bold"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFF00"
        android:textSize="15dp" />

</LinearLayout>

注意:对于Button,CheckBox等按钮控件如果用在ListView中需要设置android:clickable="false" android:focusable="false" android:focusableInTouchMode="false",否则的话click事件将被这些控件捕获,无法被ListView的OnListItemClick捕获;
2)创建一个继承自ListActivity的活动:
package ui.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListViewActivity extends ListActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.listview_item, new String[] { "author", "title" },
				new int[] { R.id.author, R.id.title });
		setListAdapter(adapter);
	}

	private List<Map<String, String>> getData() {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		Map<String, String> entry = new HashMap<String, String>();
		entry.put("author", "哈迪斯");
		entry.put("title", "死神");
		data.add(entry);
		entry = new HashMap<String, String>();
		entry.put("author", "宙斯");
		entry.put("title", "神王.雷神");
		data.add(entry);
		entry = new HashMap<String, String>();
		entry.put("author", "波塞冬");
		entry.put("title", "海神");
		data.add(entry);
		return data;
	}

	@Override
	public void onListItemClick(ListView list, View view, int position, long id) {
		LinearLayout layout = (LinearLayout) list.getChildAt(position);
		CheckBox cb = (CheckBox) layout.findViewById(R.id.checked);
		if (cb.isChecked()) {
			cb.setChecked(false);
		} else {
			cb.setChecked(true);
		}
	}
}

3)关联Adapter:
关联ListAdapter和设置onListItemClick的处理方式在上面的代码中给出;

运行结果如下:
  • 大小: 21 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics