欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2级

程序员文章站 2022-06-03 22:45:52
...

//布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.activity.MainActivity">

    <ExpandableListView
        android:layout_weight="1"
        android:id="@+id/listCar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <CheckBox
                android:id="@+id/checkAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="全选"/>
            <TextView
                android:layout_marginLeft="20dp"
                android:id="@+id/priceAll1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="合计"/>

            <TextView
                android:textColor="#f00"
                android:layout_marginLeft="20dp"
                android:id="@+id/priceAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥:"/>
        </LinearLayout>
        <Button
            android:background="#ea9c0a"
            android:text="去结算"
            android:layout_width="wrap_content"
            android:layout_height="30dp"/>
    </LinearLayout>

</LinearLayout>

//sj

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

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="商家"/>

</LinearLayout>

//sp

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="-2dp"
        android:layout_toRightOf="@+id/checks"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/name"
        android:layout_toRightOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="aa"
        android:padding="10dp"/>
    <TextView
        android:id="@+id/price"
        android:layout_toRightOf="@+id/image"
        android:layout_below="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="价格"
        android:padding="10dp"/>

    <com.bawie.liu.shopcar2.data.AddSub
        android:id="@+id/add_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp">
    </com.bawie.liu.shopcar2.data.AddSub>


</RelativeLayout>

//契约

public class IShopContract {
    //V层
    public interface IShopView{
        public void getShop(Shop shop);
    }

    //P层
    public interface IShopPresenter<IShopView>{
        void attach(IShopView iShopView);
        void death(IShopView iShopView);
        void requestData();
    }

    //M层
    public interface IShopModel{
        public void shopModel(Callback callback);

        public interface Callback{
            public void responseData(Shop shop);
        }
    }
}

//jia jian qi

public class AddSub extends LinearLayout implements View.OnClickListener {

    private TextView jia;
    private TextView sum;
    private TextView jian;

    public AddSub(Context context) {
        super(context);
        initView();
    }



    public AddSub(Context context,  AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public AddSub(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.add_sub_item, this);
        jia = view.findViewById(R.id.jia);
        sum = view.findViewById(R.id.sum);
        jian = view.findViewById(R.id.jian);

        jia.setOnClickListener(this);
        jian.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int number = Integer.parseInt(sum.getText().toString());
        switch (v.getId()){
            case R.id.jia:
                number++;
                sum.setText(number+"");
                break;
            case R.id.jian:
                if (number==1){
                    Toast.makeText(getContext(), "商品不能小于1", Toast.LENGTH_SHORT).show();
                }else {
                    number--;
                    sum.setText(number+"");
                }
                break;
        }

        if (addSubLinerner!=null){
            addSubLinerner.addsub(number);
        }
    }

    public void setCount(int count){
      sum.setText(count+"");
    }

    private AddSubLinerner addSubLinerner;
    public void setAddSubLinerner(AddSubLinerner addSubLinerner) {
        this.addSubLinerner = addSubLinerner;
    }

    public interface AddSubLinerner{
        void addsub(int addsub);
    }
}

//MainActivity

public class MainActivity extends AppCompatActivity implements IShopContract.IShopView,CarAdapter.SumsLinerner {

    @BindView(R.id.listCar)
    ExpandableListView listCar;
    @BindView(R.id.checkAll)
    CheckBox checkAll;
    @BindView(R.id.priceAll1)
    TextView priceAll1;
    @BindView(R.id.priceAll)
    TextView priceAll;
    private IShopPresenterInfo iShopPresenterInfo;
    List<Shop.DataBean> list = new ArrayList<>();
    private CarAdapter carAdapter;

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



        iShopPresenterInfo = new IShopPresenterInfo();


        carAdapter = new CarAdapter(list,this);
        listCar.setAdapter(carAdapter);
        iShopPresenterInfo.attach(this);
        iShopPresenterInfo.requestData();
        carAdapter.setsumsLinerner(this);
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                carAdapter.CheckAll(isChecked);
            }
        });

    }

    @Override
    public void getShop(Shop shop) {
        List<Shop.DataBean> data = shop.getData();
        carAdapter.addAll(data);
        int size = shop.getData().size();
        for (int i = 0; i < size; i++) {
            listCar.expandGroup(i);
        }

        carAdapter.notifyDataSetChanged();

    }

    @Override
    public void sumsPrice(double sumsPrice) {
        priceAll.setText(String.valueOf(sumsPrice));
    }
}

//adapter
public class CarAdapter extends BaseExpandableListAdapter {
    List<Shop.DataBean> mShopList = new ArrayList<>();
    Context context;

    public CarAdapter(List<Shop.DataBean> mShopList, Context context) {
        this.mShopList = mShopList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return mShopList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mShopList.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mShopList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mShopList.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public void addAll(List<Shop.DataBean> data){
        if (data!=null){
            mShopList.addAll(data);
        }
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupList groupList=null;
        if(convertView==null){
            convertView = View.inflate(parent.getContext(),R.layout.sj_item,null);
            groupList = new GroupList();
            groupList.checkBox = convertView.findViewById(R.id.checkbox);
            convertView.setTag(groupList);
        }else {
            groupList = (GroupList) convertView.getTag();
        }

        //商家赋值
        final Shop.DataBean dataBean = mShopList.get(groupPosition);
        //商家名称
        groupList.checkBox.setText(dataBean.getSellerName());
        //商家选中
        groupList.checkBox.setChecked(dataBean.isCheck());
        //点击
        groupList.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                dataBean.setCheck(checkBox.isChecked());

                //设置商品信息
                List<Shop.DataBean.ListBean> list = mShopList.get(groupPosition).getList();
                //循环商品赋值
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setSelected(checkBox.isChecked()?1:0);
                }
                notifyDataSetChanged();
                sums();
            }
        });


        return convertView;
    }

    private void sums() {
        //初始化
        double chuSums = 0;
        //循环商家
        for (int i = 0; i < mShopList.size(); i++) {
            Shop.DataBean dataBean = mShopList.get(i);
            //循环商品
            for (int j = 0; j < dataBean.getList().size(); j++) {
                Shop.DataBean.ListBean listBean = dataBean.getList().get(j);
                if (listBean.getSelected()==1){
                    chuSums=chuSums+listBean.getNum()*listBean.getPrice();
                }
            }
        }
        if (sumsLinerner!=null){
            sumsLinerner.sumsPrice(chuSums);
        }

    }

    private SumsLinerner sumsLinerner;
    public void setsumsLinerner(SumsLinerner sumsLinerner){
        this.sumsLinerner=sumsLinerner;
    }

    //接口回调
    public interface SumsLinerner{
        void sumsPrice(double sumsPrice);
    }

    //全部选中或取消
    public void CheckAll(boolean ischeck){
        //循环商家
        for (int i = 0; i < mShopList.size(); i++) {
            Shop.DataBean dataBean = mShopList.get(i);
            //循环商品
            for (int j = 0; j < dataBean.getList().size(); j++) {
                Shop.DataBean.ListBean listBean = dataBean.getList().get(j);
                listBean.setSelected(ischeck?1:0);
            }
        }
        notifyDataSetChanged();
        sums();
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildList childList=null;
        if(convertView==null){
            convertView = View.inflate(parent.getContext(),R.layout.sp_item,null);
            childList = new ChildList();
            childList.checks=convertView.findViewById(R.id.checks);
            childList.image=convertView.findViewById(R.id.image);
            childList.name=convertView.findViewById(R.id.name);
            childList.price=convertView.findViewById(R.id.price);
            childList.add_sub=convertView.findViewById(R.id.add_sub);
            convertView.setTag(childList);
        }else {
            childList = (ChildList) convertView.getTag();
        }

        //商家赋值
        final Shop.DataBean.ListBean listBean = mShopList.get(groupPosition).getList().get(childPosition);
        //商品名称
        childList.name.setText(listBean.getTitle());
        //商品价格
        childList.price.setText(listBean.getPrice()+"");
        //点击
        childList.checks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkBox = (CheckBox) v;
                listBean.setSelected(checkBox.isChecked()?1:0);
                sums();
            }
        });
        childList.add_sub.setCount(listBean.getCount());
        childList.add_sub.setAddSubLinerner(new AddSub.AddSubLinerner() {
            @Override
            public void addsub(int addsub) {
                listBean.setNum(addsub);
                sums();
            }
        });

        if (listBean.getSelected()==0){
            childList.checks.setChecked(false);
        }else {
            childList.checks.setChecked(true);
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupList{
        CheckBox checkBox;
    }

    class ChildList{
        CheckBox checks;
        ImageView image;
        TextView name;
        TextView price;
        AddSub add_sub;
    }
}
相关标签: 2