前言
ListView帶CheckBox實(shí)現(xiàn)單選在Android開發(fā)中會(huì)經(jīng)常用到若锁,通常的做法就是用一個(gè)bean記錄CheckBox的狀態(tài),每次getView時(shí)去判斷這個(gè)bean的狀態(tài)斧吐。
演示如下:
實(shí)例代碼:
Person:java bean數(shù)據(jù)
public class Person implements Serializable{
private String Title ; //每條item的數(shù)據(jù)
private boolean isChecked; //每條item的狀態(tài)
public Person(String title){
Title = title;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
}
MainActivity:在OnItemClickListener通過Person中的isChecked狀態(tài)來控制CheckBox是否選中又固。
public class MainActivity extends AppCompatActivity {
@Bind(R.id.lv_main)
ListView mListView;//listview控件
List<Person> datas = new ArrayList();//數(shù)據(jù)集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ButterKnife.bind(this);
//創(chuàng)建數(shù)據(jù)
for(int i = 0;i<20;i++){
datas.add(new Person("張三"+i));
}
final MyAdapter myAdapter = new MyAdapter(this);
myAdapter.setDatas(datas);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
int currentNum = -1;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(Person person : datas){ //遍歷list集合中的數(shù)據(jù)
person.setChecked(false);//全部設(shè)為未選中
}
if(currentNum == -1){ //選中
datas.get(position).setChecked(true);
currentNum = position;
}else if(currentNum == position){ //同一個(gè)item選中變未選中
for(Person person : datas){
person.setChecked(false);
}
currentNum = -1;
}else if(currentNum != position){ //不是同一個(gè)item選中當(dāng)前的,去除上一個(gè)選中的
for(Person person : datas){
person.setChecked(false);
}
datas.get(position).setChecked(true);
currentNum = position;
}
Toast.makeText(parent.getContext(),datas.get(position).getTitle(),Toast.LENGTH_SHORT).show();
myAdapter.notifyDataSetChanged();//刷新adapter
}
});
}
}
MyAdapter:在Adapter適配器中貼出了重要的getView代碼煤率,在getView中根據(jù)position位置狀態(tài)及javaBean做狀態(tài)判斷仰冠。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//listView優(yōu)化部分
ViewHolder mViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_checkbox, parent, false);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
}else {
mViewHolder = (ViewHolder) convertView.getTag();
}
//判斷CheckBox的狀態(tài)
if(mDatas.get(position).isChecked()){
mViewHolder.mCbCheckbox.setChecked(true);//選中
}else {
mViewHolder.mCbCheckbox.setChecked(false);//未選中
}
mViewHolder.mTvTitle.setText(mDatas.get(position).getTitle());
return convertView;
}
//listview優(yōu)化使用的holder
static class ViewHolder {
@Bind(R.id.tv_title)
TextView mTvTitle;
@Bind(R.id.cb_checkbox)
CheckBox mCbCheckbox;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
Item_checkbox.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="match_parent">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="我是誰"/>
<CheckBox
android:id="@+id/cb_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>