首页 / 操作系统 / Linux / Android开发:ListView常用技巧
ListView 是 AdapterView 的派生类, AdapterView 是 ViewGroup 的派生类。 ListView 将需要显示的内容,放在一个可以垂直滚动的列表中进行显示。而要显示的内容是由和 ListView 相关联的 ListAdapter 指定的。通常是 ArrayAdapter 或者 CursorAdapter ,这两者都是 ListAdapter 的派生类。 因此 ArrayAdapter 和 CursorAdapter 就是 ListView 的数据源。通常情况下,他们的主要区别是: a. ArrayAdapter 用于指定数组中的数据,而 CursorAdapter 用于指定一个 Cursor 对象中的数据 ( 比如从数据库中查询得到的结果 ) b. ArrayAdapter 用于适合指定只有一列的数据,而 CursorAdapter 适合指定由多列的数据,但这点并不是很严格,也就是说 ArrayAdapter 也可以用于多列数据, CursorAdapter 也可以用于显示单列数据。 下面我们用实际的例子来说明。 第一个例子: 最简单的 ListView 用法。 1. 创建一个 Android Project ,修改其 main.xml ,使之如下: <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/linearlayout" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > <!-- 在 layout 中增加一个 ListView --> < ListView android:id = "@+id/listview" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ LinearLayout > 2. 修改 Activity 所对应的代码,使之如下: package com.pat.gui; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ControlListView extends Activity implements OnItemClickListener { // 声明一个 ListView 对象 private ListView listview ; // 定义一个 String 数组 , 用以代表各种不同的手机操作系统 private String os [] = { "Android" , "iOS" , "Windows Phone" , "Symbian" , "BlackBerry" , "PalmOS" , "OPhone" , "Others..." }; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. main ); // 获得 ListView 对象 listview = (ListView) this .findViewById(R.id. listview ); // 定义一个 ArrayAdapter 对象 , ArrayAdapter 有多个构造方法重载 , 其中下面用到的构造方法原型为 : //public ArrayAdapter (Context context, int textViewResourceId, T[] objects) //context The current context. //textViewResourceId The resource ID for a layout file containing a TextView to use // when instantiating views. //objects The objects to represent in the ListView. ArrayAdapter<String> adapter = new ArrayAdapter<String>( this , android.R.layout. simple_list_item_1 , os ); // android.R.layout.simple_list_item_1 是 Android 预先定义好的 , 我们自己也可以另外定义 listview .setAdapter(adapter); // 将 adapter 和 listview 关联起来 listview .setOnItemClickListener( this ); // 为 listview 设置 OnItemClickListener } //@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) // parent The AdapterView where the click happened. // view The view within the AdapterView that was clicked (this will be a view provided by the adapter) // position The position of the view in the adapter. // id The row id of the item that was clicked. { Toast.makeText ( this , "/"" + ((TextView)view).getText().toString() + "/". It"s position is " + position, Toast. LENGTH_SHORT ).show(); } } 运行结果如下: 上面的 ListView 可以上下滚动。 点击 Symbian ,则会出现: 第二个例子: 自定义显示 ListView 中每行的 layout ,同时显示图片和文字。 1. 在 res/layout 中,创建一个用于显示 ListView 条目的 layout 文件: rowlayout.xml ,使之如下: <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "horizontal" > < ImageView android:id = "@+id/row_icon" android:layout_width = "60px" android:layout_height = "80px" android:src = "@drawable/icon" /> < TextView android:id = "@+id/row_text" android:layout_width = "wrap_content" android:layout_height = "80px" android:textSize = "30px" android:textColor = "#0F0" android:gravity = "center_vertical" /> </ LinearLayout > 其中的 ImageView 用于显示图片, TextView 用于显示文字。