原文网址:http://blog.csdn.net/checkin001/article/details/11519131

Android自带的RadioButton单选框只支持添加文字,我们自己写Adapter实现自定义的RadioButton

首先item的XML源码

search_user_item.xml (现在只是文字+单选按钮+自定义背景,可以根据需要随意扩展)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/search_user_list_item"
  4. android:layout_width="fill_parent"
  5. android:layout_height="30dp"
  6. android:layout_marginBottom="10dp"
  7. android:layout_marginTop="10dp"
  8. android:background="@drawable/more_item_press"
  9. android:gravity="center_vertical"
  10. android:orientation="horizontal" >
  11. <TextView
  12. android:id="@+id/search_user_name"
  13. android:layout_width="200dp"
  14. android:layout_height="wrap_content"
  15. android:layout_marginLeft="30dp"
  16. android:gravity="left"
  17. android:textColor="@android:color/black"
  18. android:textSize="16sp" />
  19. <RadioButton
  20. android:id="@+id/radio_btn"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_marginLeft="10dp" />
  24. </LinearLayout>

Listview就是用系统自带的

  1. <ListView
  2. android:id="@+id/search_user_list"
  3. android:layout_width="fill_parent"
  4. android:layout_height="200dp"
  5. android:layout_marginLeft="5dp"
  6. android:layout_marginRight="5dp"
  7. android:paddingBottom="5dp"
  8. android:cacheColorHint="@android:color/transparent"
  9. android:divider="@null"
  10. android:listSelector="@android:color/transparent"
  11. android:visibility="gone" >
  12. </ListView>

再来是Adapter代码

SearchUserAdapter.java (具体改动写在代码注释里面)

  1. package ouc.sei.suxin.android.ui.adapter;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import ouc.sei.suxin.R;
  5. import android.content.Context;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseAdapter;
  10. import android.widget.LinearLayout;
  11. import android.widget.RadioButton;
  12. import android.widget.TextView;
  13. public class SearchUserAdapter extends BaseAdapter {
  14. private Context context;
  15. private List<String> userList;
  16. HashMap<String,Boolean> states=new HashMap<String,Boolean>();//用于记录每个RadioButton的状态,并保证只可选一个
  1. public SearchUserAdapter(Context context, List<String> userList)
  2. {
  3. this.context = context;
  4. this.userList= userList;
  5. }
  6. @Override
  7. public int getCount() {
  8. return userList.size();
  9. }
  10. @Override
  11. public Object getItem(int position) {
  12. return userList.get(position);
  13. }
  14. @Override
  15. public long getItemId(int position) {
  16. return position;
  17. }
  18. @Override
  19. public View getView(final int position, View convertView, ViewGroup parent) {
  20. ViewHolder holder;
  21. if (convertView == null) {
  22. convertView = LayoutInflater.from(context).inflate(R.layout.search_user_item, null);
  23. holder = new ViewHolder();
  24. holder.background = (LinearLayout) convertView.findViewById(R.id.search_user_list_item);
  25. holder.userName = (TextView) convertView.findViewById(R.id.search_user_name);
  26. convertView.setTag(holder);
  27. }else{
  28. holder=(ViewHolder) convertView.getTag();
  29. }
  30. final RadioButton radio=(RadioButton) convertView.findViewById(R.id.radio_btn);
  31. holder.rdBtn = radio;
  32. holder.userName.setText(userList.get(position));
  33. //根据Item位置分配不同背景
  34. if(userList.size() > 0)
  35. {
  36. if(userList.size() == 1)
  37. {
  38. holder.background.setBackgroundResource(R.drawable.more_item_press);
  39. }
  40. else{
  41. if(position == 0){
  42. holder.background.setBackgroundResource(R.drawable.more_itemtop_press);
  43. }
  44. else if(position == userList.size()-1){
  45. holder.background.setBackgroundResource(R.drawable.more_itembottom_press);
  46. }
  47. else{
  48. holder.background.setBackgroundResource(R.drawable.more_itemmiddle_press);
  49. }
  50. }
  51. }
  52. //当RadioButton被选中时,将其状态记录进States中,并更新其他RadioButton的状态使它们不被选中
  53. holder.rdBtn.setOnClickListener(new View.OnClickListener() {
  54. public void onClick(View v) {
  55. //重置,确保最多只有一项被选中
  56. for(String key:states.keySet()){
  57. states.put(key, false);
  58. }
  59. states.put(String.valueOf(position), radio.isChecked());
  60. SearchUserAdapter.this.notifyDataSetChanged();
  61. }
  62. });
  63. boolean res=false;
  64. if(states.get(String.valueOf(position)) == null || states.get(String.valueOf(position))== false){
  65. res=false;
  66. states.put(String.valueOf(position), false);
  67. }
  68. else
  69. res = true;
  70. holder.rdBtn.setChecked(res);
  71. return convertView;
  72. }
  73. static class ViewHolder {
  74. LinearLayout background;
  75. TextView userName;
  76. RadioButton rdBtn;
  77. }

List适配代码(与一般无异):

  1. adapter = new SearchUserAdapter(this, searchUserList);
  2. searchUserLV.setAdapter(adapter);
  3. searchUserLV.setVisibility(View.VISIBLE);
  4. setListViewHeightBasedOnChildren(searchUserLV);

这里还根据内容动态设置了一下,具体函数如下:

  1. public void setListViewHeightBasedOnChildren(ListView listView) {
  2. Adapter listAdapter = listView.getAdapter();
  3. if (listAdapter == null) {
  4. return;
  5. }
  6. int totalHeight = 0;
  7. int viewCount = listAdapter.getCount();
  8. for (int i = 0; i < viewCount; i++) {
  9. View listItem = listAdapter.getView(i, null, listView);
  10. listItem.measure(0, 0);
  11. totalHeight += listItem.getMeasuredHeight();
  12. }
  13. ViewGroup.LayoutParams params = listView.getLayoutParams();
  14. params.height = totalHeight
  15. + (listView.getDividerHeight() * (listAdapter.getCount()-1)) + 10;//加10是为了适配自定义背景
  16. listView.setLayoutParams(params);
  17. }

当需要获取ListView中RadioButton的选择状态时,可以直接看Adapter中的states,具体如下:

  1. // 根据RadioButton的选择情况确定用户名
  2. for (int i = 0, j = searchUserLV.getCount(); i < j; i++) {
  3. View child = searchUserLV.getChildAt(i);
  4. RadioButton rdoBtn = (RadioButton) child
  5. .findViewById(R.id.radio_btn);
  6. if (rdoBtn.isChecked())
  7. searchUser = searchUserList.get(i);
  8. }

这里的searchUserList是调用后台服务获取的用户名列表,通过states获取选中用户名进行后续操作

效果图:

版权声明:本文为博主原创文章,未经博主允许不得转载。

【转】ListView与RadioButton组合——自定义单选列表的更多相关文章

  1. ListView与RadioButton组合——自定义单选列表

      标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报  分类: Android(19)  版权声明: ...

  2. ListView与CheckBox组合实现单选

    main.xml配置文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  3. Android开发技巧——自定义单选或多选的ListView

    这篇其实应该是属于写自定义单选或多选的ListView的基础教程,无奈目前许多人对此的实现大多都绕了远路,反而使得这正规的写法倒显得有些技巧性了. 本文原创,转载请注明在CSDN上的出处: http: ...

  4. MVC应用程序与单选列表

    原文:MVC应用程序与单选列表 前几天,Insus.NET有在MVC应用程序中,练习了<MVC应用程序,动态创建单选列表(RadioButtonList)>http://www.cnblo ...

  5. ListView 自己定义BaseAdapter实现单选打勾(无漏洞)

    (假设须要完整demo,请评论留下邮箱) (眼下源代码已经不发送.假设须要源代码,加qq316701116.不喜勿扰) 近期由于一个项目的原因须要自己定义一个BaseAdapter实现ListVIew ...

  6. (转载)Android自定义标签列表控件LabelsView解析

    Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App, ...

  7. ylbtech-数据库设计与优化-对作为复选框/单选列表的集合表的设计

    ylbtech-DatabaseDesgin:ylbtech-数据库设计与优化-对作为复选框/单选列表的集合表的设计 -- DatabaseName:通用表结构-- -- 主要是针对将要设计的表对象, ...

  8. FastScroll(1)ListView打开FastScroll及自定义它的样式

    打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...

  9. 自定义SharePoint列表新增、编辑、查看页面(NewForm、EditForm、DispForm)

    转:http://blog.csdn.net/lance_lot1/article/details/7966571 在项目中,用户需求涉及在一个列表录入项目信息,选择一个项目后,与该项目相关的信息实现 ...

随机推荐

  1. 网络编程---(数据请求+slider)将网络上的大文件下载到本地,并打印其进度

    网络编程---将网络上的大文件下载到本地,并打印其进度. 点击"開始传输"button.将网络上的大文件先下载下来,下载完毕后,保存到本地. UI效果图例如以下: watermar ...

  2. Java并发学习之二——获取和设置线程信息

    本文是学习网络上的文章时的总结,感谢大家无私的分享. Thread类的对象中保存了一些属性信息可以帮助我们辨别每个线程.知道它的一些信息 ID:每一个线程的独特标示: Name:线程的名称: Prio ...

  3. [yueqian_scut]蓝牙防丢器原理、实现与Android BLE接口编程

    本文是对已实现的蓝牙防丢器项目的总结,阐述蓝牙防丢器的原理.实现与Android客户端的蓝牙BLE接口编程.在这里重点关注如何利用BLE接口来进行工程实现,对于BLE的协议.涉及到JNI的BLE接口内 ...

  4. PHP CodeBase: 判断用户是否手机访问(转)

    随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.这里就介绍 ...

  5. QT 内存泄露 检测

      一:问题出现     最近几天在做一个QT程序,IPX的检测控制程序.需要全天候运行.自己做完了,然后就运行.使用  top|grep TP2  来动态检测程序的CPU,内存占用律.不幸的是,一晚 ...

  6. 9.13noip模拟试题

    题目名称 “与” 小象涂色 行动!行动! 输入文件 and.in elephant.in move.in 输出文件 and.out elephant.in move.in 时间限制 1s 1s 1s ...

  7. OD: Kernel Exploit - 2 Programming

    本节接前方,对 exploitme.sys 进行利用. exploitme.sys 存在任意地址写任意内容的内核漏洞,现在采用执行 Ring0 Shellcode 的方式进行利用. 获取 HalDis ...

  8. js的new操作符

    1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . ...

  9. 突然想写点东西,关于web新人的。采用问答方式

    我自己是会计专业,转行自学web的,学习有一两年了,也还是新人一个,只不过不是那种超级“新”的,所以有什么话说得不对,请轻喷.欢迎大家来和我交流. 1.我能不能转行学web? 能不能学web这个不是别 ...

  10. jsp中的c标签

    核心标签库 引用: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> ...