【转】ListView与RadioButton组合——自定义单选列表
原文网址:http://blog.csdn.net/checkin001/article/details/11519131
Android自带的RadioButton单选框只支持添加文字,我们自己写Adapter实现自定义的RadioButton
首先item的XML源码
search_user_item.xml (现在只是文字+单选按钮+自定义背景,可以根据需要随意扩展)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/search_user_list_item"
- android:layout_width="fill_parent"
- android:layout_height="30dp"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="10dp"
- android:background="@drawable/more_item_press"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/search_user_name"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_marginLeft="30dp"
- android:gravity="left"
- android:textColor="@android:color/black"
- android:textSize="16sp" />
- <RadioButton
- android:id="@+id/radio_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp" />
- </LinearLayout>
Listview就是用系统自带的
- <ListView
- android:id="@+id/search_user_list"
- android:layout_width="fill_parent"
- android:layout_height="200dp"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="5dp"
- android:paddingBottom="5dp"
- android:cacheColorHint="@android:color/transparent"
- android:divider="@null"
- android:listSelector="@android:color/transparent"
- android:visibility="gone" >
- </ListView>
再来是Adapter代码
SearchUserAdapter.java (具体改动写在代码注释里面)
- package ouc.sei.suxin.android.ui.adapter;
- import java.util.HashMap;
- import java.util.List;
- import ouc.sei.suxin.R;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.TextView;
- public class SearchUserAdapter extends BaseAdapter {
- private Context context;
- private List<String> userList;
- HashMap<String,Boolean> states=new HashMap<String,Boolean>();//用于记录每个RadioButton的状态,并保证只可选一个
- public SearchUserAdapter(Context context, List<String> userList)
- {
- this.context = context;
- this.userList= userList;
- }
- @Override
- public int getCount() {
- return userList.size();
- }
- @Override
- public Object getItem(int position) {
- return userList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- convertView = LayoutInflater.from(context).inflate(R.layout.search_user_item, null);
- holder = new ViewHolder();
- holder.background = (LinearLayout) convertView.findViewById(R.id.search_user_list_item);
- holder.userName = (TextView) convertView.findViewById(R.id.search_user_name);
- convertView.setTag(holder);
- }else{
- holder=(ViewHolder) convertView.getTag();
- }
- final RadioButton radio=(RadioButton) convertView.findViewById(R.id.radio_btn);
- holder.rdBtn = radio;
- holder.userName.setText(userList.get(position));
- //根据Item位置分配不同背景
- if(userList.size() > 0)
- {
- if(userList.size() == 1)
- {
- holder.background.setBackgroundResource(R.drawable.more_item_press);
- }
- else{
- if(position == 0){
- holder.background.setBackgroundResource(R.drawable.more_itemtop_press);
- }
- else if(position == userList.size()-1){
- holder.background.setBackgroundResource(R.drawable.more_itembottom_press);
- }
- else{
- holder.background.setBackgroundResource(R.drawable.more_itemmiddle_press);
- }
- }
- }
- //当RadioButton被选中时,将其状态记录进States中,并更新其他RadioButton的状态使它们不被选中
- holder.rdBtn.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- //重置,确保最多只有一项被选中
- for(String key:states.keySet()){
- states.put(key, false);
- }
- states.put(String.valueOf(position), radio.isChecked());
- SearchUserAdapter.this.notifyDataSetChanged();
- }
- });
- boolean res=false;
- if(states.get(String.valueOf(position)) == null || states.get(String.valueOf(position))== false){
- res=false;
- states.put(String.valueOf(position), false);
- }
- else
- res = true;
- holder.rdBtn.setChecked(res);
- return convertView;
- }
- static class ViewHolder {
- LinearLayout background;
- TextView userName;
- RadioButton rdBtn;
- }
List适配代码(与一般无异):
- adapter = new SearchUserAdapter(this, searchUserList);
- searchUserLV.setAdapter(adapter);
- searchUserLV.setVisibility(View.VISIBLE);
- setListViewHeightBasedOnChildren(searchUserLV);
这里还根据内容动态设置了一下,具体函数如下:
- public void setListViewHeightBasedOnChildren(ListView listView) {
- Adapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
- int totalHeight = 0;
- int viewCount = listAdapter.getCount();
- for (int i = 0; i < viewCount; i++) {
- View listItem = listAdapter.getView(i, null, listView);
- listItem.measure(0, 0);
- totalHeight += listItem.getMeasuredHeight();
- }
- ViewGroup.LayoutParams params = listView.getLayoutParams();
- params.height = totalHeight
- + (listView.getDividerHeight() * (listAdapter.getCount()-1)) + 10;//加10是为了适配自定义背景
- listView.setLayoutParams(params);
- }
当需要获取ListView中RadioButton的选择状态时,可以直接看Adapter中的states,具体如下:
- // 根据RadioButton的选择情况确定用户名
- for (int i = 0, j = searchUserLV.getCount(); i < j; i++) {
- View child = searchUserLV.getChildAt(i);
- RadioButton rdoBtn = (RadioButton) child
- .findViewById(R.id.radio_btn);
- if (rdoBtn.isChecked())
- searchUser = searchUserList.get(i);
- }
这里的searchUserList是调用后台服务获取的用户名列表,通过states获取选中用户名进行后续操作
效果图:
版权声明:本文为博主原创文章,未经博主允许不得转载。
【转】ListView与RadioButton组合——自定义单选列表的更多相关文章
- ListView与RadioButton组合——自定义单选列表
标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报 分类: Android(19) 版权声明: ...
- ListView与CheckBox组合实现单选
main.xml配置文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Android开发技巧——自定义单选或多选的ListView
这篇其实应该是属于写自定义单选或多选的ListView的基础教程,无奈目前许多人对此的实现大多都绕了远路,反而使得这正规的写法倒显得有些技巧性了. 本文原创,转载请注明在CSDN上的出处: http: ...
- MVC应用程序与单选列表
原文:MVC应用程序与单选列表 前几天,Insus.NET有在MVC应用程序中,练习了<MVC应用程序,动态创建单选列表(RadioButtonList)>http://www.cnblo ...
- ListView 自己定义BaseAdapter实现单选打勾(无漏洞)
(假设须要完整demo,请评论留下邮箱) (眼下源代码已经不发送.假设须要源代码,加qq316701116.不喜勿扰) 近期由于一个项目的原因须要自己定义一个BaseAdapter实现ListVIew ...
- (转载)Android自定义标签列表控件LabelsView解析
Android自定义标签列表控件LabelsView解析 作者 donkingliang 关注 2017.03.15 20:59* 字数 759 阅读 406评论 0喜欢 3 无论是在移动端的App, ...
- ylbtech-数据库设计与优化-对作为复选框/单选列表的集合表的设计
ylbtech-DatabaseDesgin:ylbtech-数据库设计与优化-对作为复选框/单选列表的集合表的设计 -- DatabaseName:通用表结构-- -- 主要是针对将要设计的表对象, ...
- FastScroll(1)ListView打开FastScroll及自定义它的样式
打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...
- 自定义SharePoint列表新增、编辑、查看页面(NewForm、EditForm、DispForm)
转:http://blog.csdn.net/lance_lot1/article/details/7966571 在项目中,用户需求涉及在一个列表录入项目信息,选择一个项目后,与该项目相关的信息实现 ...
随机推荐
- [Angular 2] Passing data to components with @Input
@Input allows you to pass data into your controller and templates through html and defining custom p ...
- Lucene中string docvalues使用utf-16的优化
原来的string docvalues使用utf-8编码,载入时转码花费大量时间,我们把转码实现从new String(bytes, "UTF-8")改用lucene的bytesR ...
- 用PHP迭代器来实现一个斐波纳契数列(转)
斐波纳契数列通常做法是用递归实现,当然还有其它的方法.这里现学现卖,用PHP的迭代器来实现一个斐波纳契数列,几乎没有什么难度,只是把类里的next()方法重写了一次.注释已经写到代码中,也是相当好理解 ...
- java中的泛型(转)
什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个 ...
- iOS多线程编程之GCD的使用
什么是线程呢? 1个CPU执行的CPU命令列为一条无分叉的路径即为线程. 这种无分叉路径不止1条,存在多条时即为多线程. 什么是GCD? Grand Central Dispatch (GCD)是异步 ...
- jquery ajax 提交表单(file && input)
用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...
- JAVA中的finalize()方法
[转]JAVA中的finalize()方法 今天早上看Thinking in java的[第四章 初始化和清除].[ 清除:终结和垃圾回收]的时候, 看到了这个东西. 用于清理滴... 当然,这个方 ...
- linux File Handling commands 'ls'.
ref:Linux / Unix Command: ls NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DES ...
- iOS开发网络资源整理-持续更新
本文记录iOS开发相关的网络社区和博客 1.objc中国 网址:http://objccn.io 简介:onevcat创建,项目的成立源于国内 Objective-C 社区对 objc.io 的翻译活 ...
- 六、C# 派生
派生 对一个现有的类型进行扩展,以便添加更多的功能,或者对现有的类型的操作进行重写. 比如可以将两个类都适用的方法和属性,用一个新的类进行重构,两个类再分别继承这个类. 定义一个派生类时,要在 ...