依赖
compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup.okio:okio:1.5.0' compile 'com.google.code.gson:gson:2.6.2' compile 'com.github.bumptech.glide:glide:3.7.0'
权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
//activity_main布局 LinearLayout android:orientation="vertical"
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/iv" android:layout_gravity="center"/>
//activity_cart布局 LinearLayout android:orientation="vertical"
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="购物车" android:textColor="@android:color/black" android:textSize="25sp"/> <View android:layout_width="match_parent" android:layout_height="0.75dp" android:background="#999999"/> <ExpandableListView android:id="@+id/ex_list_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:groupIndicator="@null"></ExpandableListView> <View android:layout_width="match_parent" android:layout_height="0.75dp" android:background="#999999"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cart_cb" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="全选/反选" android:textColor="@android:color/black" android:textSize="25sp"/> <TextView android:id="@+id/cart_sum12" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="总价:0" android:textColor="@android:color/black" android:textSize="25sp"/> </LinearLayout>
//activity_jiajian布局LinearLayout android:orientation="horizontal"
<TextView android:layout_width="40px" android:layout_height="30px" android:text="-" android:id="@+id/jian" android:gravity="center" android:background="@drawable/textview_border"/> <EditText style="?android:attr/textViewStyle" android:background="@drawable/textview_border" android:layout_width="40px" android:layout_height="30px" android:hint="0" android:id="@+id/content" android:gravity="center_horizontal" android:inputType="number"/> <TextView android:background="@drawable/textview_border" android:layout_width="40px" android:layout_height="30px" android:text="+" android:id="@+id/jia" android:gravity="center" />
//activity_show布局 LinearLayout android:orientation="vertical"
<LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/bb" android:layout_gravity="center_vertical"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="详情" android:textSize="30dp" android:layout_gravity="center" android:gravity="center"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.75dp" android:background="#999999"></View> <ImageView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/iv_show" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/main_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商品名" android:textSize="20dp"/> <TextView android:id="@+id/main_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="原价" android:textSize="20dp"/> <TextView android:id="@+id/main_price2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="折扣价" android:textSize="20dp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.75dp" android:background="#F00"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="horizontal"> <TextView android:id="@+id/main_cart" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:clickable="true" android:gravity="center" android:text="购物车"/> <View android:layout_width="0.75dp" android:layout_height="match_parent" android:background="#F00"/> <TextView android:id="@+id/main_add" android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:clickable="true" android:gravity="center" android:text="加入购物车"/> </LinearLayout>
//layout_child布局LinearLayout android:orientation="horizontal"
<CheckBox android:id="@+id/child_cb" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/child_img" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/child_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商品名"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/child_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="优惠价" android:textColor="@android:color/holo_red_dark"/> <mywork.com.gwcdemodaidonghua.View.MyView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mv" android:layout_alignParentRight="true"></mywork.com.gwcdemodaidonghua.View.MyView> </RelativeLayout> </LinearLayout>
//layout_parent布局 LinearLayout android:orientation="horizontal"
<CheckBox android:id="@+id/parent_cb" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/parent_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商家" android:textColor="@android:color/black" android:textSize="25sp"/>
//Https连网工具类
//HttpConfig类
public class HttpConfig { public static String detail_url = "https://www.zhaoapi.cn/product/getProductDetail"; public static String add_url = "https://www.zhaoapi.cn/product/addCart"; public static String cartList_url = "https://www.zhaoapi.cn/product/getCarts"; }
//OkHttpUtils
import android.os.Handler; import android.os.Message; import java.io.IOException; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response;
public class OkHttpUtils { private static OkHttpUtils okHttpUtils; MyHandler handler = new MyHandler(); private OkloadListener okloadlistener; public static OkHttpUtils getinstance(){ if(okHttpUtils==null){ okHttpUtils = new OkHttpUtils(); } return okHttpUtils; } public void okget(String uri){ OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInter()).build(); Request request = new Request.Builder().url(uri).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message msg = new Message(); msg.what=0; msg.obj=e.getMessage(); handler.sendMessage(msg); } @Override public void onResponse(Call call, Response response) throws IOException { Message msg = new Message(); msg.what=1; msg.obj=response.body().string(); handler.sendMessage(msg); } }); } public void okpost(String uri, Map<String,String> map){ OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInter()).build(); FormBody.Builder builder = new FormBody.Builder(); Set<String> set = map.keySet(); for (String key : set){ String value = map.get(key); builder.add(key,value+""); } FormBody build = builder.build(); Request request = new Request.Builder().url(uri).post(build).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message msg = new Message(); msg.what=0; msg.obj=e.getMessage(); handler.sendMessage(msg); } @Override public void onResponse(Call call, Response response) throws IOException { Message msg = new Message(); msg.what=1; msg.obj=response.body().string(); handler.sendMessage(msg); } }); } class MyInter implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); RequestBody body = request.body(); if(body instanceof FormBody){ FormBody.Builder builder = new FormBody.Builder(); for (int i = 0; i <((FormBody)body).size() ; i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); builder.add(name,value); } builder.add("source", "android"); FormBody build = builder.build(); Request build1 = request.newBuilder().post(build).build(); Response response = chain.proceed(build1); return response; } return null; } } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int what = msg.what; if(what==0){ String error = (String) msg.obj; okloadlistener.okloaderror(error); }else{ String json = (String) msg.obj; okloadlistener.okloadsuccess(json); } } } public void setokLoadListener(OkloadListener okloadListener){ this.okloadlistener = okloadListener; } }
//OkloadListener接口
public interface OkloadListener { void okloadsuccess(String json); void okloaderror(String error); }
//Modle层
//AddListener接口
public interface AddListener { void addsuccess(String json); void adderror(String error); }
//CartBean类
public class CartBean {
private String msg; private String code; private List<DataBean> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { //添加一boolean,记录选中状态 private boolean parentIsSelected; public DataBean(boolean parentIsSelected, String sellerName, String sellerid, List<ChildBean> list) { this.parentIsSelected = parentIsSelected; this.sellerName = sellerName; this.sellerid = sellerid; this.list = list; } public boolean isParentIsSelected() { return parentIsSelected; } public void setParentIsSelected(boolean parentIsSelected) { this.parentIsSelected = parentIsSelected; } private String sellerName; private String sellerid; private List<ChildBean> list; public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ChildBean> getList() { return list; } public void setList(List<ChildBean> list) { this.list = list; } } public class ChildBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private double num; private double pid; private double price; private double pscid; private double selected; private double sellerid; private String subhead; private String title; private boolean childIsSelected; private int content; public ChildBean(double bargainPrice, String createtime, String detailUrl, String images, double num, double pid, double price, double pscid, double selected, double sellerid, String subhead, String title, boolean childIsSelected,int content) { this.bargainPrice = bargainPrice; this.createtime = createtime; this.detailUrl = detailUrl; this.images = images; this.num = num; this.pid = pid; this.price = price; this.pscid = pscid; this.selected = selected; this.sellerid = sellerid; this.subhead = subhead; this.title = title; this.childIsSelected = childIsSelected; this.content = content; } public boolean isChildIsSelected() { return childIsSelected; } public void setChildIsSelected(boolean childIsSelected) { this.childIsSelected = childIsSelected; } public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public double getNum() { return num; } public void setNum(double num) { this.num = num; } public double getPid() { return pid; } public void setPid(double pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getPscid() { return pscid; } public void setPscid(double pscid) { this.pscid = pscid; } public double getSelected() { return selected; } public void setSelected(double selected) { this.selected = selected; } public double getSellerid() { return sellerid; } public void setSellerid(double sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getContent() { return content; } public void setContent(int content) { this.content = content; } } }
//CartListLoadListener接口
public interface CartListLoadListener { //购物车数据,加载成功 void loadCartSuccess(String json); //加载失败 void loadCartError(String error); }
// DatailBean类
public class DatailBean { /** * msg : * seller : {"description":"我是商家10","icon":"http://120.27.23.105/images/icon.png","name":"商家10","productNums":999,"score":5,"sellerid":10} * code : 0 * data : {"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":0,"pid":17,"price":299,"pscid":1,"salenum":888,"sellerid":10,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"} */ private String msg; private SellerBean seller; private String code; private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public SellerBean getSeller() { return seller; } public void setSeller(SellerBean seller) { this.seller = seller; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class SellerBean { /** * description : 我是商家10 * icon : http://120.27.23.105/images/icon.png * name : 商家10 * productNums : 999 * score : 5 * sellerid : 10 */ private String description; private String icon; private String name; private int productNums; private int score; private int sellerid; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getProductNums() { return productNums; } public void setProductNums(int productNums) { this.productNums = productNums; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } } public static class DataBean { /** * bargainPrice : 111.99 * createtime : 2017-10-03T23:53:28 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * itemtype : 0 * pid : 17 * price : 299 * pscid : 1 * salenum : 888 * sellerid : 10 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private int price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } }
//DetailListener 接口
public interface DetailListener { void detaloadsuccess(String json); void detaloaderror(String error); }
//Imodle接口
public interface Imodle { void getDetailData(String uri, Map<String, String> map, DetailListener listener); void addToCart(String url, Map<String, String> parms, AddListener addListener); void showDataToCart(String url, Map<String, String> parms, CartListLoadListener cartListLoadListener); double calcaulate(CartBean cartBean); }
//ModleImpl类
import android.util.Log; import java.util.List; import java.util.Map; import mywork.com.gwcdemodaidonghua.Https.OkHttpUtils; import mywork.com.gwcdemodaidonghua.Https.OkloadListener;
public class ModleImpl implements Imodle { private static final String TAG = "ModleImpl"; @Override public void getDetailData(String uri, Map<String, String> map, final DetailListener listener) { OkHttpUtils utils = OkHttpUtils.getinstance(); utils.okpost(uri,map); utils.setokLoadListener(new OkloadListener() { @Override public void okloadsuccess(String json) { listener.detaloadsuccess(json); } @Override public void okloaderror(String error) { listener.detaloaderror(error); } }); } @Override public void addToCart(String url, Map<String, String> parms, final AddListener addListener) { OkHttpUtils okHttpUtils = new OkHttpUtils(); okHttpUtils.okpost(url,parms); okHttpUtils.setokLoadListener(new OkloadListener() { @Override public void okloadsuccess(String json) { addListener.addsuccess(json); } @Override public void okloaderror(String error) { addListener.adderror(error); } }); } @Override public void showDataToCart(String url, Map<String, String> parms, final CartListLoadListener cartListLoadListener) { OkHttpUtils utils = OkHttpUtils.getinstance(); utils.okpost(url,parms); utils.setokLoadListener(new OkloadListener() { @Override public void okloadsuccess(String json) { cartListLoadListener.loadCartSuccess(json); } @Override public void okloaderror(String error) { cartListLoadListener.loadCartError(error); } }); } @Override public double calcaulate(CartBean cartBean) { double sum=0; List<CartBean.DataBean> data = cartBean.getData(); for (int i = 0; i < data.size(); i++) { List<CartBean.ChildBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { if(list.get(j).isChildIsSelected()){ double bargainPrice = list.get(j).getBargainPrice(); int content = list.get(j).getContent(); Log.e(TAG, "calcaulate: "+content ); sum+=bargainPrice*content; } } } return sum; } }
//MyAdapter类
import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import mywork.com.gwcdemodaidonghua.Presenter.presenterimpl; import mywork.com.gwcdemodaidonghua.R; import mywork.com.gwcdemodaidonghua.View.ICartView; import mywork.com.gwcdemodaidonghua.View.MyView;
public class MyAdapter extends BaseExpandableListAdapter{ private final Context context; private final CartBean cartBean; private final ICartView iCartView; private final List<CartBean.DataBean> list; private static final String TAG = "MyAdapter"; private EditText editText; private TextView jian; private TextView jia; public MyAdapter(Context context, CartBean cartBean, ICartView iCartView) { this.context = context; this.cartBean = cartBean; this.list = cartBean.getData(); 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 false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) { ParentViewHolder parentViewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_parent, null); CheckBox parent_cb = convertView.findViewById(R.id.parent_cb); TextView parent_tv = convertView.findViewById(R.id.parent_tv); parentViewHolder = new ParentViewHolder(parent_cb, parent_tv); convertView.setTag(parentViewHolder); } else { parentViewHolder = (ParentViewHolder) convertView.getTag(); } //赋值 parentViewHolder.getParent_cb().setChecked(list.get(groupPosition).isParentIsSelected()); parentViewHolder.getParent_tv().setText(list.get(groupPosition).getSellerName()); //点击事件 parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isCheked = list.get(groupPosition).isParentIsSelected(); isCheked = !isCheked; list.get(groupPosition).setParentIsSelected(isCheked); //遍历子元素,全部置为true List<CartBean.ChildBean> list = MyAdapter.this.list.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { list.get(i).setChildIsSelected(isCheked); } //通知界面 notifyDataSetChanged(); //计算 presenterimpl presenter = new presenterimpl(); presenter.jisuan(new ModleImpl(), cartBean, iCartView); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, ViewGroup parent) { ChildeViewHolder childeViewHolder = null; int i = list.get(groupPosition).getList().get(childPosition).getContent(); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_child, null); CheckBox child_cb = convertView.findViewById(R.id.child_cb); ImageView child_img = convertView.findViewById(R.id.child_img); TextView child_title = convertView.findViewById(R.id.child_title); TextView child_price = convertView.findViewById(R.id.child_price); MyView mv = convertView.findViewById(R.id.mv); childeViewHolder = new ChildeViewHolder(child_cb, child_img, child_title, child_price,mv); convertView.setTag(childeViewHolder); } else { childeViewHolder = (ChildeViewHolder) convertView.getTag(); } //赋值 childeViewHolder.getChild_cb().setChecked(list.get(groupPosition).getList().get(childPosition).isChildIsSelected()); //获取图的地址 String images = list.get(groupPosition).getList().get(childPosition).getImages(); String imageUrl = images.split(".jpg")[0] + ".jpg"; Glide.with(context).load(imageUrl).into(childeViewHolder.getChild_imge()); childeViewHolder.getChild_title().setText(list.get(groupPosition).getList().get(childPosition).getTitle()); childeViewHolder.getChild_price().setText(list.get(groupPosition).getList().get(childPosition).getBargainPrice() + ""); final MyView myview = childeViewHolder.getMyview(); UiBean uiBean = myview.getet(); jia = uiBean.getJia(); jian = uiBean.getJian(); editText = uiBean.getEditText(); jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(list.get(groupPosition).getList().get(childPosition).getContent()<5){ list.get(groupPosition).getList().get(childPosition).setChildIsSelected(true); int content = list.get(groupPosition).getList().get(childPosition).getContent(); content++; Log.e(TAG, "onClick: "+editText ); //editText.setText(content+""); myview.aa(content); list.get(groupPosition).getList().get(childPosition).setContent(content); //通知改变 notifyDataSetChanged(); //计算 presenterimpl presenter = new presenterimpl(); presenter.jisuan(new ModleImpl(), cartBean, iCartView); } } }); jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(list.get(groupPosition).getList().get(childPosition).getContent()>1){ int content = list.get(groupPosition).getList().get(childPosition).getContent(); content--; myview.aa(content); // editText.setText(content+""); list.get(groupPosition).getList().get(childPosition).setContent(content); //通知改变 notifyDataSetChanged(); //计算 presenterimpl presenter = new presenterimpl(); presenter.jisuan(new ModleImpl(), cartBean, iCartView); } } }); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { String s = editable.toString(); Integer integer = Integer.valueOf(s); list.get(groupPosition).getList().get(childPosition).setContent(integer); presenterimpl presenter = new presenterimpl(); presenter.jisuan(new ModleImpl(), cartBean, iCartView); } }); //字条目中的checkbox点击状态 //点击事件 childeViewHolder.getChild_cb().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isCheked = list.get(groupPosition).getList().get(childPosition).isChildIsSelected(); isCheked = !isCheked; list.get(groupPosition).getList().get(childPosition).setChildIsSelected(isCheked); //遍历 List<CartBean.ChildBean> list1 = MyAdapter.this.list.get(groupPosition).getList(); boolean flag = true; for (int i = 0; i < list1.size(); i++) { if (!list1.get(i).isChildIsSelected()) { flag = false; } } list.get(groupPosition).setParentIsSelected(flag); //通知改变 notifyDataSetChanged(); //计算 presenterimpl presenter = new presenterimpl(); presenter.jisuan(new ModleImpl(), cartBean, iCartView); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } //优化 class ParentViewHolder { public CheckBox parent_cb; public TextView parent_tv; public ParentViewHolder(CheckBox parent_cb, TextView parent_tv) { this.parent_cb = parent_cb; this.parent_tv = parent_tv; } public CheckBox getParent_cb() { return parent_cb; } public void setParent_cb(CheckBox parent_cb) { this.parent_cb = parent_cb; } public TextView getParent_tv() { return parent_tv; } public void setParent_tv(TextView parent_tv) { this.parent_tv = parent_tv; } } class ChildeViewHolder { private MyView myview; public CheckBox child_cb; public ImageView child_imge; public TextView child_title; public TextView child_price; public ChildeViewHolder(CheckBox child_cb, ImageView child_imge, TextView child_title, TextView child_price, MyView myView) { this.myview = myView; this.child_cb = child_cb; this.child_imge = child_imge; this.child_title = child_title; this.child_price = child_price; } public CheckBox getChild_cb() { return child_cb; } public void setChild_cb(CheckBox child_cb) { this.child_cb = child_cb; } public ImageView getChild_imge() { return child_imge; } public void setChild_imge(ImageView child_imge) { this.child_imge = child_imge; } 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 MyView getMyview() { return myview; } public void setMyview(MyView myview) { this.myview = myview; } } }
//UiBean类
public class UiBean { private TextView jia; private TextView jian; private EditText editText; public UiBean(TextView jia, TextView jian, EditText editText){ this.jia = jia; this.jian = jian; this.editText = editText; } public TextView getJia() { return jia; } public void setJia(TextView jia) { this.jia = jia; } public TextView getJian() { return jian; } public void setJian(TextView jian) { this.jian = jian; } public EditText getEditText() { return editText; } public void setEditText(EditText editText) { this.editText = editText; } }
//Presenter层
//Ipresenter接口
public interface Ipresenter { void showdatato(Imodle imodle, ImainView imainView); void jumptocart(ImainView imainView); void showDataToCart(Context context, Imodle iModel, ICartView iCartView); void jisuan(Imodle iModel, CartBean cartBean, ICartView iCartView); void addcart(Imodle imodle, ImainView imainView); }
//presenterimpl类
import android.content.Context; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; import mywork.com.gwcdemodaidonghua.Https.HttpConfig; import mywork.com.gwcdemodaidonghua.Modle.AddListener; import mywork.com.gwcdemodaidonghua.Modle.CartBean; import mywork.com.gwcdemodaidonghua.Modle.CartListLoadListener; import mywork.com.gwcdemodaidonghua.Modle.DatailBean; import mywork.com.gwcdemodaidonghua.Modle.DetailListener; import mywork.com.gwcdemodaidonghua.Modle.Imodle; import mywork.com.gwcdemodaidonghua.View.ICartView; import mywork.com.gwcdemodaidonghua.View.ImainView;
public class presenterimpl implements Ipresenter { @Override public void showdatato(Imodle imodle, final ImainView imainView) { Map<String, String> map = new HashMap<String, String>(); map.put("pid", "17"); imodle.getDetailData(HttpConfig.detail_url, map, new DetailListener() { @Override public void detaloadsuccess(String json) { Gson gson = new Gson(); DatailBean datailBean = gson.fromJson(json, DatailBean.class); imainView.showDetailData(datailBean); } @Override public void detaloaderror(String error) { imainView.showAddError(); } }); } //跳转 @Override public void jumptocart(ImainView imainView) { imainView.jumpcat(); } @Override public void showDataToCart(final Context context, Imodle iModel, final ICartView iCartView) { Map<String, String> map = new HashMap<String, String>(); map.put("uid", "71"); iModel.showDataToCart(HttpConfig.cartList_url, map, new CartListLoadListener() { @Override public void loadCartSuccess(String json) { //将json---Bean Gson gson = new Gson(); CartBean cartBean = gson.fromJson(json, CartBean.class); //调用view的回调 iCartView.showDataToCart(context, cartBean); } @Override public void loadCartError(String error) { } }); } @Override public void jisuan(Imodle iModel, CartBean cartBean, ICartView iCartView) { double sum = iModel.calcaulate(cartBean); iCartView.showSum(sum); } @Override public void addcart(Imodle imodle, final ImainView imainView) { Map<String,String> map = new HashMap<String,String>(); map.put("pid",imainView.getpid()); map.put("uid","71"); imodle.addToCart(HttpConfig.add_url, map, new AddListener() { @Override public void addsuccess(String json) { imainView.showAddSucess(); } @Override public void adderror(String error) { imainView.showAddError(); } }); } }
//View层
//AnimationUtils类
import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Context; import android.util.DisplayMetrics; import android.view.View;
public class AnimationUtils { public static AnimatorSet setAnimatorset(Context context, View view){ //获取屏幕高度 DisplayMetrics metrics = context.getResources().getDisplayMetrics(); int pixels = metrics.heightPixels; ValueAnimator translation = ObjectAnimator.ofFloat(view,"translationY",0,pixels/2-10); ValueAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX",2.0f,1.0f); ValueAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY",2.0f,1.0f); ValueAnimator alpha = ObjectAnimator.ofFloat(view,"alpha",0,1.0f); ValueAnimator rotation = ObjectAnimator.ofFloat(view,"rotation",0,360); AnimatorSet set = new AnimatorSet(); set.playTogether(translation,scaleX,scaleY,alpha,rotation); set.setDuration(3000); set.start(); return set; } }
//建立一个Activity
//CartActivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import java.util.List; import mywork.com.gwcdemodaidonghua.Modle.CartBean; import mywork.com.gwcdemodaidonghua.Modle.ModleImpl; import mywork.com.gwcdemodaidonghua.Modle.MyAdapter; import mywork.com.gwcdemodaidonghua.Presenter.presenterimpl; import mywork.com.gwcdemodaidonghua.R;
public class CartActivity extends AppCompatActivity implements ICartView,OnClickListener{ private ExpandableListView ex_list_view; private CheckBox cart_cb; private TextView cart_sum; private CartBean carBean; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cart); initView(); presenterimpl presenter = new presenterimpl(); presenter.showDataToCart(this, new ModleImpl(), this); } private void initView() { ex_list_view = findViewById(R.id.ex_list_view); cart_cb = findViewById(R.id.cart_cb); cart_sum = findViewById(R.id.cart_sum12); cart_cb.setOnClickListener(this); } @Override public void showDataToCart(Context context, final CartBean cartBean) { this.carBean = cartBean; //设置适配器 myAdapter = new MyAdapter(context, cartBean, this); ex_list_view.setAdapter(myAdapter); //将expanablelistview全部展开 int childCount = myAdapter.getGroupCount(); for (int i = 0; i < childCount; i++) { ex_list_view.expandGroup(i); } } @Override public void showSum(double sum) { cart_sum.setText("总价"+sum); } @Override public CartBean getCartBean() { return carBean; } @Override public void onClick(View view) { switch(view.getId()){ case R.id.cart_cb: List<CartBean.DataBean> data = carBean.getData(); for (int i = 0; i < data.size(); i++) { data.get(i).setParentIsSelected(!data.get(i).isParentIsSelected()); List<CartBean.ChildBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { list.get(j).setChildIsSelected(!list.get(j).isChildIsSelected()); if(!list.get(j).isChildIsSelected()){ data.get(i).setParentIsSelected(list.get(j).isChildIsSelected()); } } } presenterimpl presenterimpl = new presenterimpl(); presenterimpl.jisuan(new ModleImpl(),carBean,CartActivity.this); myAdapter.notifyDataSetChanged(); break; } } }
//ICartView接口
public interface ICartView { //显示 void showDataToCart(Context context, CartBean cartBean); //显示总价 void showSum(double sum); CartBean getCartBean(); }
// ImainView接口
public interface ImainView { void showDetailData(DatailBean datailBean); void jumpcat(); void addcart(); String getpid(); void showAddSucess(); void showAddError(); }
//MainActivity类
import android.content.Intent; import android.graphics.Paint; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import mywork.com.gwcdemodaidonghua.Modle.DatailBean; import mywork.com.gwcdemodaidonghua.Modle.ModleImpl; import mywork.com.gwcdemodaidonghua.Presenter.presenterimpl; import mywork.com.gwcdemodaidonghua.R;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,ImainView { private ImageView iv_show; private TextView man_name; private TextView main_price; private TextView main_price2; private ImageView iv; private DatailBean datailBean; private presenterimpl presenterimpl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); inview(); } private void inview() { iv_show = findViewById(R.id.iv_show); man_name = findViewById(R.id.main_name); main_price = findViewById(R.id.main_price); main_price2 = findViewById(R.id.main_price2); TextView main_cart = findViewById(R.id.main_cart); TextView main_add = findViewById(R.id.main_add); main_cart.setOnClickListener(MainActivity.this); main_add.setOnClickListener(MainActivity.this); presenterimpl = new presenterimpl(); presenterimpl.showdatato(new ModleImpl(),MainActivity.this); } @Override public void onClick(View view) { switch(view.getId()){ case R.id.main_cart: presenterimpl.jumptocart(this); break; case R.id.main_add: presenterimpl.addcart(new ModleImpl(),MainActivity.this); break; } } @Override public void showDetailData(DatailBean datailBean) { this.datailBean = datailBean; String images = datailBean.getData().getImages(); String imageurl = images.split(".jpg")[0] + ".jpg"; Glide.with(MainActivity.this).load(imageurl).into(iv_show); man_name.setText(datailBean.getData().getTitle()); main_price.setText("原价"+datailBean.getData().getPrice()+""); main_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); main_price2.setText("折扣价"+datailBean.getData().getBargainPrice()+""); } @Override public void jumpcat() { startActivity(new Intent(MainActivity.this,CartActivity.class)); overridePendingTransition(R.anim.in,R.anim.out); finish(); } @Override public void addcart() { } @Override public String getpid() { return datailBean.getData().getPid()+""; } @Override public void showAddSucess() { Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show(); } @Override public void showAddError() { Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show(); } }
//MyView类
import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import mywork.com.gwcdemodaidonghua.Modle.UiBean; import mywork.com.gwcdemodaidonghua.R;
public class MyView extends LinearLayout { public int i=1; private TextView jia; private TextView jian; private EditText content; private UiBean uibean; public MyView(Context context) { this(context,null); } public MyView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.activity_jiajian, this,true); jia = view.findViewById(R.id.jia); jian = view.findViewById(R.id.jian); content = view.findViewById(R.id.content); uibean = new UiBean(jia,jian,content); } public UiBean getet(){ if(uibean!=null){ return uibean; }else{ uibean = new UiBean(jia,jian,content); return uibean; } } public void aa(int a){ content.setText(a+""); } }