Listview上的checkbox重复和滚动自动调用chexkbox设置监听

来源:互联网 发布:婚前同居 知乎 编辑:程序博客网 时间:2024/06/05 03:04
本例子重点在于hashmap保存checkbox所修改的项和checkbox监听的设置
适配器实例化时我们定义一个适配器上数据产都的map,map的key是索引,value是boolean类型,代表该索引对应的checkbox是否选中了。
/**
* @author Administrator 补货商品列表
*/
class ListViewAdapter extends BaseAdapter {
Activity context;
private HashMap<Integer, Boolean> isSelected;


public ListViewAdapter(Activity context) {
this.context = context;
isSelected = new HashMap<Integer, Boolean>();
initDate();

}


// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < pdbillList.size(); i++) {
getIsSelected().put(i, false);
}
}



@Override
public int getCount() {
return pdbillList.size();
}


@Override
public Object getItem(int position) {
return pdbillList.get(position);
}


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


@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = context.getLayoutInflater().inflate(
R.layout.pd_hw_list_item, null);
holder.spbm_xstmTv = (TextView) convertView
.findViewById(R.id.pd_spmc_xstm);
holder.spmclTv = (TextView) convertView
.findViewById(R.id.pd_spbm);
holder.bhslTv = (TextView) convertView
.findViewById(R.id.pd_bhsl);
holder.hsjjTv = (TextView) convertView
.findViewById(R.id.pd_hsjj);
holder.lsjTv = (TextView) convertView.findViewById(R.id.pd_lsj);
holder.zpsl = (TextView) convertView.findViewById(R.id.pd_zpsl);
holder.cb_checkbox = (CheckBox) convertView
.findViewById(R.id.pd_sfys_check);
holder.ys_item_content = (LinearLayout) convertView
.findViewById(R.id.pd_item_content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PDBillModel list = pdbillList.get(position);
holder.zpsl.setVisibility(View.GONE);
holder.ys_item_content
.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
nowPosition = position;
changeDataDialog();
}
});
holder.cb_checkbox.setOnCheckedChangeListener(null);//先使checkbox监听消失,当我们设置后在添加监听,这样就避免了因为滑动而自动回调checkbox的监听
holder.cb_checkbox.setChecked(getIsSelected().get(position));

holder.cb_checkbox .setOnCheckedChangeListener(new
OnCheckedChangeListener() {
 
@Override public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) { 
nowPosition = position; 
if(isSelected.get(position)) {
pdbillList.get(position).setHandle(false);
//当点击后监听到时会根据该checkbox在map中的值,进入这里代表该值原先为true,现修改为false,即从选中状态变为未选中状态 
isSelected.put(position, false);
setIsSelected(isSelected);//修改后将map进行重新赋值 
}else { 
pdbillList.get(position).setHandle(true);
//当点击后监听到时会根据该checkbox在map中的值,进入这里代表该值原先为false,现修改为true,即从未选中状态变为选中状态
isSelected.put(position, true); 
setIsSelected(isSelected);//修改后将map进行重新赋值

}
});


holder.spbm_xstmTv.setText(list.getName() + " \n销售条码:"+ list.getC1());
holder.spmclTv.setText(list.getComCode());// 商品编码
holder.bhslTv.setText(list.getCcol() + "");// 陈列号
holder.hsjjTv.setText(list.getUnit() + "");// 单位
holder.lsjTv.setText(list.getScsl() + "");// 数量


return convertView;
}


public HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}


public void setIsSelected(HashMap<Integer, Boolean> isSelected) {
this.isSelected = isSelected;
}



class ViewHolder {
TextView spbm_xstmTv;
TextView spmclTv;
TextView bhslTv;
TextView hsjjTv;
TextView lsjTv;
TextView zpsl;
CheckBox cb_checkbox;
LinearLayout ys_item_content;
}

}


类似的例子:http://pan.baidu.com/s/1mioBvrq




下面的是我在网上看到的一个例子自己转载过来记录下,实现起来比较简单
主布局
<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="com.example.umengtest.MainActivity" >


    <TextView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1111" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="fffff" />


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>


主Activity
public class MainActivity extends Activity {
private String device_token;
private TextView show;
private ListView listView;
private ListViewAdapter adapter;
private String[] beans = new String[] { "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "19", "19", "19", "19", "19", "19", "19", "19", "19", "19",
"19", "19", "19", "19", "19", "19", "19", "19", "19", "19", "19",
"19", "19", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "19", "19",
"19", "19", "19", "19", "19", "19", "19", "19", "19", "19", "19",
"19", "19", "19", "19", "19", "19", "19", "19", "19", "19" };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.listView1);
adapter = new ListViewAdapter(MainActivity.this, beans);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}


主适配器
public class ListViewAdapter extends BaseAdapter {


private Context context;
private String[] beans;


// 用来控制CheckBox的选中状况
private static HashMap<Integer, Boolean> isSelected;


class ViewHolder {


TextView tvName;
CheckBox cb;
}


public ListViewAdapter(Context context, String[] beans) {
// TODO Auto-generated constructor stub
this.beans = beans;
this.context = context;
isSelected = new HashMap<Integer, Boolean>();
// 初始化数据
initDate();
}


// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < beans.length; i++) {
getIsSelected().put(i, false);
}
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return beans.length;
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return beans[position];
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}





public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}


public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
ListViewAdapter.isSelected = isSelected;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
String bean = beans[position];
LayoutInflater inflater = LayoutInflater.from(context);
if (convertView == null) {
convertView = inflater.inflate(R.layout.assist_device_binding_list_item, null);
holder = new ViewHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.tvName = (TextView) convertView
.findViewById(R.id.tv_device_name);
convertView.setTag(holder);
} else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}


holder.tvName.setText(bean);
// 监听checkBox并根据原来的状态来设置新的状态
holder.cb.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {


if (isSelected.get(position)) {
isSelected.put(position, false);
setIsSelected(isSelected);
} else {
isSelected.put(position, true);
setIsSelected(isSelected);
}


}
});


// 根据isSelected来设置checkbox的选中状况
holder.cb.setChecked(getIsSelected().get(position));
return convertView;
}
}


单项布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <RelativeLayout
        android:id="@+id/outpatient_check_hospital"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5.0dip"
        android:layout_marginLeft="12.599976dip"
        android:layout_marginRight="12.599976dip"
        android:layout_marginTop="5.0dip"
        android:background="#AAAAAA"
        android:gravity="center_vertical" >


        <LinearLayout
            android:id="@+id/linear_layout_up"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10.0dip"
            android:gravity="center"
            android:orientation="horizontal" >


            <ImageView
                android:layout_width="10dip"
                android:layout_height="10dip"
                android:adjustViewBounds="false" />


            <TextView
                android:id="@+id/tv_device_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2.0"
                android:text="名称"
                android:textColor="#ff323232"
                android:textSize="16.0sp"
                android:typeface="monospace" />


            <CheckBox
                android:id="@+id/checkBox1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />
        </LinearLayout>
    </RelativeLayout>


</LinearLayout>




https://yunpan.cn/cvGgKNEeqKkeV  访问密码 3587
0 0