简单购物车Demo_全选+反选+商品计价

尊重原创,如果转载请帖明出处,谢谢!

重点是功能,请自行修改布局,高手勿喷!


MyAdapter:

public class MyAdapter extends BaseAdapter {
    // 上下文
    Context context;
    // 数据源
    ArrayList<Goods> lists;

    MainActivity mainActivity;

    int MONEY = 0;

    HashMap<Integer,Boolean> isSelected;

    public MyAdapter(Context context, ArrayList<Goods> lists) {
        this.context = context;
        this.lists = lists;
        isSelected = new HashMap<>();
        mainActivity = (MainActivity) context;
        initData();
    }

    private void initData() {
        for (int i = 0 ; i < lists.size();i++){
            isSelected.put(i,false);
        }
    }

    @Override
    public int getCount() {
        return lists == null ? 0 : lists.size();
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item,null);
            holder = new MyHolder();
            holder.cb = (CheckBox) convertView.findViewById(R.id.item_check);
            holder.tv = (TextView) convertView.findViewById(R.id.item_text);
            holder.price = (TextView) convertView.findViewById(R.id.item_price);
            convertView.setTag(holder);
        }else{
            holder = (MyHolder) convertView.getTag();
        }

        holder.cb.setChecked(isSelected.get(position));
        holder.tv.setText(lists.get(position).name);
        holder.price.setText(lists.get(position).price+"");
        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                /*if(isChecked){    // 这个写法影响到商品的计价,取消选择商品时不会减价。
                    isSelected.put(position,isChecked);
                }*/
                isSelected.put(position,isChecked);

                MONEY = 0;
                for (int i = 0 ; i <lists.size();i++){
                    if (isSelected.get(i)){
                        MONEY+=lists.get(i).price;
                    }
                }
                mainActivity.changeMoney(MONEY);
            }
        });

        return convertView;
    }

    class MyHolder{
        TextView tv,price;
        CheckBox cb;
    }
}

item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_check"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_text"
        android:text="wenzi "/>
    <TextView
        android:paddingLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_price"
        android:text="价格 "/>
</LinearLayout>

public class MainActivity extends AppCompatActivity {

    CheckBox btn_all, btn_revers;
    ArrayList<Goods> lists;
    MyAdapter adapter;
    ListView listView;
    TextView all_money;

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

        btn_all = (CheckBox) findViewById(R.id.btn_all);
        btn_revers = (CheckBox) findViewById(R.id.btn_revers);
        lists = new ArrayList<>();
        listView = (ListView) findViewById(R.id.listView);
        all_money = (TextView) findViewById(R.id.all_money);
        for (int i = 1; i < 11; i++) {
            lists.add(new Goods("商品"+i,i*10));
        }
        adapter = new MyAdapter(MainActivity.this, lists);
        listView.setAdapter(adapter);

        btn_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i < lists.size(); i++) {
                    adapter.isSelected.put(i, isChecked);
                }
                adapter.notifyDataSetChanged();
            }
        });
        btn_revers.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    for (int i = 0 ; i < lists.size();i++){
                        if (adapter.isSelected.get(i)){
                            adapter.isSelected.put(i,!isChecked);
                        }else{
                            adapter.isSelected.put(i,isChecked);
                        }
                    }
                }else{
                    for (int i = 0 ; i < lists.size();i++){
                        if (adapter.isSelected.get(i)){
                            adapter.isSelected.put(i,isChecked);
                        }else{
                            adapter.isSelected.put(i,!isChecked);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
    public void changeMoney(int money){
        all_money.setText("总价:"+money);
    }}
 

main.xml

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

    <CheckBox
        android:id="@+id/btn_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选" />

    <CheckBox
        android:id="@+id/btn_revers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="反选" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="400dp"></ListView>

    <TextView
        android:id="@+id/all_money"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="总价"
        android:textStyle="bold"
        android:textSize="30dp"/>
</LinearLayout>

如果需要简单Demo的(仅模拟数据 实现全选反选批量删除),请参考http://blog.csdn.net/qq_37199105/article/details/78463034。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值