前段项目设计的时候,里面有个如图一所示的选择,当时看见他,我的第一反应就是用一个gridview或者listview,里面用一个checkbox作为item来考虑。结果到后面处理的时候发现这样的checkbox不能作为单选,所以经过我查资料等,想尽一切办法去处理总于解决这个问题了。现在把自己的思路和demo共享出来,请大家指正:(第一次发帖)

首先:activity_main.xml代码:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<gridview
android:id="@+id/activity_main_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
android:paddingLeft="10dp"
android:scrollbars="none" />
</gridview
</textview
item里面的内容:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<checkbox
android:id="@+id/all_class"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:text="全部"
android:textStyle="normal"
android:textSize="12dp"
style="@style/CustomCheckboxTheme"
/>
</checkbox
activity里面的内容:
private List list;
private GridView gird;
private CheckBoxAdapter adapter;
HashMap isSelectTy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化组件
*/
private void initView() {
setList();
gird = (GridView) MainActivity.this.findViewById(R.id.activity_main_grid);
adapter = new CheckBoxAdapter(MainActivity.this, list, isSelectTy, "求职");
gird.setAdapter(adapter);
}
/**
* 初始化list,添加数据,初始化checkbox的值
*/
private void setList(){
list = new ArrayList();
if(list.size() == 0){
list.add("求职");
list.add("实习");
list.add("返家");
list.add("培训");
list.add("旅游");
list.add("病假");
list.add("事假");
}
isSelectTy = new HashMap();
isSelectTy.put(0, false);
isSelectTy.put(1, false);
isSelectTy.put(2, false);
isSelectTy.put(3, false);
isSelectTy.put(4, false);
isSelectTy.put(5, false);
isSelectTy.put(6, false);
}
adapter代码如下:package com.example.checkboxonlyselect;
public class CheckBoxAdapter extends BaseAdapter{
public static int temp = -1;
Activity context;
private List list;
private String str;
HashMap isSelect;
public CheckBoxAdapter(Activity context, List list,HashMap isSelect,String str){
this.context = context;
this.list = list;
this.isSelect=isSelect;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.checkbox_textview, null);
holder = new Holder();
holder.tvContent = (CheckBox) convertView.findViewById(R.id.all_class);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
// holder.tvContent.setId(position);
holder.tvContent.setChecked(isSelect.get(position));
holder.tvContent.setText(list.get(position));
if(list.get(position).equals(str)){
holder.tvContent.setChecked(true);
}
holder.tvContent.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isSelect.put(position, isChecked);
if(buttonView.isChecked())
{
for(int i=0;i<list.size();i++)
{
//把其他的checkbox设置为false
if(i!=position){
isSelect.put(i, false);
}
}
}
//通知适配器更改
CheckBoxAdapter.this.notifyDataSetChanged();
}
});
if (temp == position) {
holder.tvContent.setChecked(true);
}
return convertView;
}
class Holder {
CheckBox tvContent;
}
public void notifyDataSetChanged(String str) {
this.str = str;
this.notifyDataSetChanged();
}
}
思路就是,我点击里面的某一个checkbox的时候,就把for(int i=0;i<list.size();i++)
{
//把其他的checkbox设置为false
if(i!=position){
isSelect.put(i, false);
}
} 就把不是对应位置的选中全部设置为false,在根据是否选中或取消进行设置当前位置的选中状态。在刷新adapter里面的数据,里面用一个temp进行处理判断设置当前是否设置为true。



首先:activity_main.xml代码:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<gridview
android:id="@+id/activity_main_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
android:paddingLeft="10dp"
android:scrollbars="none" />
</gridview
</textview
item里面的内容:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<checkbox
android:id="@+id/all_class"
android:layout_width="60dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:text="全部"
android:textStyle="normal"
android:textSize="12dp"
style="@style/CustomCheckboxTheme"
/>
</checkbox
activity里面的内容:
private List list;
private GridView gird;
private CheckBoxAdapter adapter;
HashMap isSelectTy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化组件
*/
private void initView() {
setList();
gird = (GridView) MainActivity.this.findViewById(R.id.activity_main_grid);
adapter = new CheckBoxAdapter(MainActivity.this, list, isSelectTy, "求职");
gird.setAdapter(adapter);
}
/**
* 初始化list,添加数据,初始化checkbox的值
*/
private void setList(){
list = new ArrayList();
if(list.size() == 0){
list.add("求职");
list.add("实习");
list.add("返家");
list.add("培训");
list.add("旅游");
list.add("病假");
list.add("事假");
}
isSelectTy = new HashMap();
isSelectTy.put(0, false);
isSelectTy.put(1, false);
isSelectTy.put(2, false);
isSelectTy.put(3, false);
isSelectTy.put(4, false);
isSelectTy.put(5, false);
isSelectTy.put(6, false);
}
adapter代码如下:package com.example.checkboxonlyselect;
public class CheckBoxAdapter extends BaseAdapter{
public static int temp = -1;
Activity context;
private List list;
private String str;
HashMap isSelect;
public CheckBoxAdapter(Activity context, List list,HashMap isSelect,String str){
this.context = context;
this.list = list;
this.isSelect=isSelect;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.checkbox_textview, null);
holder = new Holder();
holder.tvContent = (CheckBox) convertView.findViewById(R.id.all_class);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
// holder.tvContent.setId(position);
holder.tvContent.setChecked(isSelect.get(position));
holder.tvContent.setText(list.get(position));
if(list.get(position).equals(str)){
holder.tvContent.setChecked(true);
}
holder.tvContent.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isSelect.put(position, isChecked);
if(buttonView.isChecked())
{
for(int i=0;i<list.size();i++)
{
//把其他的checkbox设置为false
if(i!=position){
isSelect.put(i, false);
}
}
}
//通知适配器更改
CheckBoxAdapter.this.notifyDataSetChanged();
}
});
if (temp == position) {
holder.tvContent.setChecked(true);
}
return convertView;
}
class Holder {
CheckBox tvContent;
}
public void notifyDataSetChanged(String str) {
this.str = str;
this.notifyDataSetChanged();
}
}
思路就是,我点击里面的某一个checkbox的时候,就把for(int i=0;i<list.size();i++)
{
//把其他的checkbox设置为false
if(i!=position){
isSelect.put(i, false);
}
} 就把不是对应位置的选中全部设置为false,在根据是否选中或取消进行设置当前位置的选中状态。在刷新adapter里面的数据,里面用一个temp进行处理判断设置当前是否设置为true。