购物车

//mainActivity=========

  package com.bwei.model.day20;

import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.bwei.model.day20.adapter.ExpandAdapter;
import com.bwei.model.day20.bean.JsonBean;
import com.bwei.model.day20.net.OkHttpUtil;
import com.bwei.model.day20.unm.AddNumLayout;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView mExpandView;
    private String urlPath = "http://120.27.23.105/product/getCarts?source=android&uid=99";
    private ExpandAdapter mAdapter;
    private List<JsonBean.DataBean> mData;
    private CheckBox txtQaxa;
    private TextView txtZong, txtJiesuan;
    private JsonBean.DataBean.ListBean mChild;
    private AddNumLayout onNumChangedListener;

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

        //初始化控件
        initView();

        if (mAdapter == null){
            //添加适配器
            mAdapter = new ExpandAdapter(this);
            mExpandView.setAdapter(mAdapter);
        }


        //初始化网络
        initData();

        //点击事件
        initListener();

    }

    //点击方法
    private void setCheckAll(int s){
        int itemCount = mAdapter.getGroupCount();
        for (int i = 0; i < itemCount; i++) {
            JsonBean.DataBean item = (JsonBean.DataBean) mAdapter.getGroup(i);
            List<JsonBean.DataBean.ListBean> list = item.getList();
            for (int j = 0; j < list.size(); j++) {
                JsonBean.DataBean.ListBean listBean = list.get(j);
                listBean.setSelected(s);
            }
        }
        mAdapter.notifyDataSetChanged();
        getToal();
    }
    private void initListener() {

        txtQaxa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    setCheckAll(0);
                }else {
                    setCheckAll(1);
                }
            }
        });
        mExpandView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

                JsonBean.DataBean group = (JsonBean.DataBean) mAdapter.getGroup(groupPosition);
                group.setIschecked(!group.isIschecked());
                int c = 1;
                if (group.isIschecked()){
                    c = 0;
                }
                List<JsonBean.DataBean.ListBean> list = group.getList();
                for (int i = 0; i < list.size(); i++) {
                    JsonBean.DataBean.ListBean listBean = list.get(i);
                    listBean.setSelected(c);
                }
                mAdapter.notifyDataSetChanged();
                getToal();
                return true;
            }
        });

        mExpandView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                mChild = (JsonBean.DataBean.ListBean) mAdapter.getChild(groupPosition, childPosition);
                boolean checked = mChild.isChecked();
                if (checked){
                    mChild.setSelected(0);
                }else {
                    mChild.setSelected(1);
                }
                mAdapter.notifyDataSetChanged();
                getToal();
                return true;
            }
        });

        mAdapter.setOnNumChangedListener(new AddNumLayout.OnClickListener() {
            @Override
            public void onClick(View v) {
                getToal();
            }
        });
    }

    private void initData() {
        //连接okhttp
        new OkHttpUtil().get(urlPath).setGetData(new OkHttpUtil.GetData() {
            @Override
            public void Feast(String s) {
                Gson gson = new Gson();
                JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
                mData = jsonBean.getData();
                mAdapter.setList(mData);
                //默认展开
                for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                    mExpandView.expandGroup(i);
                }
            }
        });
    }

    private void initView() {
        mExpandView = (ExpandableListView) findViewById(R.id.expand_view);
        txtQaxa = (CheckBox) findViewById(R.id.txt_qaxa);
        txtZong = (TextView) findViewById(R.id.txt_zong);
        txtJiesuan = (TextView) findViewById(R.id.txt_jisuan);
    }

    public void getToal() {
        double total = 0;
        int groupCount = mAdapter.getGroupCount();
        for (int i = 0; i < groupCount; i++) {
            JsonBean.DataBean item = (JsonBean.DataBean) mAdapter.getGroup(i);
            List<JsonBean.DataBean.ListBean> list = item.getList();
            for (int j = 0; j < list.size(); j++) {
                JsonBean.DataBean.ListBean listBean = list.get(j);
                boolean checked = listBean.isChecked();
                if (!checked){
                    double price = listBean.getPrice();
                    total += price * listBean.getNum();
                }
            }
        }
        txtZong.setText("合计:"+ total);
    }
}

//mainactivty.xml

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/expanded_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/txt_lin"
       />

    <RelativeLayout
        android:id="@+id/txt_lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="10dp">

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

        <TextView
            android:id="@+id/txt_zong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_toRightOf="@id/txt_qaxa"
            android:layout_margin="10dp"/>

        <TextView
            android:id="@+id/txt_jisuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去结算"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>

</RelativeLayout>

//适配器adapter

package com.bwei.model.day20.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwei.model.day20.R;
import com.bwei.model.day20.bean.JsonBean;
import com.bwei.model.day20.unm.AddNumLayout;

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

public class ExpandAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<JsonBean.DataBean> mList = new ArrayList<JsonBean.DataBean>();
    private View.OnClickListener mOnNumChangedListener;

    public ExpandAdapter(Context context) {
        mContext = context;
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mList.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 true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupHolder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.adapter_group, null);
            groupHolder = new GroupViewHolder();

            groupHolder.groupBox = (CheckBox) convertView.findViewById(R.id.group_box);
            groupHolder.txtName = (TextView) convertView.findViewById(R.id.txt_name);

            convertView.setTag(groupHolder);
        }else{
            groupHolder = (GroupViewHolder) convertView.getTag();
        }
        groupHolder.txtName.setText(mList.get(groupPosition).getSellerName());
        groupHolder.groupBox.setChecked(mList.get(groupPosition).isIschecked());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childHolder;
        if (convertView == null){
            convertView = View.inflate(mContext, R.layout.adapter_child, null);
            childHolder = new ChildViewHolder();

            childHolder.childBox = (CheckBox) convertView.findViewById(R.id.child_box);
            childHolder.childImage = (ImageView) convertView.findViewById(R.id.child_image);
            childHolder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
            childHolder.txtPrice = (TextView) convertView.findViewById(R.id.txt_price);
            childHolder.sumAddsub = (AddNumLayout) convertView.findViewById(R.id.sum_addsub);

            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildViewHolder) convertView.getTag();
        }
        final JsonBean.DataBean.ListBean listBean = mList.get(groupPosition).getList().get(childPosition);

        childHolder.childBox.setChecked(listBean.getSelected()==0);
        childHolder.txtTitle.setText(listBean.getTitle());
        childHolder.txtPrice.setText(listBean.getPrice()+ "");
        childHolder.sumAddsub.setCurentcount(listBean.getNum());
        Glide.with(mContext).load(listBean.getImages().split("\\|")[0]).into(childHolder.childImage);

        return convertView;
    }

    public void setOnNumChangedListener(View.OnClickListener onNumChangedListener) {
        mOnNumChangedListener = onNumChangedListener;
    }
    public void setList(List<JsonBean.DataBean> list) {
        mList = list;
    }

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


    class GroupViewHolder{

        public CheckBox groupBox;
        public TextView txtName;
    }
    class ChildViewHolder{

        public CheckBox childBox;
        public ImageView childImage;
        public TextView txtPrice;
        public TextView txtTitle;
        public AddNumLayout sumAddsub;
    }
}

//父类xml

<?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:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/group_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:enabled="false"
        />

    <TextView
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:padding="10dp"
        android:layout_marginLeft="20dp"
        />
</LinearLayout>

//子类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="match_parent"
   >

    <CheckBox
        android:id="@+id/child_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:enabled="false"
        android:layout_centerVertical="true"
        />

    <ImageView
        android:id="@+id/child_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:padding="10dp"
        android:layout_toRightOf="@id/child_box"
        android:layout_marginLeft="10dp"
        />
    <TextView
        android:id="@+id/child_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="头"
        android:padding="10dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/child_image"
        />
    <TextView
        android:id="@+id/child_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格"
        android:padding="10dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/child_image"
        android:layout_below="@id/child_title"
        />

    <com.bwei.model.gouwuche.sum.AddsumLayout
        android:id="@+id/sum_addsub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        />
</RelativeLayout>

//自定义加减

public class ShoppingNumView extends LinearLayout {

    private TextView sumSubtract, sumNmb, sumAdd;

    //自动生成
    public ShoppingNumView(Context context) {
        this( context, null );
    }

    public ShoppingNumView(Context context, @Nullable AttributeSet attrs) {
        this( context, attrs, -1 );
    }

    public ShoppingNumView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super( context, attrs, defStyleAttr );
        initView( context );
        initlisetener();
    }

    private void initView(Context context) {
        View view = View.inflate( context, R.layout.shopping_adapter_view, this  );
        sumSubtract = (TextView) view.findViewById( R.id.sum_subtract );
        sumNmb = (TextView) view.findViewById( R.id.sum_nmb );
        sumAdd = (TextView) view.findViewById( R.id.sum_add );
        sumNmb.setText( "1" );
    }

    private void initlisetener() {
        sumAdd.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        } );

        sumSubtract.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                Subtract();
            }
        } );
    }

    private void add() {
        String shu = sumNmb.getText().toString();
        int suan = Integer.parseInt( shu );
        suan++;
        setCurentcount( suan );
    }

    private void Subtract() {
        String shu = sumNmb.getText().toString();
        int suan = Integer.parseInt(shu);
        if (suan > 1){
            suan--;
            setCurentcount(suan);
        }else {
            Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
        }

    }

    public int getCurentcount() {
        return Integer.parseInt( sumNmb.getText().toString() );
    }

    public void setCurentcount(int num) {
        sumNmb.setText( num+"" );
        if (onNumChangedListener != null){
            onNumChangedListener.onNumChange( this, num );
        }
    }

    private OnNumChangedListener onNumChangedListener;

    public void setOnNumChangedListener(OnNumChangedListener onNumChangedListener) {
        this.onNumChangedListener = onNumChangedListener;
    }

    private interface OnNumChangedListener{
        void onNumChange(View view, int curNum);
    }
}

//加减xml

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

    <TextView
        android:id="@+id/sum_jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"
        android:text="--"
        android:textColor="@color/colorAccent"/>

    <TextView
        android:id="@+id/sum_nmb"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/sum_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20dp"
        android:text="+"
        android:textColor="@color/colorAccent"/>
</LinearLayout>

//RecyclerView ========================================

// Activity

public class MainActivity extends AppCompatActivity implements CarView{

    @BindView(R.id.tou)
    TextView tou;
    @BindView(R.id.car_recycler)
    RecyclerView carRecycler;
    @BindView(R.id.car_box)
    CheckBox carBox;
    @BindView(R.id.car_zong)
    TextView carZong;

    private List<CarBean.DataBean> mList = new ArrayList<>();
    
    private CarRecycleAdapter mCarAdapter;
    private Object mTotal;
    private List<CarBean.DataBean.ListBean> mChildlist;
    private TextView carMai;
    
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage( msg );
            switch (msg.what){
                case 1:
                    setAdapter();
                    break;
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        ButterKnife.bind( this );
        carMai = (TextView) findViewById( R.id.car_mai );

        //p层
        CarPesenter pesenter = new CarPesenter( this );
        pesenter.car("71");

    }
    public void setAdapter(){
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        carRecycler.setLayoutManager(layoutManager);
        //适配器
        mCarAdapter = new CarRecycleAdapter( this, mList);
        carRecycler.setAdapter(mCarAdapter);
        //接收接口
        mCarAdapter.setOnShopItemClick( new CarRecycleAdapter.OnShopItemClick() {
            @Override
            public void onCheckBoxClick(View view, int i) {
                getTotal();
                mCarAdapter.notifyDataSetChanged();
            }

            @Override
            public void onSubViewClick(View view, int i) {
                getTotal();
                mCarAdapter.notifyDataSetChanged();
            }

            @Override
            public void onAddViewClick(View view, int i) {
                getTotal();
                mCarAdapter.notifyDataSetChanged();
            }
        } );
    }
    //总价
    public void getTotal() {
        int num = 0;
        double total = 0;
        for (int i = 0; i < mList.size(); i++) {
            List<CarBean.DataBean.ListBean> list = mList.get( i ).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get( j ).getSelected()==1){
                    num += list.get( j ).getNum();
                    total += list.get( j ).getNum() * list.get( j ).getPrice();
                }
            }
        }
        carZong.setText( "合计:¥" +total );
    }
    @OnClick({R.id.tou, R.id.car_recycler, R.id.car_box, R.id.car_zong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tou:

                break;
            case R.id.car_recycler:

                break;
            case R.id.car_box:
              //全选
                for (int i = 0; i < mCarAdapter.getItemCount(); i++) {
                    if (carBox.isChecked()){
                        mList.get( i ).setIsChecked( 1 );
                    }else {
                        mList.get( i ).setIsChecked( 0 );
                    }
                    mChildlist = mList.get( i ).getList();
                    for (int j = 0; j < mChildlist.size(); j++) {
                        if (carBox.isChecked()){
                            mChildlist.get( j ).setSelected(1);
                        }else {
                            mChildlist.get( j ).setSelected( 0 );
                        }
                    }
                }
                mCarAdapter.notifyDataSetChanged();
                getTotal();
                break;
            case R.id.car_zong:

                break;
        }
    }

    @Override
    public void carSuccess(CarBean success) {
        mList = success.getData();
        Toast.makeText( this, mList.toString()+"", Toast.LENGTH_SHORT ).show();
        mHandler.sendEmptyMessage( 1 );
    }

    @Override
    public void carFail(String carfail) {
        Toast.makeText( this, carfail, Toast.LENGTH_SHORT ).show();
    }
}

//适配器父布局

public class CarRecycleAdapter extends RecyclerView.Adapter<CarRecycleAdapter.ViewHolder> {
    private Context mContext;
    private List<CarBean.DataBean> mList;
    private CarchildAdapter mChildadapter;


    public CarRecycleAdapter(Context context, List<CarBean.DataBean> list) {
        mContext = context;
        mList = list;
    }

    //接口回调
    private OnShopItemClick mOnShopItemClick;

    public void setOnShopItemClick(OnShopItemClick onShopItemClick) {
        this.mOnShopItemClick = onShopItemClick;
    }

    public interface OnShopItemClick{
        void onCheckBoxClick(View view, int i);
        void onSubViewClick(View view, int i);
        void onAddViewClick(View view, int i);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate( mContext, R.layout.carfu_adapter, null );
        ViewHolder holder = new ViewHolder( view );

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder,final int i) {
        viewHolder.adapterFuCheck.setText( mList.get( i ).getSellerName()+"" );
        int selected = mList.get( i ).getIsChecked();
        if (selected == 1){
            viewHolder.adapterFuCheck.setChecked( true );
        }else {
            viewHolder.adapterFuCheck.setChecked( false );
        }
        //点击商家选中商品(不好使)
        viewHolder.adapterFuCheck.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<CarBean.DataBean.ListBean> childList = mList.get( i ).getList();
                for (int j = 0; j < childList.size(); j++) {
                    childList.get( j ).setSelected( 1 );
                }
                mChildadapter.notifyDataSetChanged();
            }
        } );
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        viewHolder.adapterChildRecyclerView.setLayoutManager(layoutManager);
        //连接子布局集合
        List<CarBean.DataBean.ListBean> list = mList.get( i ).getList();
        //适配器
        mChildadapter = new CarchildAdapter( mContext, list );
        viewHolder.adapterChildRecyclerView.setAdapter( mChildadapter );

        //点击接收接口回调
        mChildadapter.setOnChildItemClick( new CarchildAdapter.OnChildItemClick() {
            @Override
            public void onCheckBoxClick(View view, int i) {
                mOnShopItemClick.onCheckBoxClick(view, i);
            }

            @Override
            public void onSubViewClick(View view, int i) {
                mOnShopItemClick.onSubViewClick(view,i);
            }

            @Override
            public void onAddViewClick(View view, int i) {
                mOnShopItemClick.onAddViewClick(view, i);
            }
        } );
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final RecyclerView adapterChildRecyclerView;
        private final CheckBox adapterFuCheck;

        public ViewHolder(@NonNull View itemView) {
            super( itemView );
            adapterFuCheck = (CheckBox) itemView.findViewById( R.id.adapter_fuCheck );
            adapterChildRecyclerView = (RecyclerView) itemView.findViewById( R.id.adapter_child_recyclerView );

        }
    }
}

//子布局

public class CarchildAdapter extends RecyclerView.Adapter<CarchildAdapter.ViewHodel> {

    private Context mContext;
    private List<CarBean.DataBean.ListBean> mList;

    public CarchildAdapter(Context context, List<CarBean.DataBean.ListBean> list) {
        mContext = context;
        mList = list;
    }

    //接口回调
    private OnChildItemClick onChildItemClick;

    public void setOnChildItemClick(OnChildItemClick onChildItemClick) {
        this.onChildItemClick = onChildItemClick;
    }

    public interface OnChildItemClick {
        void onCheckBoxClick(View view, int i);
        void onSubViewClick(View view, int i);
        void onAddViewClick(View view, int i);
    }

    @NonNull
    @Override
    public ViewHodel onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate( mContext, R.layout.carchild_adapter, null );
        ViewHodel holder = new ViewHodel( view );
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHodel viewHodel, final int i) {
        viewHodel.tvGoodsName.setText( mList.get( i ).getTitle() );
        viewHodel.tvGoodsPrice.setText( mList.get( i ).getPrice() + "" );
        //特殊图片
        String images = mList.get( i ).getImages();
        String[] spit = images.split( "\\|" );
        Glide.with( mContext ).load(spit[0] ).into( viewHodel.adapterChildimage );
        viewHodel.tvNum.setText( mList.get( i ).getNum() + "" );
        //选择器
        viewHodel.adapterChildCheck.setChecked( mList.get( i ).getSelected()== 1 );
        //点击选择框
        viewHodel.adapterChildCheck.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int check = mList.get( i ).getSelected();
                if (check == 1) {
                    mList.get( i ).setSelected( 0 );
                } else {
                    mList.get( i ).setSelected( 1 );
                }
                notifyDataSetChanged();
                onChildItemClick.onCheckBoxClick( v, i );
            }
        } );
        //点击加减
        viewHodel.tvSub.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = mList.get( i ).getNum();
                if (num > 1) {
                    num--;
                    mList.get( i ).setNum( num );
                }
                notifyDataSetChanged();
                onChildItemClick.onSubViewClick( v, i );
            }
        } );
        viewHodel.tvAdd.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int numadd = mList.get( i ).getNum();
                numadd++;
                mList.get( i ).setNum( numadd );
                notifyDataSetChanged();
                onChildItemClick.onAddViewClick( v, i );
            }
        } );
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHodel extends RecyclerView.ViewHolder {

        private final CheckBox adapterChildCheck;
        private final ImageView adapterChildimage;
        private final TextView tvGoodsName, tvGoodsPrice, tvNum, tvSub, tvAdd;

        public ViewHodel(@NonNull View itemView) {
            super( itemView );
            adapterChildCheck = (CheckBox) itemView.findViewById( R.id.adapter_childCheck );
            adapterChildimage = (ImageView) itemView.findViewById( R.id.adapter_childimage );
            tvGoodsName = (TextView) itemView.findViewById( R.id.tv_goodsName );
            tvGoodsPrice = (TextView) itemView.findViewById( R.id.tv_goodsPrice );
            tvSub = (TextView) itemView.findViewById( R.id.tv_sub );
            tvNum = (TextView) itemView.findViewById( R.id.tv_num );
            tvAdd = (TextView) itemView.findViewById( R.id.tv_add );

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值