Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中ListView + CheckBox實現單選、多選效果

Android中ListView + CheckBox實現單選、多選效果

編輯:關於Android編程

還是先來看看是不是你想要的效果:

不廢話,直接上代碼,很簡單,代碼裡都有注釋

1 單選

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:

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;
 }
}

多選:

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:

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實現單選、多選效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved