//需要导的包
compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.squareup.okhttp3:logging-interceptor:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.android.support:recyclerview-v7:24.0.0-alpha1' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'org.greenrobot:eventbus:3.0.0'
//需要导入的资源文件
//工程名
//网络请求HttpUtils
public class HttpUtils { private static HttpUtils httpUtils; private final OkHttpClient httpClient; public HttpUtils() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient = new OkHttpClient.Builder() .addInterceptor(logging) .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){ Request request = new Request.Builder().url(url).build(); httpClient.newCall(request).enqueue(callback); } }
//网络请求是否成功OnNewListener
public interface OnNewListener<T> { public void onSuccess(T t); public void onFailure(Exception e); }
//请求数据的地址Api
public class Api { public static final String JSONURL="http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation"; }
//IShowModel
public interface IShowModel { public void getGoods(OnNewListener<UserBean> onNewListener); }
//解析数据ShowModel
public class ShowModel implements IShowModel{ private Handler handler=new Handler(Looper.getMainLooper()); @Override public void getGoods(final OnNewListener<UserBean> onNewListener) { HttpUtils.getHttpUtils().doGet(Api.JSONURL, 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 UserBean userBean = new Gson().fromJson(string, UserBean.class); handler.post(new Runnable() { @Override public void run() { onNewListener.onSuccess(userBean); } }); } }); } }
//ShowPresenter
public class ShowPresenter { private IMainActivity iMainActivity; private final IShowModel iShowModel; public ShowPresenter(IMainActivity iMainActivity) { this.iMainActivity=iMainActivity; iShowModel = new ShowModel(); } public void getGoods(){ iShowModel.getGoods(new OnNewListener<UserBean>() { @Override public void onSuccess(UserBean userBean) { List<UserBean.DataBean> data = userBean.getData(); List<UserBean.DataBean.DatasBean> show=new ArrayList<UserBean.DataBean.DatasBean>(); for (int i = 0; i <data.size() ; i++) { List<UserBean.DataBean.DatasBean> datas = data.get(i).getDatas(); show.addAll(datas); } iMainActivity.showList(show); } @Override public void onFailure(Exception e) { } }); } }
//IMainActivity
public interface IMainActivity { public void showList(List<UserBean.DataBean.DatasBean> show); }
//适配器MyAdaper
public class MyAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ //定义上下文,和集合 private Context context; private List<UserBean.DataBean.DatasBean> list; public MyAdaper(Context context, List<UserBean.DataBean.DatasBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=LayoutInflater.from(context).inflate(R.layout.child,parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { final UserBean.DataBean.DatasBean datasBean = list.get(position); final MyViewHolder myViewHolder= (MyViewHolder) holder; myViewHolder.cbChild.setChecked(datasBean.isChecked()); myViewHolder.tv_tel.setText(datasBean.getType_name()); myViewHolder.tv_content.setText(datasBean.getMsg()); myViewHolder.tv_time.setText(datasBean.getAdd_time()); myViewHolder.tv_price.setText(datasBean.getPrice()+"");//这里必须加引号 myViewHolder.myView.setNum(datasBean.getNum()+"");//这里必须加引号 myViewHolder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { datasBean.setChecked(myViewHolder.cbChild.isChecked()); PriceAndCountEvent compute=compute(); EventBus.getDefault().post(compute); if(myViewHolder.cbChild.isChecked()){ if(isAllCbSelected()){ //改变全选的状态 changeAllCbState(true); } }else{ changeAllCbState(false); } notifyDataSetChanged(); } }); //给加号设置点击事件 myViewHolder.myView.setAddClick(new View.OnClickListener() { @Override public void onClick(View v) { int num = myViewHolder.myView.getNum(); num++; datasBean.setNum(num); if(myViewHolder.cbChild.isChecked()){ EventBus.getDefault().post(compute()); } notifyDataSetChanged(); } }); //给减号设置点击事件 myViewHolder.myView.setDelClick(new View.OnClickListener() { @Override public void onClick(View v) { int num = myViewHolder.myView.getNum(); if(num==1){ return; } num--; datasBean.setNum(num); if(myViewHolder.cbChild.isChecked()){ EventBus.getDefault().post(compute()); } notifyDataSetChanged(); } }); //进行删除 myViewHolder.tv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private final CheckBox cbChild; private final TextView tv_tel; private final TextView tv_content; private final TextView tv_time; private final TextView tv_price; private MyView myView; private final TextView tv_del; public MyViewHolder(View itemView) { super(itemView); cbChild = (CheckBox) itemView.findViewById(R.id.cb_child); tv_tel = (TextView) itemView.findViewById(R.id.tv_tel); tv_content = (TextView) itemView.findViewById(R.id.tv_content); tv_time = (TextView) itemView.findViewById(R.id.tv_time); tv_price = (TextView) itemView.findViewById(R.id.tv_pri); tv_del = (TextView) itemView.findViewById(R.id.tv_del); myView = (MyView) itemView.findViewById(R.id.mv); } } /** * 改变全选的状态 * @param flag */ private void changeAllCbState(boolean flag){ MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); } private boolean isAllCbSelected(){ for(int i=0;i<list.size();i++){ UserBean.DataBean.DatasBean datasBean = list.get(i); if(!datasBean.isChecked()){ return false; } } return true; } //计算价钱和数量 private PriceAndCountEvent compute(){ int price=0; int count=0; for (int i = 0; i <list.size() ; i++) { UserBean.DataBean.DatasBean datasBean = list.get(i); if(datasBean.isChecked()){ price+=datasBean.getPrice()*datasBean.getNum(); count+=datasBean.getNum(); } } //把钱和数量都反回去 PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setPrice(price); priceAndCountEvent.setCount(count); return priceAndCountEvent; } public void allSelect(boolean flag){ for (int i = 0; i <list.size() ; i++) { UserBean.DataBean.DatasBean datasBean = list.get(i); datasBean.setChecked(flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }
//计算价钱和数量EvenBas
public class PriceAndCountEvent { private int price; private int count; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
//设置主界面的全选按钮
public class MessageEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } }
//主界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yuekaochongci.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#990000ff" android:gravity="center" android:text="购物车" android:textColor="#ff3660" android:textSize="25sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:gravity="center_vertical"> <CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:focusable="false" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/checkbox2" android:gravity="center_vertical" android:text="全选" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="合计 :" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="10dp" android:paddingRight="10dp" android:text="0" android:textColor="@android:color/holo_red_light" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="50dp" android:background="@android:color/holo_red_dark" android:gravity="center" android:padding="10dp" android:text="结算(0)" android:textColor="@android:color/white" /> </LinearLayout> </RelativeLayout> </LinearLayout>
//主方法里面的逻辑
public class MainActivity extends AppCompatActivity implements IMainActivity, View.OnClickListener { private ExpandableListView mElv; private CheckBox mCheckbox2; /** * 0 */ private TextView mTvPrice; /** * 结算(0) */ private TextView mTvNum; private LinearLayout mActivityMain; private RecyclerView mRv; private MyAdaper myAdaper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); new ShowPresenter(this).getGoods(); initView(); mCheckbox2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAdaper.allSelect(mCheckbox2.isChecked()); } }); } private void initView() { mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2); mTvPrice = (TextView) findViewById(R.id.tv_price); mTvNum = (TextView) findViewById(R.id.tv_num); mActivityMain = (LinearLayout) findViewById(R.id.activity_main); mRv = (RecyclerView) findViewById(R.id.rv); mRv.setOnClickListener(this); mRv.setLayoutManager(new LinearLayoutManager(this)); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void onMessageEvent(MessageEvent event){ mCheckbox2.setChecked(event.isChecked()); } //调用计算价钱和数量的方法 @Subscribe public void onMessageEvent(PriceAndCountEvent event){ mTvNum.setText("结算("+event.getCount()+")"); mTvPrice.setText(event.getPrice()+""); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.rv: break; } } @Override public void showList(List<UserBean.DataBean.DatasBean> show) { //设置适配器 myAdaper = new MyAdaper(this, show); mRv.setAdapter(myAdaper); } }
//自定义view加减号
public class MyView extends LinearLayout{ private View view; private ImageView iv_add; private ImageView iv_del; private TextView tv_num; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); view = LayoutInflater.from(context).inflate(R.layout.myview,this); iv_add = (ImageView) findViewById(R.id.iv_add); iv_del = (ImageView) findViewById(R.id.iv_del); tv_num = (TextView) findViewById(R.id.tv_num); } //给加号设置点击事件 public void setAddClick(OnClickListener onClickListener){ iv_add.setOnClickListener(onClickListener); } //给减号设置点击事件 public void setDelClick(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); } }
//自定义view的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:gravity="center_vertical" android:layout_height="match_parent"> <ImageView android:id="@+id/iv_del" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/shopcart_minus_grey" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/shopcart_add_btn" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" android:text="1" /> <ImageView android:id="@+id/iv_add" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="5dp" android:src="@drawable/shopcart_add_red" /> </LinearLayout>
//适配器里面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/darker_gray" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginLeft="40dp" android:layout_marginTop="30dp" android:focusable="false" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="iphone6" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="什么手机" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="2016-12-10" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tv_pri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥3000.00" /> <com.example.yuekaochongci.view.MyView android:id="@+id/mv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/tv_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /> </LinearLayout>