查看: 1145|回复: 0

[手机开发] Android中ListView + CheckBox实现单选、多选效果

发表于 2017-8-1 08:00:04

还是先来看看是不是你想要的效果:

不废话,直接上代码,很简单,代码里都有注释

1 单选

  1. public class SingleActivity extends AppCompatActivity {
  2. private ListView listView;
  3. private ArrayList<String> groups;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_single);
  8. listView = (ListView) this.findViewById(R.id.lvGroup);
  9. groups = new ArrayList<String>();
  10. groups.add("11");
  11. groups.add("22");
  12. groups.add("33");
  13. groups.add("44");
  14. groups.add("55");
  15. groups.add("66");
  16. groups.add("77");
  17. AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() {
  18. @Override
  19. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  20. // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
  21. SingleAdapter.ViewHolder viewHolder = (SingleAdapter.ViewHolder) view.getTag();
  22. viewHolder.cb.toggle();// 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中
  23. }
  24. };
  25. SingleAdapter adapter = new SingleAdapter(this, groups);
  26. listView.setAdapter(adapter);
  27. listView.setOnItemClickListener(listItemClickListener);
  28. }
  29. }
复制代码

Adapter:

  1. public class SingleAdapter extends BaseAdapter {
  2. private Activity activity;//上下文
  3. private ArrayList<String> list;
  4. private LayoutInflater inflater = null;//导入布局
  5. private int temp = -1;
  6. public SingleAdapter(Activity context, ArrayList<String> list) {
  7. this.activity = context;
  8. this.list = list;
  9. inflater = LayoutInflater.from(context);
  10. }
  11. @Override
  12. public int getCount() {
  13. return list.size();
  14. }
  15. @Override
  16. public Object getItem(int position) {
  17. return list.get(position);
  18. }
  19. @Override
  20. public long getItemId(int position) {
  21. return position;
  22. }
  23. //listview每显示一行数据,该函数就执行一次
  24. @Override
  25. public View getView(int position, View convertView, ViewGroup parent) {
  26. ViewHolder holder = null;
  27. if (convertView == null) {//当第一次加载ListView控件时 convertView为空
  28. convertView = inflater.inflate(R.layout.group_item_view, null);//所以当ListView控件没有滑动时都会执行这条语句
  29. holder = new ViewHolder();
  30. holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
  31. holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
  32. convertView.setTag(holder);//为view设置标签
  33. } else {//取出holder
  34. holder = (ViewHolder) convertView.getTag();
  35. }
  36. //设置list的textview显示
  37. holder.tv.setTextColor(Color.WHITE);
  38. holder.tv.setText(list.get(position));
  39. // 根据isSelected来设置checkbox的选中状况
  40. holder.cb.setId(position);//对checkbox的id进行重新设置为当前的position
  41. holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  42. @Override
  43. public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
  44. if (isChecked) {//实现checkbox的单选功能,同样适用于radiobutton
  45. if (temp != -1) {
  46. //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
  47. CheckBox tempCheckBox = (CheckBox) activity.findViewById(temp);
  48. if (tempCheckBox != null)
  49. tempCheckBox.setChecked(false);
  50. }
  51. temp = compoundButton.getId();//保存当前选中的checkbox的id值
  52. }
  53. }
  54. });
  55. if (position == temp)//比对position和当前的temp是否一致
  56. holder.cb.setChecked(true);
  57. else
  58. holder.cb.setChecked(false);
  59. return convertView;
  60. }
  61. public static class ViewHolder {
  62. TextView tv;
  63. CheckBox cb;
  64. }
  65. }
复制代码

多选:

  1. public class MulActivity extends AppCompatActivity {
  2. private ListView listView;
  3. private ArrayList<String> groups;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_mul);
  8. listView = (ListView) this.findViewById(R.id.list);
  9. groups = new ArrayList<>();
  10. groups.add("11");
  11. groups.add("22");
  12. groups.add("33");
  13. groups.add("44");
  14. groups.add("55");
  15. groups.add("66");
  16. groups.add("77");
  17. AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener() {
  18. @Override
  19. public void onItemClick(AdapterView<?> parent, View view, int position,
  20. long id) {
  21. // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
  22. MulAdapter.ViewHolder viewHolder = (MulAdapter.ViewHolder) view.getTag();
  23. viewHolder.cb.toggle();// 把CheckBox的选中状态改为当前状态的反,gridview确保是单一选中
  24. MulAdapter.getIsSelected().put(position, viewHolder.cb.isChecked());//将CheckBox的选中状况记录下来
  25. }
  26. };
  27. MulAdapter adapter = new MulAdapter(this, groups);
  28. listView.setAdapter(adapter);
  29. listView.setOnItemClickListener(listItemClickListener);
  30. }
  31. }
复制代码

Adapter:

  1. public class MulAdapter extends BaseAdapter {
  2. private Context context;//上下文
  3. private ArrayList<String> list;
  4. //控制CheckBox选中情况
  5. private static HashMap<Integer, Boolean> isSelected;
  6. private LayoutInflater inflater = null;//导入布局
  7. public MulAdapter(Context context, ArrayList<String> list) {
  8. this.context = context;
  9. this.list = list;
  10. inflater = LayoutInflater.from(context);
  11. isSelected = new HashMap<Integer, Boolean>();
  12. initData();
  13. }
  14. private void initData() {//初始化isSelected的数据
  15. for (int i = 0; i < list.size(); i++) {
  16. getIsSelected().put(i, false);
  17. }
  18. }
  19. @Override
  20. public int getCount() {
  21. return list.size();
  22. }
  23. @Override
  24. public Object getItem(int position) {
  25. return list.get(position);
  26. }
  27. @Override
  28. public long getItemId(int position) {
  29. return position;
  30. }
  31. //listview每显示一行数据,该函数就执行一次
  32. @Override
  33. public View getView(int position, View convertView, ViewGroup parent) {
  34. ViewHolder holder = null;
  35. if (convertView == null) {//当第一次加载ListView控件时 convertView为空
  36. convertView = inflater.inflate(R.layout.group_item_view, null);//所以当ListView控件没有滑动时都会执行这条语句
  37. holder = new ViewHolder();
  38. holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
  39. holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
  40. convertView.setTag(holder);//为view设置标签
  41. } else {//取出holder
  42. holder = (ViewHolder) convertView.getTag();//the Object stored in this view as a tag
  43. }
  44. //设置list的textview显示
  45. holder.tv.setTextColor(Color.WHITE);
  46. holder.tv.setText(list.get(position));
  47. // 根据isSelected来设置checkbox的选中状况
  48. holder.cb.setChecked(getIsSelected().get(position));
  49. return convertView;
  50. }
  51. public static class ViewHolder {
  52. TextView tv;
  53. CheckBox cb;
  54. }
  55. public static HashMap<Integer, Boolean> getIsSelected() {
  56. return isSelected;
  57. }
  58. public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
  59. MulAdapter.isSelected = isSelected;
  60. }
  61. }
复制代码

以上所述是小编给大家介绍的Android中ListView + CheckBox实现单选、多选效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条