Android 에서 ListView 바 인 딩 CheckBox 는 전체 선택 추가 및 삭제 기능(DEMO)을 수행 합 니 다.
 
 알 겠 습 니 다.프로젝트 를 새로 만 들 겠 습 니 다.LvCheckBox
 
 우 리 는 먼저 이 두 개의 레이아웃 을 잘 씁 시다.하 나 는 메 인 레이아웃 이 고,또 하 나 는 listview 의 item.xml 입 니 다.더 말 할 필요 가 없 을 것 이 라 고 믿 습 니 다.
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>
<?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>컨트롤 초기 화
이 컨트롤 들 을 initView()방법 으로 초기 화 합 니 다.
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);
}
implements OnClickListener,OnItemClickListener여기 서 제일 어 려 운 게 adapter 예요.
1.Bean
우 리 는 데이터 의 기록 편 의 를 위해 서 실체 류 를 미리 썼 다.
package com.lgl.lvcheckbox;
public class Bean {
private String title;
//     
public Bean(String title) {
super();
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}여기에 모든 것 이 주석 을 써 서 모두 가 똑똑히 볼 수 있 게 하 였 다.
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 {
//    
private List<Bean> list = new ArrayList<Bean>();
//    
private Context mContext;
//         map  
private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();
//     
public ListAdapter(Context mContext) {
super();
this.mContext = mContext;
//       
initCheck(false);
}
//    map  
public void initCheck(boolean flag) {
// map      list       
for (int i = 0; i < list.size(); i++) {
//        
isCheck.put(i, flag);
}
}
//     
public void setData(List<Bean> data) {
this.list = data;
}
//     
public void addData(Bean bean) {
//      
list.add(0, bean);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//    null     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;
//           
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);
//   ,    
view.setTag(viewHolder);
} else {
view = convertView;
//       
viewHolder = (ViewHolder) view.getTag();
}
//     
Bean bean = list.get(position);
//     
viewHolder.title.setText(bean.getTitle().toString());
//         
viewHolder.cbCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//  map    
isCheck.put(position, isChecked);
}
});
//     
if (isCheck.get(position) == null) {
isCheck.put(position, false);
}
viewHolder.cbCheckBox.setChecked(isCheck.get(position));
return view;
}
//   
public static class ViewHolder {
public TextView title;
public CheckBox cbCheckBox;
}
//         
public Map<Integer, Boolean> getMap() {
//     
return isCheck;
}
//       
public void removeData(int position) {
list.remove(position);
}
}데이터 초기 화
우 리 는 기본적으로 항상 데이터 가 필요 하 다.
private void initData() {
//        
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);
}
//     
case R.id.tv_add:
adapter.addData(new Bean("   "));
//        
adapter.notifyDataSetChanged();
break;우리 가 모두 선택 할 때 단 추 는 모두 선택 하지 않 아야 하기 때문에 여기 서 우 리 는 상태 가 있다.
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;삭제 도 여러 가지 요 소 를 고려 해 야 합 니 다.
//     
case R.id.btn_detele:
//       
Map<Integer, Boolean> isCheck_delete = adapter.getMap();
//        ,map.size = list.size,  
int count = adapter.getCount();
//   
for (int i = 0; i < count; i++) {
//      map list     ,    
int position = i - (count - adapter.getCount());
//      true   
if (isCheck_delete.get(i) != null && isCheck_delete.get(i)) {
// listview    
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()); 
 listview 클릭
            cheakbox   
// listview     
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//   view    
if (view.getTag() instanceof ViewHolder) {
//      ,  
ViewHolder holder = (ViewHolder) view.getTag();
//     
holder.cbCheckBox.toggle();
}
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.