首页 > 代码库 > 全选功能实现总结

全选功能实现总结

全选功能实现:


xml部分:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal"
    android:paddingBottom="8dp"
    android:paddingTop="8dp" >
 
    <CheckBox
        android:id="@+id/item_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:clickable="false"(防止和ItemOnclick事件冲突)
        android:button="@drawable/icon_check_box"
        android:focusable="false" />
 
    <TextView
        android:id="@+id/dish_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/item_cb"
        android:text="麻婆豆腐"
        android:textColor="#666666"
        android:textSize="15sp" />
 
    <TextView
        android:id="@+id/receiver_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@+id/dish_name_tv"
        android:text="samy"
        android:textColor="#969696"
        android:textSize="12sp" />
 
    <TextView
        android:id="@+id/how_copies_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="1份"
        android:textColor="#666666"
        android:textSize="15sp" />
 
</RelativeLayout>  


Adapter部分:
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sign_in_dynamic_load_ll_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        setItemData(position, holder);
        return convertView;
    }
 
    private void setItemData(int position, ViewHolder holder) {
        SignInItemBean bean = getItem(position);
        holder.dish_name_tv.setText(bean.dishName);
        holder.receiver_name_tv.setText(bean.receiver);
        holder.how_copies_tv.setText("一份");
 
        holder.item_cb.setChecked(choosePosList.contains(position));
    }

Activity部分: 
   private boolean isCheckAll = false;
 
    private SignInBean bean;
    private List<SignInItemBean> inItemBeans;
    private List<Integer> choosePosList = new ArrayList<Integer>(); 
 
 
    private void fillViewFromNetwork() {
        inItemBeans = new ArrayList<SignInItemBean>();
        inItemBeans = bean.signList;
        shop_name_all_order_tv.setText(bean.buildingName + bean.houseNumber + "所有订单");
 
        adapter.addItems(inItemBeans);
        View view = inflater.inflate(R.layout.sign_list_lv_footernull);
        all_item_cb = (CheckBox) view.findViewById(R.id.all_item_cb);
        all_copies_tv = (TextView) view.findViewById(R.id.all_copies_tv);
        sign_list_lv.addFooterView(view);//(记住脚注一定要加载这里setAdapter(adapter)之前,防止部分手机显示不了脚注)
        sign_list_lv.setAdapter(adapter);
        // CheckBox直接用的一个点击事件处理;
        all_item_cb.setOnClickListener(this);
    }
 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (choosePosList.contains(new Integer(position))) {
            choosePosList.remove(new Integer(position));
        } else {
            choosePosList.add(new Integer(position));
        }
        if (choosePosList.size() == inItemBeans.size()) {
            isCheckAll = true;
        } else {
            isCheckAll = false;
        }
        showSelectContent(isCheckAll);
        adapter.notifyDataSetChanged();
    }
 
    private void doListSelect() {
        isCheckAll = !isCheckAll;
        choosePosList.clear();
        if (isCheckAll) {
            for (int i = 0; i < inItemBeans.size(); i++) {
                choosePosList.add(new Integer(i));
            }
        }
        showSelectContent(isCheckAll);
        adapter.notifyDataSetChanged();
    }
 
    private void showSelectContent(boolean isCheckAll) {
        all_item_cb.setChecked(isCheckAll);
        all_copies_tv.setText(bean.merchantName + choosePosList.size() + "份");
    }  



来自为知笔记(Wiz)



全选功能实现总结