Android編程開發(fā)中ListView的常見用法分析
本文實(shí)例講述了Android編程開發(fā)中ListView的常見用法。分享給大家供大家參考,具體如下:
一、ListView的使用步驟
ListView的使用通常有以下三個(gè)要素:
(1)ListView中每個(gè)條目的布局;
(2)填充進(jìn)入ListView中的內(nèi)容;
(3)將內(nèi)容與頁面進(jìn)行整合的Adapter.
因此,使用ListView也通常有以下三個(gè)步驟:
(1)創(chuàng)建ListView條目的布局文件(或使用Android SDK提供的布局);
(2)創(chuàng)建填充進(jìn)入ListView中的內(nèi)容,如字符串、圖片等;
(3)創(chuàng)建Adapter并將其與ListView綁定.
二、使用默認(rèn)的布局文件創(chuàng)建ListView
因?yàn)楸纠惺褂玫氖茿ndroid SDK默認(rèn)的布局文件:android.R.layout.simple_list_item_1,所以只需創(chuàng)建主Activity布局文件。
ListViewTest.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" android:orientation="vertical" > <ListView android:id="@+id/mylistview" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout>
接下來是Activity文件。
ListViewTestActivity.java
package com.blogtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewTestActivity extends Activity {
private static String[] strs = new String[] { "1", "2", "3", "4", "5" };//定義要顯示的數(shù)據(jù)
private ListView myListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewtest);
findWidgets();
setAdapter();
}
private void findWidgets() {
myListView = (ListView) findViewById(R.id.mylistview);
}
private void setAdapter() {
myListView.setAdapter(new ArrayAdapter<String>(this, //此例中用的是ArrayAdapter
android.R.layout.simple_list_item_1, strs));//使用系統(tǒng)自帶的布局文件
}
}
運(yùn)行結(jié)果為:

三、支持多項(xiàng)選擇的ListView
Android還提供了一個(gè)支持多項(xiàng)選擇的item布局文件:android.R.layout.simple_list_item_multiple_choice.
但還必須調(diào)用ListView.setChoiceMode()方法。
修改的代碼片段如下:
private void setAdapter() {
myListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, strs));
myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
運(yùn)行結(jié)果為:

四、響應(yīng)觸摸事件
為了讓ListView中的Item響應(yīng)點(diǎn)擊事件,需要?jiǎng)?chuàng)建一個(gè)OnItemClickListener類并綁定給該ListView。廢話不多說,以下為Activity類文件:
package com.blogtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewTestActivity extends Activity {
private static String[] strs = new String[] { "0", "1","2", "3", "4", "5" };
private ListView myListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findWidgets();
setAdapter();
setListener();
}
private void findWidgets() {
myListView = (ListView) findViewById(R.id.mylistview);
}
private void setAdapter() {
myListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strs));
}
private void setListener() {
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
new AlertDialog.Builder(ListViewTestActivity.this)
.setMessage("Clicked Line No." + arg2)
.setPositiveButton("Confirm", null).show();
}
});
}
}
運(yùn)行結(jié)果為:

除了OnItemClickListener之外,還有OnItemLongClickListener,OnItemSelectedListener等監(jiān)聽器.
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
- Android編程使用緩存優(yōu)化ListView的方法
- Android中ListView如何分頁加載數(shù)據(jù)
- Android實(shí)現(xiàn)ListView分頁自動(dòng)加載數(shù)據(jù)的方法
- Android checkbox的listView具體操作方法
- Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法
- Android ListView優(yōu)化之提高android應(yīng)用效率
- Android ListView詳解
- Android編程記錄ListView標(biāo)記行狀態(tài)的方法
- Android中ListView Item布局優(yōu)化技巧
- Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android開發(fā)之listView組件用法實(shí)例簡(jiǎn)析
相關(guān)文章
Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有記住密碼功能的登陸界面,主要采用SharedPreferences來保存用戶數(shù)據(jù),感興趣的小伙伴們可以參考一下2016-05-05
Android開發(fā)筆記之:返回鍵的復(fù)寫onBackPressed()介紹
比較完整的android MP3 LRC歌詞滾動(dòng)高亮顯示(附源碼)
android studio安裝時(shí) AVD出現(xiàn)問題如何快速解決
Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法

