1.init方法中的實現
定義2個HashMap用於儲存已選擇的及是否可見的標誌
private void init() {
isSelected = new HashMap<Integer, Boolean>();
isVisible = new HashMap<Integer, Boolean>();
if (mData != null && mData.size() > 0) {
for (int i = 0; i < mData.size(); i++) {
isSelected.put(i, false);
isVisible.put(i, false);
}
}
}
複製程式碼
2.getView方法中的實現
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null){
view= LayoutInflater.from(mContext).inflate(R.layout.item_fund,viewGroup,false);
holder=new ViewHolder();
view.setTag(holder);
holder.tv_name=view.findViewById(R.id.tv_name);
holder.tv_code=view.findViewById(R.id.tv_code);
holder.checkBox=view.findViewById(R.id.checkBox);
}else {
holder=(ViewHolder) view.getTag();
}
if (mData!=null){
holder.tv_name.setText(mData.get(i).getName());
holder.tv_code.setText(mData.get(i).getCode());
holder.checkBox.setTag(i);
if (type.equals("delete")){
if (isSelected.get(i)){
isSelected.put(i,false);
}
}else {
}
holder.checkBox.setChecked(isSelected.get(i));
if (isVisible.get(i)) {
holder.checkBox.setVisibility(View.VISIBLE);
} else {
holder.checkBox.setVisibility(View.GONE);
}
}
return view;
}
複製程式碼
3.Activity中的實現
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (isEdit) {
mAdapter.type = "select";
FundListAdpater.ViewHolder holder = (FundListAdpater.ViewHolder) view.getTag();
holder.checkBox.toggle();
mAdapter.isSelected.put(position, holder.checkBox.isChecked());
}
}
});
@Override
public void onClick(View view) {
isHasSelect = false;
for (int i = 0; i < datas.size(); i++) {
if (mAdapter.isSelected.get(i)) {
isHasSelect = true;
break;
}
}
switch (view.getId()) {
case R.id.tv_edit:
if (datas != null && datas.size() > 0) {
if (isEdit) {
tv_edit.setText("編輯");
ll_bottom.setVisibility(View.GONE);
for (int i = 0; i < datas.size(); i++) {
mAdapter.isVisible.put(i, false);
}
mAdapter.notifyDataSetChanged();
isEdit = false;
} else {
tv_edit.setText("完成");
ll_bottom.setVisibility(View.VISIBLE);
for (int i = 0; i < datas.size(); i++) {
mAdapter.isVisible.put(i, true);
}
mAdapter.notifyDataSetChanged();
isEdit = true;
}
}
break;
case R.id.tv_cancel:
mAdapter.type = "select";
tv_cancel.setSelected(true);
tv_delete.setSelected(false);
if (isAllSelect) {
for (int i = 0; i < datas.size(); i++) {
mAdapter.isSelected.put(i, false);
}
} else {
for (int i = 0; i < datas.size(); i++) {
mAdapter.isSelected.put(i, true);
}
}
mAdapter.notifyDataSetChanged();
isAllSelect = !isAllSelect;
break;
case R.id.tv_delete:
mAdapter.type = "delete";
if (isHasSelect) {
tv_cancel.setSelected(false);
tv_delete.setSelected(true);
if (deleteOrderNoList != null) {
deleteOrderNoList.clear();
}
for (int i = 0; i < datas.size(); i++) {
if (mAdapter.isSelected.get(i)) {
//FundListAdpater.ViewHolder holder = (FundListAdpater.ViewHolder) lv.getChildAt(i-lv.getFirstVisiblePosition()).getTag();
deleteOrderNoList.add(datas.get(i).getCode());//選擇刪除的訂單號holder.tv_code.getText().toString()
FundBean deleteOrder = (FundBean) lv.getItemAtPosition(i);
deleteDatas.add(deleteOrder);
}
}
deleteFund(deleteOrderNoList);
}
break;
}
}
private void deleteFund(List<String> deleteOrderNoList) {
if (datas != null) {
datas.removeAll(deleteDatas);
mAdapter.notifyDataSetChanged();
mAdapter.notifyDataSetInvalidated();
}
Toast.makeText(this, deleteOrderNoList.toString(), Toast.LENGTH_LONG).show();
if (datas.size() == 0) {
tv_edit.setText("編輯");
ll_bottom.setVisibility(View.GONE);
for (int i = 0; i < datas.size(); i++) {
mAdapter.isVisible.put(i, false);
}
mAdapter.notifyDataSetChanged();
isEdit = false;
}
}
複製程式碼