倒入依赖:
略(。。。)
得到接口里面的数据信息。。。。。在bean包里面设置一种状态
//设置一种状态
private boolean child_flag=false;
public boolean isChild_flag() {
return child_flag;
}
public void setChild_flag(boolean child_flag) {
this.child_flag = child_flag;
}
实现一个AddSubView类(自定义View)
主要功能:
public class AddSubView extends LinearLayout implements View.OnClickListener {
private TextView sub;
private TextView add;
private EditText count;
private AddClickListener addClickListener;
private SubClickListener subClickListener;
private static final String TAG = "AddSubView------------";
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.activity_add_sub_view, this);
//获取控件
sub = view.findViewById(R.id.sub);
add = view.findViewById(R.id.add);
count = view.findViewById(R.id.count);
sub.setOnClickListener(this);
add.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View v) {
switch (v.getId()) {
//减少
case R.id.sub:
String c = count.getText().toString();
int i = Integer.parseInt(c);
if (i <= 0) {
return;
}
count.setText(--i + "");
subClickListener.onSubClick(v,i);
Log.d(TAG, "减-----" + i);
break;
//增加
case R.id.add:
String c1 = count.getText().toString();
int i1 = Integer.parseInt(c1);
count.setText(++i1 + "");
addClickListener.onAddClick(v,i1);
Log.d(TAG, "onClick() returned: " + "加===");
break;
}
}
public int getCount() {
return Integer.parseInt(count.getText().toString().trim());
}
public void setCount(int s) {
count.setText(s + "");
}
public interface AddClickListener {
void onAddClick(View view, int count);
}
public interface SubClickListener {
void onSubClick(View view, int count);
}
public void setOnAddClickListener(AddClickListener addClickListener) {
this.addClickListener = addClickListener;
}
public void setOnSubClickListener(SubClickListener subClickListener) {
this.subClickListener = subClickListener;
}
}
自定义一个工具类 ——-》计算总价
public class SumUtils {
public static String sum(List<CartBean.DataBean> list){
double sum=0;
for (int i=0;i<list.size ();i++){
List<CartBean.ChildBean> list1 = list.get (i).getList ();
for (int j=0;j<list1.size ();j++){
CartBean.ChildBean childBean = list1.get (j);
if(childBean.isChild_flag ()){
sum+=childBean.getPrice ()*childBean.getNum ();
}
}
}
return Double.toString (sum);
}
}
主方法里面的方法:
public class ShoppingActivity extends AppCompatActivity implements ICartView,View.OnClickListener{
private static final String TAG = "ShoppingActivity---";
private ExpandableListView expandableListView;
private CheckBox checkBox;
private TextView sum;
private Button js;
private CheckBox cb;
private List<CartBean.DataBean> list;
private MyExpandAdapter myExpandAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_shopping);
initDatas();
initViews();
}
private void initViews() {
PresenterImpel presenterImpel = new PresenterImpel ();
presenterImpel.ShowCartToView (new ModelImpel (presenterImpel),this);
}
private void initDatas() {
//得到控件
expandableListView = findViewById(R.id.expandlist);
checkBox = findViewById(R.id.cb);
sum = findViewById(R.id.sum);
js = findViewById(R.id.js);
cb = findViewById (R.id.cb);
cb.setOnClickListener (this);
checkBox.setOnClickListener(this);
js.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cb:
boolean checked = cb.isChecked ();
for (int i = 0; i<list.size (); i++){
CartBean.DataBean dataBean = list.get (i);
dataBean.setParent_flag (checked);
List<CartBean.ChildBean> list1 = dataBean.getList ();
for (int j=0;j<list1.size ();j++){
list1.get (j).setChild_flag (checked);
}
}
myExpandAdapter.notifyDataSetChanged ();
//计算。。。。
String count = SumUtils.sum (list);
sum.setText (count);
break;
case R.id.js:
Toast.makeText (ShoppingActivity.this,"结算了-----",Toast.LENGTH_SHORT).show ();
break;
}
}
@Override
public void ShowCartToViews(List<CartBean.DataBean> list) {
Log.d (TAG, "ShowCartToViews: "+list.size ());
this.list = list;
myExpandAdapter = new MyExpandAdapter (ShoppingActivity.this,list,this);
expandableListView.setAdapter (myExpandAdapter);
int childCount = expandableListView.getCount ();
for (int i=0;i<childCount;i++){
expandableListView.expandGroup (i);
}
}
@Override
public void getCount(String count) {
Log.d (TAG, "总价: "+count);
sum.setText ("总价:"+count);
}
}
设置适配器
public class MyExpandAdapter extends BaseExpandableListAdapter {
private static final String TAG = "MyExpandAdapter---";
private final Context context;
private final List<CartBean.DataBean> list;
private final ICartView iCartView;
private ParentViewHolder parentViewHolder;
private ChildViewHolder childViewHolder;
public MyExpandAdapter(Context context, List<CartBean.DataBean> list, ICartView iCartView){
this.context = context;
this.list = list;
this.iCartView = iCartView;
}
@Override
public int getGroupCount() {
return list.size ();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getList ().size ();
}
@Override
public Object getGroup(int groupPosition) {
return list.get (groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.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(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
if(convertView==null){
convertView = View.inflate (context, R.layout.parent_item,null);
CheckBox parent_box = convertView.findViewById (R.id.parent_cb);
TextView parent_title = convertView.findViewById (R.id.parent_title);
parentViewHolder = new ParentViewHolder (parent_box, parent_title);
convertView.setTag (parentViewHolder);
}else{
parentViewHolder = (ParentViewHolder) convertView.getTag ();
}
parentViewHolder.parent_title.setText (list.get (groupPosition).getSellerName ());
//-----------------------------------------得到复选框的状态
parentViewHolder.getParent_cb ().setChecked (list.get (groupPosition).isParent_flag ());
parentViewHolder.getParent_cb ().setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
boolean isChecked = list.get (groupPosition).isParent_flag ();
isChecked=!isChecked;
list.get (groupPosition).setParent_flag (isChecked);
List<CartBean.ChildBean> list_parent = MyExpandAdapter.this.list.get (groupPosition).getList ();
for (int i=0;i<list_parent.size ();i++){
list_parent.get (i).setChild_flag (isChecked);
}
notifyDataSetChanged ();
//计算。。。。
String sum = SumUtils.sum (list);
iCartView.getCount (sum);
}
//-----------------------------------------得到复选框的状态
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = View.inflate (context,R.layout.child_item,null);
CheckBox child_cb = convertView.findViewById (R.id.child_cb);
SimpleDraweeView child_pic = convertView.findViewById (R.id.child_pic);
TextView child_title = convertView.findViewById (R.id.chid_title);
TextView chile_price = convertView.findViewById (R.id.chid_price);
AddSubView addSubView = convertView.findViewById (R.id.add_sub_view);
Button delete = convertView.findViewById (R.id.delete);
childViewHolder = new ChildViewHolder (child_cb, child_pic, child_title, chile_price,addSubView,delete);
convertView.setTag (childViewHolder);
}else{
childViewHolder = (ChildViewHolder) convertView.getTag ();
}
final List<CartBean.ChildBean> child_list = this.list.get (groupPosition).getList ();
String images = child_list.get (childPosition).getImages ();
String pic_url = images.split ("\\|")[0];
childViewHolder.child_pic.setImageURI (pic_url);
childViewHolder.child_title.setText (child_list.get (childPosition).getTitle ());
childViewHolder.child_price.setText (child_list.get (childPosition).getPrice ()+"");
childViewHolder.addSubView.setCount (child_list.get (childPosition).getNum ());
//设置点击事件,两个事件都要设置
//添加的监听
childViewHolder.getAddSubView().setOnAddClickListener(new AddSubView.AddClickListener() {
@Override
public void onAddClick(View view, int count) {
list.get(groupPosition).getList().get(childPosition).setNum(count);
//开始计算,如果选中了,就计算,没选中,就不计算
boolean selected = list.get(groupPosition).getList().get(childPosition).isChild_flag ();
Log.d(TAG, "onAddClick() returned: " + "计算开始");
if (selected){
String sum = SumUtils.sum(list);
Log.d(TAG, "onAddClick() returned: " + sum);
iCartView.getCount (sum);
}
}
});
//减法
childViewHolder.getAddSubView().setOnSubClickListener(new AddSubView.SubClickListener() {
@Override
public void onSubClick(View view, int count) {
list.get(groupPosition).getList().get(childPosition).setNum(count);
boolean selected = list.get(groupPosition).getList().get(childPosition).isChild_flag ();
Log.d(TAG, "onAddClick() returned: " + "计算开始");
if (selected){
String sum = SumUtils.sum(list);
Log.d(TAG, "onAddClick() returned: " + sum);
iCartView.getCount (sum);
}
}
});
//-----------------------------------------得到复选框的状态
childViewHolder.getChild_cb ().setChecked (child_list.get (childPosition).isChild_flag ());
//计算。。。。
String sum = SumUtils.sum (list);
iCartView.getCount (sum);
childViewHolder.getDelete ().setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
child_list.remove (childPosition);
list.remove (groupPosition);
notifyDataSetChanged ();
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public class ParentViewHolder{
private CheckBox parent_cb;
private TextView parent_title;
public ParentViewHolder(CheckBox parent_cb, TextView parent_title) {
this.parent_cb = parent_cb;
this.parent_title = parent_title;
}
public CheckBox getParent_cb() {
return parent_cb;
}
public void setParent_cb(CheckBox parent_cb) {
this.parent_cb = parent_cb;
}
public TextView getParent_title() {
return parent_title;
}
public void setParent_title(TextView parent_title) {
this.parent_title = parent_title;
}
}
public class ChildViewHolder{
private CheckBox child_cb;
private SimpleDraweeView child_pic;
private TextView child_title;
private TextView child_price;
private AddSubView addSubView;
private Button delete;
public ChildViewHolder(CheckBox child_cb, SimpleDraweeView child_pic, TextView child_title, TextView child_price, AddSubView addSubView, Button delete) {
this.child_cb = child_cb;
this.child_pic = child_pic;
this.child_title = child_title;
this.child_price = child_price;
this.addSubView = addSubView;
this.delete = delete;
}
public CheckBox getChild_cb() {
return child_cb;
}
public void setChild_cb(CheckBox child_cb) {
this.child_cb = child_cb;
}
public SimpleDraweeView getChild_pic() {
return child_pic;
}
public void setChild_pic(SimpleDraweeView child_pic) {
this.child_pic = child_pic;
}
public TextView getChild_title() {
return child_title;
}
public void setChild_title(TextView child_title) {
this.child_title = child_title;
}
public TextView getChild_price() {
return child_price;
}
public void setChild_price(TextView child_price) {
this.child_price = child_price;
}
public AddSubView getAddSubView() {
return addSubView;
}
public void setAddSubView(AddSubView addSubView) {
this.addSubView = addSubView;
}
public Button getDelete() {
return delete;
}
public void setDelete(Button delete) {
this.delete = delete;
}
}
}
布局文件:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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:background="#fff"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/expandable_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:groupIndicator="@null"
android:layout_weight="1"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="合计:0" />
<Button
android:id="@+id/js"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F00"
android:text="去结算(0)" />
</LinearLayout>
</LinearLayout>
item_child:
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/child_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/child_pic"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:singleLine="true"
android:id="@+id/child_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:singleLine="true"
android:id="@+id/child_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品价格"
android:textColor="#F00" />
<com.bawie.chenzhiqiang.shopcar_demo.view.AddSubView
android:id="@+id/add_sub_view"
android:layout_width="100dp"
android:layout_marginLeft="60dp"
android:layout_height="wrap_content">
</com.bawie.chenzhiqiang.shopcar_demo.view.AddSubView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
item_parent:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/parent_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/parent_title"
android:text="商家"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_add_sub_vew:
<?xml version="1.0" encoding="utf-8"?>
<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="horizontal">
<TextView
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="-"
android:textSize="25sp" />
<EditText
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/add_sub_shape"
android:focusable="false"
android:gravity="center"
android:text="0" />
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="+"
android:textSize="25sp" />
</LinearLayout>
在drawable文件夹下面创建效果:
add_sub_shap:
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="100dp" android:height="30dp"/>
<stroke android:width="1dp" android:color="#999999"/>
</shape>
添加删除操作:例如
//删除
childViewHolder.getDelte().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
child_list.remove(childPosition);
notifyDataSetChanged();
}
});