android 中ListView嵌套CheckBox解决复用相关问题

时间:2021-10-11 19:33:03

1.建立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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

2.建立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="match_parent"
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/item_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        />
    <CheckBox 
        android:id="@+id/item_ch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:checked="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />
</LinearLayout>

3.Myadatper类中的代码

public class MyAdapter extends BaseAdapter {


private List<String> list;
private static HashMap<Integer,Boolean> isSelected;
private Context context;
private LayoutInflater inflater;
ViewHolder viewHolder=null;
private Abc abc; 
public MyAdapter(List<String> list, Context context,Abc abc) {
super();
this.list = list;
this.context = context;
isSelected=new HashMap<Integer, Boolean>();
inflater=LayoutInflater.from(context);
this.abc=abc;
initDate();
}
    //初始化isSelected数据
    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 arg0) {
return list.get(arg0);
}


@Override
public long getItemId(int arg0) {
return arg0;
}


@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
if(convertView==null){
viewHolder=new ViewHolder();
convertView=inflater.inflate(R.layout.item,null);
viewHolder.tv=(TextView) convertView.findViewById(R.id.item_tv);
viewHolder.cb=(CheckBox) convertView.findViewById(R.id.item_ch);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
// 设置list中TextView的显示 
viewHolder.tv.setText(list.get(position)); 
        // 根据isSelected来设置checkbox的选中状况 
viewHolder.cb.setChecked(getIsSelected().get(position));
CheckBox ch=viewHolder.cb;
if(!getIsSelected().get(position)){
abc.update(ch,position,true);
}else{
abc.update(ch,position,false);
}
        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{
  public TextView tv;
  public CheckBox cb;
   }
   public interface Abc{
  public void update(View view,int position,boolean flag);
   }
}

4.MainAcitivity代码


public class MainActivity extends Activity {


private ListView  lv;
private MyAdapter mAdapter;
private ArrayList<String> list;
    private Abc abc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listview);
list = new ArrayList<String>();
initDate();
abc=new Abc() {

@Override
public void update(View view,final int position,final boolean flag) {
CheckBox ch=(CheckBox) view;
ch.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
MyAdapter.getIsSelected().put(position,flag);
Toast.makeText(MainActivity.this,position+"dddd"+true,0).show();
}
});
   }
};
mAdapter=new MyAdapter(list,this,abc);
lv.setAdapter(mAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
}
// 初始化数据   
    private void initDate() {   
        for (int i = 0; i <56; i++) {   
            list.add("data" + "   " + i);   
        }   
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}