Android中ListView + CheckBox实现单选、多选效果
更新时间:2017年02月09日 15:33:21 作者:哎呀小嘿
这篇文章主要介绍了Android中ListView + CheckBox实现单选、多选效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
还是先来看看是不是你想要的效果:
不废话,直接上代码,很简单,代码里都有注释
1 单选
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class SingleActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> groups; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_single); listView = (ListView) this .findViewById(R.id.lvGroup); groups = new ArrayList<String>(); groups.add( "11" ); groups.add( "22" ); groups.add( "33" ); groups.add( "44" ); groups.add( "55" ); groups.add( "66" ); groups.add( "77" ); AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤 SingleAdapter.ViewHolder viewHolder = (SingleAdapter.ViewHolder) view.getTag(); viewHolder.cb.toggle(); // 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中 } }; SingleAdapter adapter = new SingleAdapter( this , groups); listView.setAdapter(adapter); listView.setOnItemClickListener(listItemClickListener); } } |
Adapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | public class SingleAdapter extends BaseAdapter { private Activity activity; //上下文 private ArrayList<String> list; private LayoutInflater inflater = null ; //导入布局 private int temp = - 1 ; public SingleAdapter(Activity context, ArrayList<String> list) { this .activity = context; this .list = list; inflater = LayoutInflater.from(context); } @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; } //listview每显示一行数据,该函数就执行一次 @Override public View getView( int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; if (convertView == null ) { //当第一次加载ListView控件时 convertView为空 convertView = inflater.inflate(R.layout.group_item_view, null ); //所以当ListView控件没有滑动时都会执行这条语句 holder = new ViewHolder(); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); convertView.setTag(holder); //为view设置标签 } else { //取出holder holder = (ViewHolder) convertView.getTag(); } //设置list的textview显示 holder.tv.setTextColor(Color.WHITE); holder.tv.setText(list.get(position)); // 根据isSelected来设置checkbox的选中状况 holder.cb.setId(position); //对checkbox的id进行重新设置为当前的position holder.cb.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { if (isChecked) { //实现checkbox的单选功能,同样适用于radiobutton if (temp != - 1 ) { //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉 CheckBox tempCheckBox = (CheckBox) activity.findViewById(temp); if (tempCheckBox != null ) tempCheckBox.setChecked( false ); } temp = compoundButton.getId(); //保存当前选中的checkbox的id值 } } }); if (position == temp) //比对position和当前的temp是否一致 holder.cb.setChecked( true ); else holder.cb.setChecked( false ); return convertView; } public static class ViewHolder { TextView tv; CheckBox cb; } } |
多选:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class MulActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> groups; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_mul); listView = (ListView) this .findViewById(R.id.list); groups = new ArrayList<>(); groups.add( "11" ); groups.add( "22" ); groups.add( "33" ); groups.add( "44" ); groups.add( "55" ); groups.add( "66" ); groups.add( "77" ); AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤 MulAdapter.ViewHolder viewHolder = (MulAdapter.ViewHolder) view.getTag(); viewHolder.cb.toggle(); // 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中 MulAdapter.getIsSelected().put(position, viewHolder.cb.isChecked()); //将CheckBox的选中状况记录下来 } }; MulAdapter adapter = new MulAdapter( this , groups); listView.setAdapter(adapter); listView.setOnItemClickListener(listItemClickListener); } } |
Adapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | public class MulAdapter extends BaseAdapter { private Context context; //上下文 private ArrayList<String> list; //控制CheckBox选中情况 private static HashMap<Integer, Boolean> isSelected; private LayoutInflater inflater = null ; //导入布局 public MulAdapter(Context context, ArrayList<String> list) { this .context = context; this .list = list; inflater = LayoutInflater.from(context); isSelected = new HashMap<Integer, Boolean>(); initData(); } private void initData() { //初始化isSelected的数据 for ( int i = 0 ; i < list.size(); i++) { getIsSelected().put(i, false ); } } @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; } //listview每显示一行数据,该函数就执行一次 @Override public View getView( int position, View convertView, ViewGroup parent) { ViewHolder holder = null ; if (convertView == null ) { //当第一次加载ListView控件时 convertView为空 convertView = inflater.inflate(R.layout.group_item_view, null ); //所以当ListView控件没有滑动时都会执行这条语句 holder = new ViewHolder(); holder.tv = (TextView) convertView.findViewById(R.id.item_tv); holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb); convertView.setTag(holder); //为view设置标签 } else { //取出holder holder = (ViewHolder) convertView.getTag(); //the Object stored in this view as a tag } //设置list的textview显示 holder.tv.setTextColor(Color.WHITE); holder.tv.setText(list.get(position)); // 根据isSelected来设置checkbox的选中状况 holder.cb.setChecked(getIsSelected().get(position)); return convertView; } public static class ViewHolder { TextView tv; CheckBox cb; } public static HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer, Boolean> isSelected) { MulAdapter.isSelected = isSelected; } } |
以上所述是小编给大家介绍的Android中ListView + CheckBox实现单选、多选效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Android中的RecyclerView新组件初步上手指南
RecyclerView是Android L版本开始采用的一个组件,被人们认为用来代替传统的ListView,下面我们就一起来看一下Android中的RecyclerView新组件初步上手指南2016-06-06
最新评论