我是
靠谱客的博主
会撒娇饼干,最近开发中收集的这篇文章主要介绍
checkbox选中的值在翻屏时会跳来跳去,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
其实就是红色的地方是关键点,用了一个集合来保存cb的选择状态,其它的关于viewHolder和和inflate的布局什么就不说了
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;
importandroid.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewCheckboxActivity extendsActivity implements OnCheckedChangeListener {
private ListView listView;
privateList<Map<String,Object>> 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 = newListViewAdapter(this, list);
listView.setAdapter(adapter);
}
privateList<Map<String,Object>> buildList() {
List<Map<String,Object>> list = newArrayList<Map<String,Object>>();
for (int i = 0; i< 20; i++) {
Map<String, Object> map = newHashMap<String, Object>();
map.put("icon", R.drawable.icon);
map.put("title", "主题" + (i + 1));
list.add(map);
}
return list;
}
class ListViewAdapter extends BaseAdapter {
private Context context;
privateList<Map<String,Object>> list;
private LayoutInflaterinflater;
privateList<Boolean> checkBoxesStatus;
public ListViewAdapter(Contextcontext, List<Map<String,Object>> list) {
super();
this.context =context;
this.list =list;
int listsize =list.size();
checkBoxesStatus = newArrayList<Boolean>(listsize);
for(int i =0;i<list.size();i++){
checkBoxesStatus.add(false);
}
this.inflater= LayoutInflater.from(context);
}
publicList<Map<String,Object>> getList() {
returnlist;
}
publicList<Boolean> getCheckBoxesStatus(){
returncheckBoxesStatus;
}
@Override
public int getCount() {
returnlist.size();
}
@Override
public Object getItem(intposition) {
returnlist.get(position);
}
@Override
public long getItemId(intposition) {
returnposition;
}
@Override
public View getView(final intposition, View convertView, ViewGroup parent) {
ViewHolderviewHolder = null;
Map<String, Object> map =list.get(position);
BooleancheckBoxStatus = 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(newOnCheckedChangeListener() {
@Override
public voidonCheckedChanged(CompoundButton buttonView, boolean isChecked){
checkBoxesStatus.set(buttonView.getId(), isChecked);
notifyDataSetChanged();
}
});
returnconvertView;
}
}
static class ViewHolder {
private ImageView img;
private TextView tv;
private CheckBox cb;
}
@Override
public void onCheckedChanged(CompoundButtonbuttonView, boolean isChecked){
List<Boolean> checkBoxesStatus =adapter.getCheckBoxesStatus();
for(int i =0;i<list.size();i++){
checkBoxesStatus.set(i, isChecked);
}
adapter.notifyDataSetChanged();
}
最后
以上就是会撒娇饼干为你收集整理的checkbox选中的值在翻屏时会跳来跳去的全部内容,希望文章能够帮你解决checkbox选中的值在翻屏时会跳来跳去所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复