标签:
public class Bean {
private boolean isChecked;
private String msg = "这是一条测试数据";
public Bean(){
}
public Bean(boolean isCheched) {
this.isChecked = isCheched;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
然后在getView的时候判断状态
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView == null){
convertView = mInfater.inflate(R.layout.list_item, null);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mTextView.setText(mDatas.get(position).getMsg());
if(mDatas.get(position).isChecked()){//状态选中
mViewHolder.mCheckBox.setChecked(true);
}else{
mViewHolder.mCheckBox.setChecked(false);
}
return convertView;
}
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ListAdapter mAdapter;
private List<Bean> mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.main_listView);
mDatas = new ArrayList<Bean>();
for (int i = 0; i < 20; i++) {
if(i == 0){//默认选中第一个
mDatas.add(new Bean(true));
continue;
}
mDatas.add(new Bean(false));
}
mAdapter = new ListAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);
}
private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
for (Bean bean : mDatas) {//全部设为未选中
bean.setChecked(false);
}
mDatas.get(position).setChecked(true);//点击的设为选中
mAdapter.notifyDataSetChanged();
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/main_list_item_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="@android:color/black"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<!-- checkBox获取事件的优先级大于listView故需设置focusable = false -->
<CheckBox
android:id="@+id/main_list_item_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:backgroundTintMode="multiply"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"
/>
</LinearLayout>
这种办法不推荐使用
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ListAdapter mAdapter;
private List<Bean> mDatas;
public static int pos;//记录选中的position
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.main_listView);
mDatas = new ArrayList<Bean>();
for (int i = 0; i < 20; i++) {
if(i == 0){
mDatas.add(new Bean(true));
continue;
}
mDatas.add(new Bean(false));
}
mAdapter = new ListAdapter(this, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(mOnItemClickListener);
}
private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
pos = position;
mAdapter.notifyDataSetChanged();
}
};
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView == null){
convertView = mInfater.inflate(R.layout.list_item, null);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mTextView.setText(mDatas.get(position).getMsg());
if(MainActivity.pos == position){//MainActivity的pos是否等于position
mViewHolder.mCheckBox.setChecked(true);
}else{
mViewHolder.mCheckBox.setChecked(false);
}
return convertView;
}
标签:
原文地址:http://blog.csdn.net/xuyonghong1122/article/details/51362674