Android ListView 默認(rèn)選中某一項(xiàng)實(shí)現(xiàn)代碼
這里是使用 TOC 生成的目錄:
•Layout文件定義
◦ListView定義
◦item 模板定義
•代碼
◦初始化列表
◦用戶點(diǎn)擊處理
•效果
--------------------------------------------------------------------------------
要使用 ListView 實(shí)現(xiàn)一個(gè)充值方式選擇,默認(rèn)想選中第二項(xiàng),搞了一下午,終于搞定了。原本就沒怎么用 Java 寫過 Android 應(yīng)用,又隔了好久沒寫,一切都生疏了,半吊子變成大呆瓜了……
Layout文件定義
分兩部分,一部分是 ListView 的定義,一部分 item 模板,即 row 的定義。
ListView定義
說起來也很簡(jiǎn)單,下面是 Layout 文件中的 ListView 定義:
<ListView
android:id="@+id/recharge_method_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:dividerHeight="2dp"
android:divider="@color/ssq_bkgnd"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:listSelector="@null"
>
</ListView>
嘿,別說,CSDN的Markdown編輯器比原來的默認(rèn)編輯器好用多了,插入代碼更簡(jiǎn)單了。這是第一次使用CSDN的Markdown,贊一個(gè)。
item 模板定義
item模板如下定義:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="@drawable/option_selector"
>
<ImageView
android:id="@+id/recharge_method_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="4dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/recharge_method_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
/>
<TextView
android:id="@+id/recharge_method_clue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
<ImageView
android:id="@+id/recharge_method_checked"
android:layout_width="34dp"
android:layout_height="28dp"
android:layout_marginRight="16dp"
android:src="@drawable/option_checked"
android:visibility="invisible"
/>
</LinearLayout>
我為了給一個(gè) ListView 的 item 顯示一個(gè)選擇圖標(biāo),在定義 item 模板文件時(shí)直接加了一個(gè) ImageView ,通過控制它的顯示和隱藏來達(dá)到看起來選中的效果。偷了個(gè)懶,這是比較簡(jiǎn)單的實(shí)現(xiàn),在 ListView 中 item 數(shù)量不多時(shí)對(duì)內(nèi)存、性能等影響不大。
代碼
代碼比較簡(jiǎn)單,分兩部分來看吧,一部分是初始化列表,一部分是用戶點(diǎn)擊列表中的某項(xiàng)后切換選中標(biāo)記。
初始化列表
initRechargeList()方法用來初始化充值方式列表,代碼如下:
private void initRechargeList(){
actionTexts = new String[]{
getString(R.string.recharge_unionpay), getString(R.string.recharge_alipay), getString(R.string.recharge_bestpay)
};
actionClue = new String[]{
getString(R.string.recharge_unionpay_clue), getString(R.string.recharge_alipay_clue), getString(R.string.recharge_bestpay_clue)
};
actionImages = new int[]{
R.drawable.unionpay,
R.drawable.recharge_icon_alipay,
R.drawable.recharge_icon_bestpay
};
actionList = (ListView)findViewById(R.id.recharge_method_list);
actionItems = new ArrayList<HashMap<String, Object>>();
actionAdapter = new SimpleAdapter(this, actionItems, R.layout.recharge_method_list_item,
new String[]{"action_icon", "action_name", "action_clue"},
new int[]{R.id.recharge_method_icon, R.id.recharge_method_name, R.id.recharge_method_clue});
for(int i = 0; i < actionImages.length; ++i) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("action_icon", actionImages[i]);
item.put("action_name", actionTexts[i]);
item.put("action_clue", actionClue[i]);
actionItems.add(item);
}
actionList.setAdapter(actionAdapter);
actionList.setOnItemClickListener(itemListener);
actionList.post(new Runnable() {
@Override
public void run() {
lastCheckedOption = actionList.getChildAt(1).findViewById(R.id.recharge_method_checked);
lastCheckedOption.setVisibility(View.VISIBLE);
actionList.setItemChecked(1, true);
}
});
}
上面的代碼是初始化充值方式列表。 ListView 的用法也比較簡(jiǎn)單,View–Row Template–Data–Adapter,四個(gè)要素。
我遇到的問題是:如何默認(rèn)選中某一項(xiàng)。
實(shí)際上我的列表中只有三項(xiàng),不用考慮哪一項(xiàng)會(huì)不可見,應(yīng)該在安卓手機(jī)上都是可見的。
一開始我在調(diào)用了 ListView 的 setAdapter 方法后,直接使用 getChildAt(1) 來獲取第二項(xiàng)對(duì)應(yīng)的 View ,你猜到了,沒錯(cuò),崩潰了: NullPointerException ??罩羔槹。?C++ 時(shí)的老情人,改用 Java 寫 Android 了,她又跑來和我約會(huì)了。
搞了半天,我才弄明白: setAdapter() 其實(shí)是異步的 ,調(diào)用了這個(gè)方法, ListView 的 item 并沒有立馬創(chuàng)建,而是在下一輪消息處理時(shí)才創(chuàng)建。弄明白了這個(gè),就有了前面代碼中的解決辦法:使用 post() 提交一個(gè) Runnable() 對(duì)象,在 Runnable() 內(nèi)部來做默認(rèn)選中這種初始化動(dòng)作。
如你所見,我 new 了一個(gè) Runnable 給 post() 方法,在 run() 內(nèi)找到了第 2 項(xiàng),顯示了選中圖標(biāo);并且我把第 2 項(xiàng)對(duì)應(yīng)的 View 保存到 lastCheckedOption 成員變量中。后面我們會(huì)通過 lastCheckedOption 這個(gè)變量,結(jié)合 OnItemClickListener 來實(shí)現(xiàn) ListView 中三個(gè) item 的互斥選擇效果。
Markdown怎么給每個(gè)段落前加縮進(jìn)呢……迷惑中……四個(gè)空格就給本段落打上背景色了,挺好……
用戶點(diǎn)擊處理
點(diǎn)擊處理是通過 AdapterView.OnItemClickedListener 接口完成的。代碼如下:
private AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(lastCheckedOption != null){
lastCheckedOption.setVisibility(View.INVISIBLE);
}
lastCheckedOption = view.findViewById(R.id.recharge_method_checked);
lastCheckedOption.setVisibility(View.VISIBLE);
}
};
如你所見,我通過 lastCheckedOption 變量保存了上次選中的 item 中的表示選中效果的圖標(biāo),用戶點(diǎn)擊某一個(gè)時(shí),先隱藏上一個(gè) item 的選中圖標(biāo),再顯示當(dāng)前的,就有了貌似互斥的效果了。
一切就這么簡(jiǎn)單,搞定了。
效果
最終的效果是醬紫的:

噯,插入圖片比原來的非 Markdown 編輯器好用多了。
--------------------------------------------------------------------------------
好啦,想不到我又來寫 Android 應(yīng)用了,感覺很 High 啊。
--------------------------------------------------------------------------------
還不知道 Markdown 版本的編輯器寫出來的博客,發(fā)表出來腫么樣呢,生成了個(gè)目錄,直接 TOC 就 OK 了,還是很方便的。寫完了,還是沒搞明白段落的行首縮進(jìn)如何搞呢。
據(jù)說還支持離線編輯,贊。
還有一點(diǎn):左右分欄,可以看到效果,比 github 的 wiki 頁面編輯要強(qiáng)一點(diǎn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio 3.6安裝全過程及AVD安裝運(yùn)行步驟詳解
這篇文章主要介紹了Android Studio 3.6安裝全過程及AVD安裝運(yùn)行步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Mac OS X 下有關(guān)Android adb用法詳解
這篇文章主要介紹了Mac OS X 下有關(guān)Android adb用法詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android View的事件分發(fā)機(jī)制深入分析講解
事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會(huì)傳遞給Android的輸入系統(tǒng)服務(wù)IMS,通過Binder把消息發(fā)送到activity,activity會(huì)通過phoneWindow、DecorView最終發(fā)送給ViewGroup。這里就直接分析ViewGroup的事件分發(fā)2023-01-01
Android studio 混淆+打包+驗(yàn)證是否成功
本文主要介紹了Android studio 混淆+打包+驗(yàn)證是否成功的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Android開發(fā)中setContentView和inflate的區(qū)別分析
這篇文章主要介紹了Android開發(fā)中setContentView和inflate的區(qū)別,較為詳細(xì)的分析了setContentView和inflate的功能、用法及二者的區(qū)別,需要的朋友可以參考下2016-07-07
Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼
這篇文章主要介紹了Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,代碼中包含注釋,需要的朋友可以參考下2015-04-04
android中RecycleView添加下滑到底部的監(jiān)聽示例
本篇文章主要介紹了android中RecycleView添加下滑到底部的監(jiān)聽示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android Studio 自定義Debug變量視圖的方法
這篇文章主要介紹了Android Studio 自定義Debug變量視圖的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Android?Studio中如何修改APP圖標(biāo)和APP名稱
這篇文章主要介紹了Android?Studio中如何修改APP圖標(biāo)和APP名稱,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

