ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去

阅读更多
废话不多说,直接上图上代码

翻屏前选择了2,3,4,5
ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第1张图片

翻屏后
ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第2张图片


向上翻屏,选择的还是2,3,4,5
ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第3张图片


其实就是红色的地方是关键点,用了一个集合来保存cb的选择状态,其它的关于viewHolder和和inflate的布局什么就不说了


[code="Activity界面"]
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewCheckboxActivity extends Activity implements OnCheckedChangeListener {

	private ListView listView;
	private List> list;
	private CheckBox cball;
	ListViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		listView = (ListView) findViewById(R.id.lv);
		cball = (CheckBox) findViewById(R.id.cball);
		cball.setOnCheckedChangeListener(this);//全选
		
		list = buildList();
		adapter = new ListViewAdapter(this, list);
		listView.setAdapter(adapter);
	}

	private List> buildList() {
		List> list = new ArrayList>();
		for (int i = 0; i  map = new HashMap();
			map.put("icon", R.drawable.icon);
			map.put("title", "主题" + (i + 1));
			list.add(map);
		}
		return list;
	}

	class ListViewAdapter extends BaseAdapter {
		private Context context;
		private List> list;
		private LayoutInflater inflater;
		private List checkBoxesStatus;

		
		public ListViewAdapter(Context context, List> list) {
			super();
			this.context = context;
			this.list = list;
			int listsize = list.size();
			checkBoxesStatus = new ArrayList(listsize);
			for(int i = 0;i> getList() {
			return list;
		}
		public List getCheckBoxesStatus() {
			return checkBoxesStatus;
		}
		@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) {
			
			ViewHolder viewHolder = null;
			Map map = list.get(position);
			Boolean checkBoxStatus = checkBoxesStatus.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.list_item, null);
				viewHolder = new ViewHolder();
				viewHolder.img = (ImageView) convertView.findViewById(R.id.icon);
				viewHolder.tv = (TextView) convertView.findViewById(R.id.title);
				viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			viewHolder.img.setImageResource((Integer) map.get("icon"));
			viewHolder.tv.setText((String) map.get("title"));
			
			viewHolder.cb.setId(position);
			viewHolder.cb.setChecked(checkBoxStatus);
			viewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					checkBoxesStatus.set(buttonView.getId(), isChecked);
					notifyDataSetChanged();
				}
			});
			return convertView;
		}
	}
	
	static class ViewHolder {
		private ImageView img;
		private TextView tv;
		private CheckBox cb;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
		List checkBoxesStatus = adapter.getCheckBoxesStatus();
		for(int i = 0;i
  • ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第4张图片
  • 大小: 67.4 KB
  • ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第5张图片
  • 大小: 70 KB
  • ListView的bug解决,如果有checkbox的话,选中的值在翻屏时会跳来跳去_第6张图片
  • 大小: 67.2 KB
  • ListViewWithCheckbox.rar (62.7 KB)
  • 下载次数: 602
  • 查看图片附件

你可能感兴趣的:(Android,ListView,Checkbox,跳屏)