Android編程開(kāi)發(fā)中ListView的常見(jiàn)用法分析
本文實(shí)例講述了Android編程開(kāi)發(fā)中ListView的常見(jiàn)用法。分享給大家供大家參考,具體如下:
一、ListView的使用步驟
ListView的使用通常有以下三個(gè)要素:
(1)ListView中每個(gè)條目的布局;
(2)填充進(jìn)入ListView中的內(nèi)容;
(3)將內(nèi)容與頁(yè)面進(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>
接下來(lái)是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。廢話不多說(shuō),以下為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)聽(tīng)器.
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程使用ListView實(shí)現(xiàn)數(shù)據(jù)列表顯示的方法
- Android編程使用緩存優(yōu)化ListView的方法
- Android中ListView如何分頁(yè)加載數(shù)據(jù)
- Android實(shí)現(xiàn)ListView分頁(yè)自動(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開(kāi)發(fā)之ListView實(shí)現(xiàn)Item局部刷新
- android開(kāi)發(fā)之listView組件用法實(shí)例簡(jiǎn)析
相關(guān)文章
Android MonoRepo多倉(cāng)和單倉(cāng)的差別理論
這篇文章主要為大家介紹了Android MonoRepo多倉(cāng)和單倉(cāng)的差別理論,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android中g(shù)ravity、layout_gravity、padding、margin的區(qū)別小結(jié)
這篇文章主要介紹了Android中g(shù)ravity、layout_gravity、padding、margin的區(qū)別小結(jié),需要的朋友可以參考下2014-08-08Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Flutter Reusable Lottie Animations技巧
這篇文章主要為大家介紹了Flutter Reusable Lottie Animations技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08