Android checkbox的listView(多選,全選,反選)具體實(shí)現(xiàn)方法
[html]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<LinearLayout
android:id="@+id/line"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/tv"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_selectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="全選" />
<Button
android:id="@+id/bt_cancleselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="反選" />
<Button
android:id="@+id/bt_deselectall"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:text="取消選擇" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/line" />
</RelativeLayout>
listView 的item布局文件:
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical" />
</LinearLayout>
Activity:
[java]
public class Ex_checkboxActivity extends Activity {
private ListView lv;
private MyAdapter mAdapter;
private ArrayList<String> list;
private Button bt_selectall;
private Button bt_cancel;
private Button bt_deselectall;
private int checkNum; // 記錄選中的條目數(shù)量
private TextView tv_show;// 用于顯示選中的條目數(shù)量
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 實(shí)例化各個(gè)控件 */
lv = (ListView) findViewById(R.id.lv);
bt_selectall = (Button) findViewById(R.id.bt_selectall);
bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
tv_show = (TextView) findViewById(R.id.tv);
list = new ArrayList<String>();
// 為Adapter準(zhǔn)備數(shù)據(jù)
initDate();
// 實(shí)例化自定義的MyAdapter
mAdapter = new MyAdapter(list, this);
// 綁定Adapter
lv.setAdapter(mAdapter);
// 全選按鈕的回調(diào)接口
bt_selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍歷list的長(zhǎng)度,將MyAdapter中的map值全部設(shè)為true
for (int i = 0; i < list.size(); i++) {
MyAdapter.getIsSelected().put(i, true);
}
// 數(shù)量設(shè)為list的長(zhǎng)度
checkNum = list.size();
// 刷新listview和TextView的顯示
dataChanged();
}
});
// 反選按鈕的回調(diào)接口
bt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍歷list的長(zhǎng)度,將已選的設(shè)為未選,未選的設(shè)為已選
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;
} else {
MyAdapter.getIsSelected().put(i, true);
checkNum++;
}
}
// 刷新listview和TextView的顯示
dataChanged();
}
});
// 取消按鈕的回調(diào)接口
bt_deselectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 遍歷list的長(zhǎng)度,將已選的按鈕設(shè)為未選
for (int i = 0; i < list.size(); i++) {
if (MyAdapter.getIsSelected().get(i)) {
MyAdapter.getIsSelected().put(i, false);
checkNum--;// 數(shù)量減1
}
}
// 刷新listview和TextView的顯示
dataChanged();
}
});
// 綁定listView的監(jiān)聽器
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// 取得ViewHolder對(duì)象,這樣就省去了通過層層的findViewById去實(shí)例化我們需要的cb實(shí)例的步驟
ViewHolder holder = (ViewHolder) arg1.getTag();
// 改變CheckBox的狀態(tài)
holder.cb.toggle();
// 將CheckBox的選中狀況記錄下來
MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
// 調(diào)整選定條目
if (holder.cb.isChecked() == true) {
checkNum++;
} else {
checkNum--;
}
// 用TextView顯示
tv_show.setText("已選中" + checkNum + "項(xiàng)");
}
});
}
// 初始化數(shù)據(jù)
private void initDate() {
for (int i = 0; i < 15; i++) {
list.add("data" + " " + i);
}
}
// 刷新listview和TextView的顯示
private void dataChanged() {
// 通知listView刷新
mAdapter.notifyDataSetChanged();
// TextView顯示最新的選中數(shù)目
tv_show.setText("已選中" + checkNum + "項(xiàng)");
};
}
列表適配器:
[java]
package com.notice.listcheck;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
// 填充數(shù)據(jù)的list
private ArrayList<String> list;
// 用來控制CheckBox的選中狀況
private static HashMap<Integer, Boolean> isSelected;
// 上下文
private Context context;
// 用來導(dǎo)入布局
private LayoutInflater inflater = null;
// 構(gòu)造器
public MyAdapter(ArrayList<String> list, Context context) {
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
// 初始化數(shù)據(jù)
initDate();
}
// 初始化isSelected的數(shù)據(jù)
private void initDate() {
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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
// 獲得ViewHolder對(duì)象
holder = new ViewHolder();
// 導(dǎo)入布局并賦值給convertview
convertView = inflater.inflate(R.layout.listviewitem, null);
holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
// 為view設(shè)置標(biāo)簽
convertView.setTag(holder);
} else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}
// 設(shè)置list中TextView的顯示
holder.tv.setText(list.get(position));
// 根據(jù)isSelected來設(shè)置checkbox的選中狀況
holder.cb.setChecked(getIsSelected().get(position));
return convertView;
}
public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
MyAdapter.isSelected = isSelected;
}
public static class ViewHolder {
TextView tv;
CheckBox cb;
}
}
- android ItemTouchHelper實(shí)現(xiàn)可拖拽和側(cè)滑的列表的示例代碼
- Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
- Android中ListView + CheckBox實(shí)現(xiàn)單選、多選效果
- Android Recyclerview實(shí)現(xiàn)多選,單選,全選,反選,批量刪除的功能
- android GridView多選效果的實(shí)例代碼
- Android的ListView多選刪除操作實(shí)現(xiàn)代碼
- Android中創(chuàng)建對(duì)話框(確定取消對(duì)話框、單選對(duì)話框、多選對(duì)話框)實(shí)例代碼
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android自定義控件實(shí)現(xiàn)可多選課程日歷CalendarView
- Android實(shí)現(xiàn)可拖拽列表和多選功能
相關(guān)文章
詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01Android利用ConstraintLayout實(shí)現(xiàn)漂亮的動(dòng)畫詳解
最近在無意中看到一篇關(guān)于ConstraintLayout的文章,ConstraintLayout是Android Studio 2.2中主要的新增功能之一,下面這篇文章主要給大家介紹了關(guān)于Android利用ConstraintLayout實(shí)現(xiàn)漂亮的動(dòng)畫的相關(guān)資料,需要的朋友可以參考下。2017-05-05Android視頻點(diǎn)播的實(shí)現(xiàn)代碼(邊播邊緩存)
本篇文章主要結(jié)合了Android視頻點(diǎn)播的實(shí)現(xiàn)代碼(邊播邊緩存),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05實(shí)現(xiàn)Android 滑動(dòng)退出Activity的功能
這篇文章主要介紹了實(shí)現(xiàn)Android 滑動(dòng)退出Activity的功能的相關(guān)資料,這里提供實(shí)例來說明滑動(dòng)退出應(yīng)用程序的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08android View 繪制完成監(jiān)聽的實(shí)現(xiàn)方法
今天小編就為大家分享一篇android View 繪制完成監(jiān)聽的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11Android Rreact Native 常見錯(cuò)誤總結(jié)
這篇文章主要介紹了Android Rreact Native 常見錯(cuò)誤總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06