博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 实现ListView中添加RaidoButton单选
阅读量:6565 次
发布时间:2019-06-24

本文共 5032 字,大约阅读时间需要 16 分钟。

hot3.png

import android.app.Activity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.TextView;

public class RecoverListAdapter extends BaseAdapter {

private LayoutInflater inflater;

int[] backup_record_item_image;

String[] backup_record_item_time;

String[] backup_record_item_to;

Activity activity;

private int temp = -1;

public RecoverListAdapter(Activity context, int[] backup_record_item_image,

String[] backup_record_item_time, String[] backup_record_item_to) {

this.inflater = LayoutInflater.from(context);

this.backup_record_item_image = backup_record_item_image;

this.backup_record_item_time = backup_record_item_time;

this.backup_record_item_to = backup_record_item_to;

this.activity = context;

}

@Override

public int getCount() {

return backup_record_item_time.length;

}

@Override

public Object getItem(int position) {

return position;

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {

holder = new ViewHolder();

convertView = this.inflater.inflate(R.layout.general_recover_list_item, null);

holder.iv = (ImageView) convertView.findViewById(R.id.backup_record_item_image);

holder.tv_time = (TextView) convertView.findViewById(R.id.backup_record_item_time);

holder.tv_to = (TextView) convertView.findViewById(R.id.backup_record_item_to);

holder.radioButton = (RadioButton) convertView

.findViewById(R.id.backup_record_item_btn);

holder.radioButton.setChecked(false);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

holder.iv.setImageResource(backup_record_item_image[position]);

holder.tv_time.setText(backup_record_item_time[position]);

holder.tv_to.setText(backup_record_item_to[position]);

//黑体部分为实现单选功能部分

holder.radioButton.setId(position);

holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

if (temp != -1) {

RadioButton tempButton = (RadioButton) activity.findViewById(temp);

if (tempButton != null) {

tempButton.setChecked(false);

}

}

temp = buttonView.getId();

}

}

});

if (position == temp) {

holder.radioButton.setChecked(true);

} else {

holder.radioButton.setChecked(false);

}

return convertView;

}

private class ViewHolder {

ImageView iv;

TextView tv_time;

TextView tv_to;

RadioButton radioButton;

}

}

general_recover_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:id="@+id/backup_record_item">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon"
android:layout_alignParentLeft="true"
android:id="@+id/backup_record_item_image"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/backup_record_item_image"
android:id="@+id/backup_record_item_txt">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="时间"
android:layout_alignTop="@+id/imageView1"
android:layout_alignBottom="@+id/imageView1"
android:id="@+id/backup_record_item_time"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="备份联系人 到 "
android:layout_toRightOf="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="手机/存储卡"
android:layout_toRightOf="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
android:id="@+id/backup_record_item_to" />
</LinearLayout>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/backup_record_item_btn" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text=""
android:id="@+id/backup_record_item_check" />
</RelativeLayout>
</LinearLayout>
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/recover_items">
<ListView
android:id="@+id/recover_list"
android:cacheColorHint="#00000000"
android:layout_height="match_parent"
android:layout_width="fill_parent"/>
<View
android:id="@+id/recover_footer"
android:layout_width="fill_parent"
android:layout_height="40dip" />
</LinearLayout>

转载于:https://my.oschina.net/u/246578/blog/63252

你可能感兴趣的文章
检查IP是否可用的方法
查看>>
BFC的一些探究
查看>>
网易微专业之《前端开发工程师》学习笔记(1)
查看>>
记一次MySQL字符集冲突导致的报错
查看>>
jenkins 集成 docker tomcat 自动化测试脚本
查看>>
pt,px,rem和em之间区别总结
查看>>
浏览器同源策略以及跨域请求时可能遇到的问题
查看>>
自己开发的MarkDown在线编辑器越来越完善了,心里感到高兴。
查看>>
ContentObserver类的使用
查看>>
可想实现一个自己的简单jQuery库?(五)
查看>>
庖丁解牛迭代器,聊聊那些藏在幕后的秘密
查看>>
【LintCode: 3. 统计数字】算法题解析
查看>>
pycrypto加密文件
查看>>
linux上安装redis
查看>>
Java使用POI读取和写入Excel指南
查看>>
偷看日历?9款 APP 涉嫌过度获取权限
查看>>
使用java9的uuid生成方式,让uuid生成速度提升一个档次 ...
查看>>
互联网架构师必备技术 Docker仓库与Java应用服务动态发布那些事
查看>>
Shell---判断(if)和分支(case)
查看>>
SpringBoot(十五)_springboot实现预览pdf
查看>>