Android ListView 默认选中某一项

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

这里是使用 TOC 生成的目录:


要使用 ListView 实现一个充值方式选择,默认想选中第二项,搞了一下午,终于搞定了。原本就没怎么用 Java 写过 Android 应用,又隔了好久没写,一切都生疏了,半吊子变成大呆瓜了……

Layout文件定义

分两部分,一部分是 ListView 的定义,一部分 item 模板,即 row 的定义。

ListView定义

说起来也很简单,下面是 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>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

嘿,别说,CSDN的Markdown编辑器比原来的默认编辑器好用多了,插入代码更简单了。这是第一次使用CSDN的Markdown,赞一个。

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>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

我为了给一个 ListView 的 item 显示一个选择图标,在定义 item 模板文件时直接加了一个 ImageView ,通过控制它的显示和隐藏来达到看起来选中的效果。偷了个懒,这是比较简单的实现,在 ListView 中 item 数量不多时对内存、性能等影响不大。

代码

代码比较简单,分两部分来看吧,一部分是初始化列表,一部分是用户点击列表中的某项后切换选中标记。

初始化列表

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);            }        });    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

上面的代码是初始化充值方式列表。 ListView 的用法也比较简单,View–Row Template–Data–Adapter,四个要素。

我遇到的问题是:如何默认选中某一项。

实际上我的列表中只有三项,不用考虑哪一项会不可见,应该在安卓手机上都是可见的。

一开始我在调用了 ListView 的 setAdapter 方法后,直接使用 getChildAt(1) 来获取第二项对应的 View ,你猜到了,没错,崩溃了: NullPointerException 。空指针啊,用 C++ 时的老情人,改用 Java 写 Android 了,她又跑来和我约会了。

搞了半天,我才弄明白: setAdapter() 其实是异步的 ,调用了这个方法, ListView 的 item 并没有立马创建,而是在下一轮消息处理时才创建。弄明白了这个,就有了前面代码中的解决办法:使用 post() 提交一个 Runnable() 对象,在 Runnable() 内部来做默认选中这种初始化动作

如你所见,我 new 了一个 Runnable 给 post() 方法,在 run() 内找到了第 2 项,显示了选中图标;并且我把第 2 项对应的 View 保存到 lastCheckedOption 成员变量中。后面我们会通过 lastCheckedOption 这个变量,结合 OnItemClickListener 来实现 ListView 中三个 item 的互斥选择效果。

Markdown怎么给每个段落前加缩进呢……迷惑中……四个空格就给本段落打上背景色了,挺好……
   
   
  • 1

用户点击处理

点击处理是通过 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);        }    };
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

如你所见,我通过 lastCheckedOption 变量保存了上次选中的 item 中的表示选中效果的图标,用户点击某一个时,先隐藏上一个 item 的选中图标,再显示当前的,就有了貌似互斥的效果了。

一切就这么简单,搞定了。

效果

最终的效果是酱紫的:

Android ListView 默认选中示意图

嗳,插入图片比原来的非 Markdown 编辑器好用多了。


好啦,想不到我又来写 Android 应用了,感觉很 High 啊。


还不知道 Markdown 版本的编辑器写出来的博客,发表出来肿么样呢,生成了个目录,直接 TOC 就 OK 了,还是很方便的。写完了,还是没搞明白段落的行首缩进如何搞呢。

据说还支持离线编辑,赞。

还有一点:左右分栏,可以看到效果,比 github 的 wiki 页面编辑要强一点。

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jfdfhh/article/details/83825612