先添加依赖:
//ok依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
//拦截器依赖 compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
Gson依赖 compile 'com.google.code.gson:gson:2.8.2'
Event依赖 compile 'org.greenrobot:eventbus:3.1.1'
//列表依赖 compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'布局:
主布局:
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="vertical" tools:context="chencheng.bwie.com.lianxi11day22_1.view.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/colorPrimaryDark" android:text="购物车" android:textSize="25sp" android:gravity="center" android:textColor="#ff0000"/> <android.support.v7.widget.RecyclerView android:id="@+id/review" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1">android.support.v7.widget.RecyclerView> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <CheckBox android:id="@+id/cb_object" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="全选" android:textSize="20sp" /> <TextView android:id="@+id/tv_sum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="150dp" android:text="合计:0" android:textSize="20sp"/> <TextView android:id="@+id/tv_num" android:layout_width="100dp" android:layout_height="match_parent" android:background="#cc0000" android:layout_alignParentRight="true" android:text="结算(0)" android:textSize="20sp" android:gravity="center" /> RelativeLayout> LinearLayout>列表布局:
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="100dp" android:layout_margin="5dp" android:background="#ffff00" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_alignParentTop="true" android:layout_alignParentStart="true"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" android:padding="5dp" android:layout_weight="1"/> <TextView android:id="@+id/jieshao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" android:padding="5dp" android:layout_weight="1"/> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好" android:padding="5dp" android:layout_weight="1"/> LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:layout_marginLeft="20dp"> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="30dp" android:layout_marginTop="20dp" android:text="300" android:textSize="20sp" /> <chencheng.bwie.com.lianxi11day22_1.view.MyViw android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> chencheng.bwie.com.lianxi11day22_1.view.MyViw> LinearLayout> LinearLayout> <TextView android:id="@+id/tv_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:text="删除" android:layout_centerVertical="true"/> RelativeLayout>自定义View:
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:orientation="horizontal"> <ImageView android:id="@+id/iv_add" android:layout_width="20dp" android:layout_height="20dp" android:gravity="center_vertical" android:src="@drawable/shopcart_minus_red"/> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shopcart_asd" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingBottom="2dp" android:paddingTop="2dp" android:gravity="center_vertical" android:text="1" /> <ImageView android:id="@+id/iv_del" android:layout_width="20dp" android:layout_height="20dp" android:gravity="center_vertical" android:src="@drawable/shopcart_add_red"/> LinearLayout>给TextView设置背景:
xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="200dp">corners> <stroke android:color="@color/colorPrimaryDark" android:width="1dp">stroke> shape>net网络请求框架:
HttpUtils:
package chencheng.bwie.com.lianxi11day22_1.net; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; /** * Created by dell on 2017/11/22. */ public class HttpUtils { private static HttpUtils httpUtils; private final OkHttpClient client; private HttpUtils(){ client = new OkHttpClient.Builder() //.addInterceptor(new MyInterceptor()) .build(); } public static HttpUtils getHttpUtils(){ if (httpUtils==null){ synchronized (HttpUtils.class){ if (httpUtils==null){ httpUtils=new HttpUtils(); } } } return httpUtils; } public void doGet(String url, Callback callback){ final Request build = new Request.Builder().url(url).build(); client.newCall(build).enqueue(callback); } }//网址
package chencheng.bwie.com.lianxi11day22_1.net; /** * Created by dell on 2017/11/22. */ public interface Api { public static final String url="http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation"; }自定义拦截器:
package chencheng.bwie.com.lianxi11day22_1.net; import java.io.IOException; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; /** * Created by dell on 2017/11/22. */ public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { final Request request = chain.request(); FormBody body = (FormBody) request.body(); String name=body.name(0); String value=body.value(0); final FormBody builder = new FormBody.Builder() .add(name,value) .add("source","android") .build(); Request builder1 = request.newBuilder() .url(request.url()) .post(builder) .build(); return chain.proceed(builder1); } }//判断请求成功或失败
package chencheng.bwie.com.lianxi11day22_1.net; /** * Created by dell on 2017/11/22. */ public interface OnNetListenter<T> { //成功 public void onSuccess(T t); //失败 public void onFailure(Exception e); }Model层:
package chencheng.bwie.com.lianxi11day22_1.model; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; import chencheng.bwie.com.lianxi11day22_1.net.OnNetListenter; /** * Created by dell on 2017/11/22. */ public interface IMainModel { public void getGowu(OnNetListenteronNetListenter); }
package chencheng.bwie.com.lianxi11day22_1.model; import android.os.Handler; import android.os.Looper; import com.google.gson.Gson; import java.io.IOException; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; import chencheng.bwie.com.lianxi11day22_1.net.Api; import chencheng.bwie.com.lianxi11day22_1.net.HttpUtils; import chencheng.bwie.com.lianxi11day22_1.net.OnNetListenter; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Response; /** * Created by dell on 2017/11/22. */ public class MainModel implements IMainModel { private Handler handler=new Handler(Looper.getMainLooper()); @Override public void getGowu(final OnNetListenterPresenter层:onNetListenter) { HttpUtils.getHttpUtils().doGet(Api.url, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string= response.body().string(); final Goowu goowu = new Gson().fromJson(string, Goowu.class); handler.post(new Runnable() { @Override public void run() { onNetListenter.onSuccess(goowu); } }); } }); } }
package chencheng.bwie.com.lianxi11day22_1.presenter; import java.util.ArrayList; import java.util.List; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; import chencheng.bwie.com.lianxi11day22_1.model.IMainModel; import chencheng.bwie.com.lianxi11day22_1.model.MainModel; import chencheng.bwie.com.lianxi11day22_1.net.OnNetListenter; import chencheng.bwie.com.lianxi11day22_1.view.IMainActivity; /** * Created by dell on 2017/11/22. */ public class MainPresenter { private IMainActivity iMainActivity; private final IMainModel iMainModel; public MainPresenter(IMainActivity iMainActivity){ this.iMainActivity=iMainActivity; iMainModel = new MainModel(); } public void doGoods(){ iMainModel.getGowu(new OnNetListenterView层:() { @Override public void onSuccess(Goowu goowu) { List list=new ArrayList (); List data=goowu.getData(); for (int i=0;i datas=data.get(i).getDatas(); list.addAll(datas); } iMainActivity.showList(list); } @Override public void onFailure(Exception e) { } }); } }
package chencheng.bwie.com.lianxi11day22_1.view; import java.util.List; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; /** * Created by dell on 2017/11/22. */ public interface IMainActivity { public void showList(List主类:list); }
package chencheng.bwie.com.lianxi11day22_1.view; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.CheckBox; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.List; import chencheng.bwie.com.lianxi11day22_1.R; import chencheng.bwie.com.lianxi11day22_1.adapter.MyAdapter; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; import chencheng.bwie.com.lianxi11day22_1.evenbus.ConMpRrice; import chencheng.bwie.com.lianxi11day22_1.evenbus.MessageEvent; import chencheng.bwie.com.lianxi11day22_1.presenter.MainPresenter; public class MainActivity extends AppCompatActivity implements IMainActivity{ private RecyclerView mReview; /** * 全选 */ private CheckBox mCbObject; /** * 合计:0 */ private TextView mTvSum; /** * 结算(0) */ private TextView mTvNum; MainPresenter mainPresenter; MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); mainPresenter=new MainPresenter(this); initView(); mReview.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); mainPresenter.doGoods(); mCbObject.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { myAdapter.IsAll(mCbObject.isChecked()); } }); } private void initView() { mReview = (RecyclerView) findViewById(R.id.review); mCbObject = (CheckBox) findViewById(R.id.cb_object); mTvSum = (TextView) findViewById(R.id.tv_sum); mTvNum = (TextView) findViewById(R.id.tv_num); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void onMessageEvent(MessageEvent event){ mCbObject.setChecked(event.isCheckd()); } @Subscribe public void onMessageEvent(ConMpRrice event) { mTvNum.setText("结算("+event.getNum()+")"); mTvSum.setText("合计:"+ event.getSum()); } @Override public void showList(List自定义View:list) { myAdapter = new MyAdapter(MainActivity.this, list); mReview.setAdapter(myAdapter); } }
package chencheng.bwie.com.lianxi11day22_1.view; import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import chencheng.bwie.com.lianxi11day22_1.R; /** * Created by dell on 2017/11/22. */ public class MyViw extends LinearLayout{ private ImageView iv_add; private ImageView iv_del; private TextView tv_num; public MyViw(Context context) { this(context,null); } public MyViw(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view= LayoutInflater.from(context).inflate(R.layout.tiem_myview,this); iv_add=findViewById(R.id.iv_add); iv_del=findViewById(R.id.iv_del); tv_num=findViewById(R.id.tv_num); } public void setAddClickListener(OnClickListener onClickListener){ iv_add.setOnClickListener(onClickListener); } public void setDelClickListener(OnClickListener onClickListener){ iv_del.setOnClickListener(onClickListener); } public void setNum(String num){ tv_num.setText(num); } public int getNum(){ String num=tv_num.getText().toString(); return Integer.parseInt(num); } }
RecyclerView适配器:
package chencheng.bwie.com.lianxi11day22_1.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import java.util.List; import chencheng.bwie.com.lianxi11day22_1.R; import chencheng.bwie.com.lianxi11day22_1.bean.Goowu; import chencheng.bwie.com.lianxi11day22_1.evenbus.ConMpRrice; import chencheng.bwie.com.lianxi11day22_1.evenbus.MessageEvent; import chencheng.bwie.com.lianxi11day22_1.view.MyViw; /** * Created by dell on 2017/11/22. */ public class MyAdapter extends RecyclerView.Adapter{ private Context context; private List list; public MyAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.iten_child,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { final Goowu.DataBean.DatasBean datasBean = list.get(position); final MyViewHolder viewHolder= (MyViewHolder) holder; viewHolder.title.setText(datasBean.getType_name()); viewHolder.cb_child.setChecked(datasBean.ischeckd()); viewHolder.jieshao.setText(datasBean.getMsg()); viewHolder.time.setText(datasBean.getAdd_time()); viewHolder.price.setText(datasBean.getPrice()+""); viewHolder.myViw.setNum(datasBean.getNum()+""); viewHolder.cb_child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { datasBean.setIscheckd(viewHolder.cb_child.isChecked()); ConMpRrice conMpRrice=comput(); EventBus.getDefault().post(conMpRrice); if (viewHolder.cb_child.isChecked()){ if (isAllcheckd()){ changAllIscheck(true); } }else{ changAllIscheck(false); } notifyDataSetChanged(); } }); viewHolder.myViw.setAddClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num=viewHolder.myViw.getNum(); num++; datasBean.setNum(num); if (viewHolder.cb_child.isChecked()){ EventBus.getDefault().post(comput()); } notifyDataSetChanged(); } }); viewHolder.myViw.setDelClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num=viewHolder.myViw.getNum(); if (num==1){ return; } num--; datasBean.setNum(num); if (viewHolder.cb_child.isChecked()){ EventBus.getDefault().post(comput()); } notifyDataSetChanged(); } }); viewHolder.tv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.remove(position); EventBus.getDefault().post(comput()); notifyDataSetChanged(); } }); } //改变全选状态 public void changAllIscheck(boolean flag){ MessageEvent messageEvent=new MessageEvent(); messageEvent.setCheckd(flag); EventBus.getDefault().post(messageEvent); } public boolean isAllcheckd(){ for (int i=0;i< list.size();i++){ Goowu.DataBean.DatasBean datasBean = list.get(i); if (!datasBean.ischeckd()){ return false; } } return true; } //全选 public void IsAll(boolean flag){ for (int i=0;i<list.size();i++){ final Goowu.DataBean.DatasBean datasBean = list.get(i); datasBean.setIscheckd(flag); } EventBus.getDefault().post(comput()); notifyDataSetChanged(); } //计算钱 private ConMpRrice comput(){ int price=0; int count=0; for (int i=0;i<list.size();i++){ final Goowu.DataBean.DatasBean datasBean = list.get(i); if (datasBean.ischeckd()){ price+=datasBean.getPrice()*datasBean.getNum(); count+=datasBean.getNum(); } } final ConMpRrice conMpRrice = new ConMpRrice(); conMpRrice.setSum(price); conMpRrice.setNum(count); return conMpRrice; } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ CheckBox cb_child; TextView title; TextView jieshao; TextView price; TextView time; TextView tv_del; MyViw myViw; public MyViewHolder(View itemView) { super(itemView); cb_child=itemView.findViewById(R.id.cb_child); time=itemView.findViewById(R.id.time); title=itemView.findViewById(R.id.title); jieshao=itemView.findViewById(R.id.jieshao); price=itemView.findViewById(R.id.price); tv_del=itemView.findViewById(R.id.tv_del); myViw=itemView.findViewById(R.id.myview); } } }
package chencheng.bwie.com.lianxi11day22_1.evenbus; /** * Created by dell on 2017/11/22. */ public class ConMpRrice { private int sum; private int num; public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public ConMpRrice() { this.sum = sum; this.num = num; } }
package chencheng.bwie.com.lianxi11day22_1.evenbus; /** * Created by dell on 2017/11/22. */ public class MessageEvent { private boolean checkd; public boolean isCheckd() { return checkd; } public void setCheckd(boolean checkd) { this.checkd = checkd; } }bean层:
package chencheng.bwie.com.lianxi11day22_1.bean; import java.util.List; /** * Created by dell on 2017/11/22. */ public class Goowu { /** * code : 200 * data : [{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}],"title":"苹果","title_id":"59280"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"小米4s (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"小米5s (亮黑色)","type_sn_id":"tgg"}],"title":"小米","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"三星 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"三星 (亮黑色)","type_sn_id":"tgg"}],"title":"三星","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"华为 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"华为 (亮黑色)","type_sn_id":"tgg"},{"add_time":"2016-12-10 4:55:28","cart_id":"445164","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":150,"type_name":"华为 (纯黑色)","type_sn_id":"hgg"}],"title":"华为","title_id":"59279"}] * flag : Success * msg : 描述 */ private String code; private String flag; private String msg; private Listdata; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * datas : [{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}] * title : 苹果 * title_id : 59280 */ private String title; private String title_id; private List datas; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getTitle_id() { return title_id; } public void setTitle_id(String title_id) { this.title_id = title_id; } public List getDatas() { return datas; } public void setDatas(List datas) { this.datas = datas; } public static class DatasBean { /** * add_time : 2016-12-10 14:54:58 * cart_id : 445162 * house_id : 1 * msg : 购买渠道:大陆国行 * price : 500 * type_name : 苹果 iPhone 6(白金色) * type_sn_id : ggh */ private boolean ischeckd; private String add_time; private String cart_id; private String house_id; private String msg; private int price; private String type_name; private String type_sn_id; private int num=1; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public boolean ischeckd() { return ischeckd; } public void setIscheckd(boolean ischeckd) { this.ischeckd = ischeckd; } public String getAdd_time() { return add_time; } public void setAdd_time(String add_time) { this.add_time = add_time; } public String getCart_id() { return cart_id; } public void setCart_id(String cart_id) { this.cart_id = cart_id; } public String getHouse_id() { return house_id; } public void setHouse_id(String house_id) { this.house_id = house_id; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType_name() { return type_name; } public void setType_name(String type_name) { this.type_name = type_name; } public String getType_sn_id() { return type_sn_id; } public void setType_sn_id(String type_sn_id) { this.type_sn_id = type_sn_id; } } } }