分类:
Android(19)
版权声明:本文为博主原创文章,未经博主允许不得转载。
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获取选中用户名进行后续操作
效果图:
- 顶
- 0
- 踩
- 0
- 猜你在找
Android开发listview中嵌套checkBox复选框实现单选全选删除列表内容Android 使用CheckBox实现ListView自定义单选Android的MasterDetail风格界面中实现自定义ListView的单选VC单选按钮控件Radio Button用法转VC单选按钮控件Radio Button用法转
查看评论
- 6楼 xuan237812939 2015-12-29 18:58发表 [回复]
- 楼主思路很好
- 5楼 superyuanzhe 2015-12-26 19:29发表 [回复]
- 赞
- 4楼 chuanglio 2015-10-27 14:34发表 [回复]
- 当获取ListView中RadioButton的选择状态时循环到看不见的行数
RadioButton rdoBtn = (RadioButton) child .findViewById(R.id.radio_btn);
会出现空指针
- 1楼 duduzaidudumei 2014-03-26 15:35发表 [回复]
- 关于状态的判断,不需要保存全部位置的状态,可以只保存一个值,被选中的position,当重新刷新view的时候判断map是否包含当前的position即可。这样数据比较多的时候,节省遍历的消耗。
- Re: KANGOD_KING 2015-09-16 11:47发表 [回复]
- 回复duduzaidudumei:确实。
不过也用不到map了,只用一个int就行。
holder.radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCheckedPosition = position;
notifyDataSetChanged();
}
});
重新刷新view的时候holder.radioButton.setChecked(position == mCheckedPosition);
http://blog.csdn.net/checkin001/article/details/11519131