欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中ListView綁定CheckBox實(shí)現(xiàn)全選增加和刪除功能(DEMO)

 更新時(shí)間:2016年08月31日 16:56:04   作者:劉某人程序員  
本文通過(guò)實(shí)例給大家講解了Android中ListView綁定CheckBox實(shí)現(xiàn)全選增加和刪除功能(DEMO)的代碼,對(duì)android checkbox全選相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧

ListView控件還是挺復(fù)雜的,也是項(xiàng)目中應(yīng)該算是比較常用的了,所以寫(xiě)了一個(gè)小Demo來(lái)講講,主要是自定義adapter的用法,加了很多的判斷等等等等….我們先來(lái)看看實(shí)現(xiàn)的效果吧!

這里寫(xiě)圖片描述

好的,我們新建一個(gè)項(xiàng)目LvCheckBox

這里寫(xiě)圖片描述

我們事先先把這兩個(gè)布局寫(xiě)好吧,一個(gè)是主布局,還有一個(gè)listview的item.xml,相信不用多說(shuō)

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#238286" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="ListView綁定CheckBox"
android:textColor="#fff" />
<TextView
android:id="@+id/tv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="17dp"
android:text="增加"
android:textColor="#fff" />
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_detele"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#238286"
android:text="刪除"
android:textColor="#fff" />
<Button
android:id="@+id/btn_select_all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#238286"
android:text="全選"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="7"
android:text="text" />
<CheckBox
android:id="@+id/cbCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

item.xml只有兩個(gè)控件,很好理解吧

初始化控件

我們用initView()方法來(lái)初始化這些控件

private void initView() {
tv_add = (TextView) findViewById(R.id.tv_add);
tv_add.setOnClickListener(this);
btn_detele = (Button) findViewById(R.id.btn_detele);
btn_detele.setOnClickListener(this);
btn_select_all = (Button) findViewById(R.id.btn_select_all);
btn_select_all.setOnClickListener(this);
listview = (ListView) findViewById(R.id.listview);
}

然后繼承點(diǎn)擊事件,button的和listview的

implements OnClickListener,OnItemClickListener

自定義Adapter

這里最難的就是adapter了

1.Bean

我們?yōu)榱藬?shù)據(jù)的記錄方便,我們提前寫(xiě)一個(gè)實(shí)體類(lèi)

package com.lgl.lvcheckbox;
public class Bean {
private String title;
// 構(gòu)造方法
public Bean(String title) {
super();
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

ListAdapter

這里所有的都寫(xiě)了注釋,也方便大家看清

package com.lgl.lvcheckbox;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
/**
* 自定義適配器
* 
* @author LGL
*
*/
public class ListAdapter extends BaseAdapter {
// 數(shù)據(jù)集
private List<Bean> list = new ArrayList<Bean>();
// 上下文
private Context mContext;
// 存儲(chǔ)勾選框狀態(tài)的map集合
private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();
// 構(gòu)造方法
public ListAdapter(Context mContext) {
super();
this.mContext = mContext;
// 默認(rèn)為不選中
initCheck(false);
}
// 初始化map集合
public void initCheck(boolean flag) {
// map集合的數(shù)量和list的數(shù)量是一致的
for (int i = 0; i < list.size(); i++) {
// 設(shè)置默認(rèn)的顯示
isCheck.put(i, flag);
}
}
// 設(shè)置數(shù)據(jù)
public void setData(List<Bean> data) {
this.list = data;
}
// 添加數(shù)據(jù)
public void addData(Bean bean) {
// 下標(biāo) 數(shù)據(jù)
list.add(0, bean);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
// 如果為null就返回一個(gè)0
return list != null ? list.size() : 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View view = null;
// 判斷是不是第一次進(jìn)來(lái)
if (convertView == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) view.findViewById(R.id.tvTitle);
viewHolder.cbCheckBox = (CheckBox) view
.findViewById(R.id.cbCheckBox);
// 標(biāo)記,可以復(fù)用
view.setTag(viewHolder);
} else {
view = convertView;
// 直接拿過(guò)來(lái)用
viewHolder = (ViewHolder) view.getTag();
}
// 拿到對(duì)象
Bean bean = list.get(position);
// 填充數(shù)據(jù)
viewHolder.title.setText(bean.getTitle().toString());
// 勾選框的點(diǎn)擊事件
viewHolder.cbCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// 用map集合保存
isCheck.put(position, isChecked);
}
});
// 設(shè)置狀態(tài)
if (isCheck.get(position) == null) {
isCheck.put(position, false);
}
viewHolder.cbCheckBox.setChecked(isCheck.get(position));
return view;
}
// 優(yōu)化
public static class ViewHolder {
public TextView title;
public CheckBox cbCheckBox;
}
// 全選按鈕獲取狀態(tài)
public Map<Integer, Boolean> getMap() {
// 返回狀態(tài)
return isCheck;
}
// 刪除一個(gè)數(shù)據(jù)
public void removeData(int position) {
list.remove(position);
}
}

當(dāng)然,有些方法是后面寫(xiě)的,我們提前寫(xiě)好,比如刪除和增加什么的

初始化數(shù)據(jù)

我們默認(rèn)總是需要點(diǎn)數(shù)據(jù)的

private void initData() {
// 默認(rèn)顯示的數(shù)據(jù)
List<Bean> list = new ArrayList<Bean>();
list.add(new Bean("張三"));
list.add(new Bean("李四"));
list.add(new Bean("王五"));
adapter = new ListAdapter(this);
adapter.setData(list);
listview.setAdapter(adapter);
}

增加數(shù)據(jù)

// 添加數(shù)據(jù)
case R.id.tv_add:
adapter.addData(new Bean("劉桂林"));
// 通知刷新適配器
adapter.notifyDataSetChanged();
break;

全選數(shù)據(jù)

當(dāng)我們?nèi)x的時(shí)候,按鈕應(yīng)該為全不選的,所以這里我們這里有狀態(tài)的

case R.id.btn_select_all:
// 全選——全不選
Map<Integer, Boolean> isCheck = adapter.getMap();
if (btn_select_all.getText().equals("全選")) {
adapter.initCheck(true);
// 通知刷新適配器
adapter.notifyDataSetChanged();
btn_select_all.setText("全不選");
btn_select_all.setTextColor(Color.YELLOW);
} else if (btn_select_all.getText().equals("全不選")) {
adapter.initCheck(false);
// 通知刷新適配器
adapter.notifyDataSetChanged();
btn_select_all.setText("全選");
btn_select_all.setTextColor(Color.YELLOW);
}
break;

刪除數(shù)據(jù)

刪除也是要考慮很多因素

// 刪除數(shù)據(jù)
case R.id.btn_detele:
// 拿到所有數(shù)據(jù)
Map<Integer, Boolean> isCheck_delete = adapter.getMap();
// 獲取到條目數(shù)量,map.size = list.size,所以
int count = adapter.getCount();
// 遍歷
for (int i = 0; i < count; i++) {
// 刪除有兩個(gè)map和list都要?jiǎng)h除 ,計(jì)算方式
int position = i - (count - adapter.getCount());
// 判斷狀態(tài) true為刪除
if (isCheck_delete.get(i) != null && isCheck_delete.get(i)) {
// listview刪除數(shù)據(jù)
isCheck_delete.remove(i);
adapter.removeData(position);
}
}
btn_select_all.setText("全選");
btn_select_all.setTextColor(Color.WHITE);
adapter.notifyDataSetChanged();
break;

這里的

int position = i - (count - adapter.getCount());

是一個(gè)計(jì)算方式,當(dāng)我們刪除之后,實(shí)際上數(shù)組是需要重新排列的,同時(shí)按鈕也要變回全選狀態(tài)的

這里寫(xiě)圖片描述

listview的點(diǎn)擊

我們直接點(diǎn)擊也是可以勾選cheakbox選中的
// listview的點(diǎn)擊事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// 判斷view是否相同
if (view.getTag() instanceof ViewHolder) {
// 如果是的話,重用
ViewHolder holder = (ViewHolder) view.getTag();
// 自動(dòng)觸發(fā)
holder.cbCheckBox.toggle();
}
}

以上所述是小編給大家介紹的Android中ListView綁定CheckBox實(shí)現(xiàn)全選增加和刪除功能(DEMO),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論