Android中ListView用法實(shí)例分析
本文實(shí)例分析了Android中ListView用法。分享給大家供大家參考,具體如下:
通過(guò)在Layout中添加ListView Widget可以達(dá)到在頁(yè)面布局具有列表效果的交互頁(yè)面。在這里通過(guò)舉例來(lái)說(shuō)明怎樣在Layout中添加ListView以及怎樣應(yīng)用。
配合設(shè)計(jì)了兩個(gè)事件Listener: OnItemSelectedListener事件為鼠標(biāo)的滾輪轉(zhuǎn)動(dòng)時(shí)所選擇的值;OnItemClickListener事件則為當(dāng)鼠標(biāo)單擊時(shí),所觸發(fā)的事件。由此可以區(qū)別出list中的“選擇”與“單擊”差異。
使用ArrayAdapter(Context context, int textViewResourceId , T[] objects)這個(gè)構(gòu)造器,其中textViewResourceId是定義在“res/layout/my_simple_list_item.xml”里的資源ResourceID(R.layout.my_simple_list_item),里面使用CheckedTextView來(lái)取得ListView中選擇的項(xiàng)目。
程序中使用了LinearLayout對(duì)象,動(dòng)態(tài)地將TextView與ListView附加進(jìn)原有的Layout布局當(dāng)中。用LinearLayout.LayoutParams來(lái)創(chuàng)建對(duì)象param1,再調(diào)用LinearLayout的addView方法將TextView和ListView以及params對(duì)象傳入。
程序如下所示:
public class A07Activity extends Activity { private static final String[]week="sunday","monday","tuesday","wednesday","thursday","friday","saturday"}; private TextView tv; private ListView lv; private ArrayAdapter<String> aa; private LinearLayout ll; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv=new TextView(this); tv.setText(R.string.title); tv.setTextColor(Color.RED); lv=new ListView(this); lv.setBackgroundColor(Color.GREEN); ll=new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setBackgroundColor(android.graphics.Color.YELLOW); aa=new ArrayAdapter<String>(A07Activity.this,R.layout.my_simple_list,week); lv.setAdapter(aa); LinearLayout.LayoutParams params01=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); ll.addView(lv, params01); LinearLayout.LayoutParams params02=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); ll.addView(tv, params02); setContentView(ll); lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub tv.setText("你選擇的是:"+arg0.getSelectedItem().toString()); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub tv.setText("你選擇的是:"+week[arg2]); } }); } }
res/layout/my_simple_list.xml如下:
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myCheckedTextView1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" />
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android應(yīng)用中通過(guò)Layout_weight屬性用ListView實(shí)現(xiàn)表格
- 詳解Android中實(shí)現(xiàn)ListView左右滑動(dòng)刪除條目的方法
- 詳解Android應(yīng)用中ListView列表選項(xiàng)欄的編寫(xiě)方法
- Android ListView物流獲取追蹤功能實(shí)現(xiàn)
- Android ListView常用小技巧匯總
- Android應(yīng)用中ListView利用OnScrollListener分頁(yè)加載數(shù)據(jù)
- Android實(shí)現(xiàn)簡(jiǎn)單的分批加載ListView
- Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個(gè)view中
- Android中ListView下拉刷新的實(shí)現(xiàn)方法
- Android ListView異步加載圖片方法詳解
- 實(shí)例講解Android app開(kāi)發(fā)中ListView的基本使用及優(yōu)化
- Android編程實(shí)現(xiàn)動(dòng)態(tài)更新ListView的方法
- Android listview多視圖嵌套多視圖
- Android App界面的ListView布局實(shí)戰(zhàn)演練
相關(guān)文章
Android開(kāi)發(fā)實(shí)例之登錄界面的實(shí)現(xiàn)
本文主要介紹Android 登錄界面實(shí)現(xiàn),這里主要講解類似Twitter的登錄界面的實(shí)現(xiàn),有興趣的小伙伴可以參考下2016-08-08使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè)
這篇文章主要為大家詳細(xì)介紹了使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Android App多個(gè)入口的實(shí)現(xiàn)方法
這篇文章主要介紹了Android App多個(gè)入口的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Android ListView實(shí)現(xiàn)圖文列表顯示
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)圖文列表顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫(huà)
這篇文章主要介紹了Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫(huà)效果的相關(guān)內(nèi)容,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android Flutter實(shí)現(xiàn)視頻上滑翻頁(yè)效果的示例代碼
我們?cè)诙桃曨l應(yīng)用中經(jīng)常會(huì)看到不停上滑瀏覽下一條視頻的沉浸式交互效果,這種交互能夠讓用戶不停地翻頁(yè),直到找到喜歡的視頻內(nèi)容。本文將通過(guò)Flutter中的PageView組件實(shí)現(xiàn),感興趣的可以了解一下2022-10-10Android基于TextView實(shí)現(xiàn)的跑馬燈效果實(shí)例
這篇文章主要介紹了Android基于TextView實(shí)現(xiàn)的跑馬燈效果,以完整實(shí)例形式分析了Android使用TextView通過(guò)屬性設(shè)置及功能代碼實(shí)現(xiàn)跑馬燈效果的相關(guān)技巧,需要的朋友可以參考下2016-02-02