1–控件要实现单选,首先要让控件实现Checkable接口,并继承某一个Layout。
控件的原始代码:
package com.example.chencklist;
import android.content.Context;
import android.view.View;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;
public class SingleView extends FrameLayout implements Checkable{
TextView tv_item;
RadioButton rb_item;
public SingleView(Context context) {
super(context);
View.inflate(context, R.layout.item_check_view, this);
tv_item = (TextView) findViewById(R.id.tv_item);
rb_item = (RadioButton) findViewById(R.id.rb_item);
}
public void setText(String str) {
tv_item.setText(str);
}
@Override
public void setChecked(boolean checked) {
rb_item.setChecked(checked);
}
@Override
public boolean isChecked() {
return rb_item.isChecked();
}
@Override
public void toggle() {
rb_item.toggle();
}
}
2–R.layout.item_check_view的xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rb_item"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="@+id/tv_item"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="name" />
</RelativeLayout>
注意,以上的这三个属性必须为false:
android:clickable=”false”
android:focusable=”false”
android:focusableInTouchMode=”false”
否则,布局中的radiobutton可以被点击,并且获得焦点;当点击item的其他地方是正常的,但是若点击item中的radiobutton时,则会和item的点击事件发生冲突,使得item点击事件失效,视觉上会造成多个radiobutton被选中的状态。
3–自定义的adapter,继承自ArrayAdapter。
因为源生代码中,ListView的ListView.CHOICE_MODE_SINGLE属性(单选模式)是结合ArrayAdapter使用的。
package com.example.chencklist;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class MyArrayAdapter extends ArrayAdapter<Bean>{
private Context context;
public MyArrayAdapter(Context context, int resource, List<Bean> objects) {
super(context, resource, objects);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SingleView view = null;
if (convertView == null) {
view = new SingleView(context);
}else {
view = (SingleView) convertView;
}
view.setText(getItem(position).getName());
return view;
}
}
4–自定义的测试实体类Bean
package com.example.chencklist;
import java.io.Serializable;
public class Bean implements Serializable{
private static final long serialVersionUID = -4788371797955109344L;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5–MainActivity中的代码:
package com.example.chencklist;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
ListView lv;
List<Bean> beans = new ArrayList<Bean>();
Bean checkedBean = new Bean();//被选中的Bean
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_main);
lv = (ListView) findViewById(R.id.lv_main);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//设置单选模式,也可以在xml中直接设置
}
@Override
protected void onResume() {
setBeans();
ListAdapter adapter = new MyArrayAdapter(MainActivity.this, R.layout.item_check_view, beans);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//注意要对被选择的Bean做非空判断
checkedBean = beans.get(position);//获取到被选择的Bean
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//拿到被选择的Bean
Toast.makeText(MainActivity.this, checkedBean.getName(), Toast.LENGTH_SHORT).show();
}
});
super.onResume();
}
//数据添加
private void setBeans() {
Bean bean1 = new Bean();
bean1.setName("name1");
Bean bean2 = new Bean();
bean2.setName("name2");
Bean bean3 = new Bean();
bean3.setName("name3");
Bean bean4 = new Bean();
bean4.setName("name4");
beans.add(bean1);
beans.add(bean2);
beans.add(bean3);
beans.add(bean4);
}
}
6–MainActivity的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chencklist.MainActivity" >
<Button
android:id="@+id/btn_main"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="button" />
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
以上,实现ListView的单选功能。