Android中使用ListView和CheckBox实现购物车多选功能

listview及相关控件 同时被 2 个专栏收录
56 篇文章 3 订阅
4 篇文章 0 订阅

1效果图
这里写图片描述
2MainActivity对应的布局activity_main

<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#0066FF"
        >
        <CheckBox
            android:id="@+id/cbQuanxuan"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="全选"

            />
        <Button
            android:id="@+id/btnQueding"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="确定"
            />


    </RelativeLayout>

  <ListView
      android:id="@+id/lvTest"
      android:layout_width="match_parent"
      android:layout_height="match_parent"></ListView>
</LinearLayout>

3MainActivity中

package com.zhh.mycheckbox;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private ListView lvTest;
    private CheckBox cbQuanxuan;
    LvAdapter lvAdapter;
    List<Domain> list;
    private Button btnQueding;
    private boolean ismChecked;

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

    private void initView() {
        btnQueding = findViewById(R.id.btnQueding);
        cbQuanxuan = findViewById(R.id.cbQuanxuan);
        lvTest = findViewById(R.id.lvTest);
        list = getData();
        lvAdapter = new LvAdapter(this, list);
        lvTest.setAdapter(lvAdapter);
    }

    private void myClick() {
//      外面的全选
        cbQuanxuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Logger.t("111").d("setOnCheckedChangeListener>>>" + isChecked);
                ismChecked = isChecked;
//              全选是选中状态
                if (isChecked) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).isXuanzhong = true;
                        lvAdapter.notifyDataSetChanged();
                    }
                }

            }
        });
        cbQuanxuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logger.t("111").d("setOnClickListener");
//              全选不是选中状态
                if (!ismChecked) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).isXuanzhong = false;
                        lvAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

        lvAdapter.setCheck(new LvAdapter.Check() {
            @Override
            public void mycheck(int position, boolean isChecked) {
                Logger.t("111").d("isChecked>>>" + isChecked);
                list.get(position).isXuanzhong = isChecked;

                for (int j = 0; j < list.size(); j++) {
                    if (list.get(j).isXuanzhong) {

                    } else {
//                  有没选中的
                        cbQuanxuan.setChecked(false);
                    }
                }
            }
        });
        btnQueding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isXuanzhong) {
                        String str = list.get(i).content;
                        sb.append(">>>>>" + str);
                    }
                }
                Logger.t("111").d("***" + sb.toString());

            }
        });
    }

    private List<Domain> getData() {
        List<Domain> list = new ArrayList<Domain>();
        for (int i = 0; i < 10; i++) {
            Domain domain = new Domain();
            int j = i % 2;
            if (j == 0) {
                domain.isXuanzhong = false;
            } else {
                domain.isXuanzhong = true;
            }
            domain.content = "我的内容" + i;
            list.add(domain);
        }
        return list;
    }


}

4LvAdapter适配器的布局item_layout

<?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="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New CheckBox"

            android:layout_gravity="center_horizontal" />

    </LinearLayout>




</LinearLayout>

5LvAdapter适配器中

package com.zhh.mycheckbox;

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.TextView;

import java.util.List;

/**
 * Created by 16838 on 2018/5/22.
 */
public class LvAdapter extends BaseAdapter{
    private Context context;
    private List<Domain>list;
    private Check check;
   public LvAdapter(Context context,List<Domain>list){
       this.context=context;
       this.list=list;
   }
    public void setCheck(Check check){
        this.check=check;
    }
    public interface Check{
        public  void mycheck(int position,boolean isChecked);

    }



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


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHoder viewHoder;
        if(convertView==null) {
            viewHoder=new ViewHoder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item_layout,null);
            viewHoder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
            convertView.setTag(viewHoder);
        }else{
            viewHoder= (ViewHoder) convertView.getTag();
        }
        viewHoder.checkBox.setText(list.get(position).content);
        viewHoder.checkBox.setChecked(list.get(position).isXuanzhong);
        viewHoder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(check!=null) {
                    check.mycheck(position,isChecked);
                }
            }
        });

        return convertView;
    }

    class ViewHoder{
        CheckBox checkBox;
    }




    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }

}

源码下载:
https://download.csdn.net/download/zhaihaohao1/10430518

  • 0
    点赞
  • 2
    评论
  • 4
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

zhaihaohao1

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值