mvp二级购物车的实现
来源:互联网 发布:怎么给淘宝供货 编辑:程序博客网 时间:2024/06/06 04:47
这个项目需要的东西
这是MainActivity下的
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;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 android.widget.Toast;import mygwc.bwei.com.mygwc.R;import mygwc.bwei.com.mygwc.adapter.MyAdapter;import mygwc.bwei.com.mygwc.bean.GwcBean;import mygwc.bwei.com.mygwc.presenter.MyPresenter;import mygwc.bwei.com.mygwc.view.IView;public class MainActivity extends AppCompatActivity implements IView { MyPresenter Presenter=new MyPresenter(this,this); private RecyclerView rv; private CheckBox allcheck; private TextView allnum; private TextView allprice; MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rv = (RecyclerView) findViewById(R.id.rv); allcheck = (CheckBox) findViewById(R.id.allcheck); allnum = (TextView) findViewById(R.id.allnum); allprice = (TextView) findViewById(R.id.allprice); Presenter.getnetdata(); LinearLayoutManager manager=new LinearLayoutManager(this); rv.setLayoutManager(manager); } @Override public void showdata(GwcBean bean) { adapter=new MyAdapter(MainActivity.this,bean); rv.setAdapter(adapter); adapter.blgwc(); allcheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { adapter.selectAll(allcheck.isChecked()); } }); adapter.setListener(new MyAdapter.UpdateUiListener() { @Override public void setTotal(String total, String num, boolean allCheck) { allcheck.setChecked(allCheck); allnum.setText("共"+num+"件商品"); allprice.setText("共"+total+"¥"); } }); adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this,"您正在点击的是第"+position+"条目",Toast.LENGTH_SHORT).show(); } }); }}
配置类adapter
import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import mygwc.bwei.com.mygwc.R;import mygwc.bwei.com.mygwc.bean.GwcBean;import mygwc.bwei.com.mygwc.myview.MyPuls;/** * Created by DELL on 2017/11/23. */public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myholder> implements View.OnClickListener{ Context context; GwcBean bean; List<GwcBean.DataBean.ListBean> list; Map<String, String> map = new HashMap<>(); public MyAdapter(Context context, GwcBean bean) { this.context = context; this.bean = bean; } public void blgwc() { if (this.list == null) { this.list = new ArrayList<>(); } // 遍历商家 for (GwcBean.DataBean shop : bean.getData()) { map.put(shop.getSellerid(), shop.getSellerName()); // 遍历商品 for (int i = 0; i < shop.getList().size(); i++) { this.list.add(shop.getList().get(i)); } } setFirst(this.list); notifyDataSetChanged(); } private void setFirst(List<GwcBean.DataBean.ListBean> list) { if (list.size() > 0) { list.get(0).setIsFirst(1); for (int i = 1; i < list.size(); i++) { if (list.get(i).getSellerid() == list.get(i - 1).getSellerid()) { list.get(i).setIsFirst(2); } else { list.get(i).setIsFirst(1); if (list.get(i).isItemSelected()) { list.get(i).setShopSelected(list.get(i).isItemSelected()); } } } } } @Override public MyAdapter.myholder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate(context,R.layout.item_layout,null); myholder myholder=new myholder(view); view.setOnClickListener(this); return myholder; } @Override public void onBindViewHolder(final MyAdapter.myholder holder, final int position) { holder.itemView.setTag(position); if (list.get(position).getIsFirst() == 1){ //显示商家 holder.shopcheck.setVisibility(View.VISIBLE); holder.shopname.setVisibility(View.VISIBLE); holder.shopcheck.setChecked(list.get(position).isShopSelected()); // 显示商家的名称 // list.get(position).getSellerid() 取到商家的id // map.get()取到 商家的名称 holder.shopname.setText(map.get(String.valueOf(list.get(position).getSellerid()))); } else { holder.shopcheck.setVisibility(View.GONE); holder.shopname.setVisibility(View.GONE); } holder.itemcheck.setChecked(list.get(position).isItemSelected()); String imgs=list.get(position).getImages(); String [] data=imgs.split("\\|"); Glide.with(context).load(data[0]).into(holder.itemiv); holder.itemname.setText(list.get(position).getTitle()); holder.itemprice.setText(list.get(position).getPrice()+"¥"); holder.plus.resetnum(list.get(position).getNum()); holder.shopcheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.get(position).setShopSelected(holder.shopcheck.isChecked()); for(int i=0;i<list.size();i++){ if(list.get(position).getSellerid() == list.get(i).getSellerid()){ list.get(i).setItemSelected(holder.shopcheck.isChecked()); } } notifyDataSetChanged(); sum(list); } }); holder.itemcheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.get(position).setItemSelected(holder.itemcheck.isChecked()); for(int i=0;i<list.size();i++){ for (int j=0;j<list.size();j++){ if(list.get(i).getSellerid() == list.get(j).getSellerid() && !list.get(j).isItemSelected()){ list.get(i).setShopSelected(false); break; }else { list.get(i).setShopSelected(true); } } } notifyDataSetChanged(); sum(list); list.get(position).setItemSelected(holder.itemcheck.isChecked()); for(int i=0;i<list.size();i++){ for (int j=0;j<list.size();j++){ if(list.get(i).getSellerid() == list.get(j).getSellerid() && !list.get(j).isItemSelected()){ list.get(i).setShopSelected(false); break; }else { list.get(i).setShopSelected(true); } } } notifyDataSetChanged(); sum(list); } }); holder.mDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { list.remove(position); setFirst(list); notifyDataSetChanged(); sum(list); } }); //MyPuls删了,从新打 holder.plus.lianjie(new MyPuls.getnownum() { @Override public void nownum(int count) { list.get(position).setNum(count); notifyDataSetChanged(); sum(list); } }); } private void sum(List<GwcBean.DataBean.ListBean> list){ int totalNum = 0 ; float totalMoney = 0.0f; boolean allCheck =true; for(int i=0;i<list.size();i++){ if(list.get(i).isItemSelected()){ totalNum += list.get(i).getNum() ; totalMoney += list.get(i).getNum() * list.get(i).getPrice(); }else { allCheck = false; } } listener.setTotal(totalMoney+"",totalNum+"",allCheck); } public void selectAll(boolean check){ for(int i=0;i<list.size();i++){ list.get(i).setShopSelected(check); list.get(i).setItemSelected(check); } notifyDataSetChanged(); sum(list); } @Override public int getItemCount() { return list == null ? 0 : list.size(); } public static interface OnItemClickListener { void onItemClick(View view , int position); } private OnItemClickListener mOnItemClickListener = null; @Override public void onClick(View v) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取position mOnItemClickListener.onItemClick(v,(int)v.getTag()); } } public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } class myholder extends RecyclerView.ViewHolder{ private CheckBox shopcheck; private TextView shopname; private CheckBox itemcheck; private ImageView itemiv; private TextView itemname; private TextView itemprice; private MyPuls plus; private Button mDel; public myholder(View itemView) { super(itemView); shopcheck = (CheckBox) itemView.findViewById(R.id.shop_check); shopname = (TextView) itemView.findViewById(R.id.shop_name); itemcheck = (CheckBox) itemView.findViewById(R.id.item_check); itemiv = (ImageView) itemView.findViewById(R.id.item_iv); itemname = (TextView) itemView.findViewById(R.id.item_name); itemprice = (TextView) itemView.findViewById(R.id.item_price); plus=itemView.findViewById(R.id.puls); mDel = (Button) itemView.findViewById(R.id.del); } } public UpdateUiListener listener; public void setListener(UpdateUiListener listener){ this.listener = listener; } public interface UpdateUiListener { public void setTotal(String total,String num,boolean allCheck); }}Bean类的各种属性自己创建Model的类接口import okhttp3.Callback;/** * Created by DELL on 2017/11/23. */public interface IModel { public void getgwcdata(Callback callback);}model的实现类mport mygwc.bwei.com.mygwc.util.OkHttp3Utils;import okhttp3.Callback;/** * Created by DELL on 2017/11/23. */public class MyModel implements IModel{ @Override public void getgwcdata(Callback callback) {地址数据 OkHttp3Utils.doGet("http://120.27.23.105/product/getCarts?uid=100",callback); }}mypuls的自定义控件import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.RelativeLayout;import mygwc.bwei.com.mygwc.R;/** * Created by DELL on 2017/11/23. */public class MyPuls extends RelativeLayout { private Button jia; private Button jian; private EditText mNumPlus; private int num=1; public MyPuls(Context context) { super(context); } public MyPuls(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view=View.inflate(context, R.layout.puls_layout,null); jia = view.findViewById(R.id.jia); jian = view.findViewById(R.id.jian); mNumPlus = view.findViewById(R.id.plus_num); addView(view); jia.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { String s=mNumPlus.getText().toString(); int i=Integer.parseInt(s); if(i>1){ i--; num=i; mNumPlus.setText(i+""); if(lisenear!=null){ lisenear.nownum(num); } } } }); jian.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { String s=mNumPlus.getText().toString(); int i=Integer.parseInt(s); i++; num=i; mNumPlus.setText(i+""); if(lisenear!=null){ lisenear.nownum(num); } } }); } public MyPuls(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public getnownum lisenear; public void resetnum(int i){ mNumPlus.setText(i+""); } public void lianjie(getnownum lisenear){ this.lisenear=lisenear; } public interface getnownum{ public void nownum(int count); }}presenter类的数据import android.content.Context;import com.google.gson.Gson;import java.io.IOException;import mygwc.bwei.com.mygwc.bean.GwcBean;import mygwc.bwei.com.mygwc.model.MyModel;import mygwc.bwei.com.mygwc.util.OnUiCallback;import mygwc.bwei.com.mygwc.view.IView;import okhttp3.Call;/** * Created by DELL on 2017/11/23. */public class MyPresenter { Context context; IView view; MyModel model; public MyPresenter(Context context, IView view) { this.context = context; this.view = view; model = new MyModel(); } public void getnetdata(){ model.getgwcdata(new OnUiCallback() { @Override public void onFailed(Call call, IOException e) { } @Override public void onSuccess(String result) { Gson gson=new Gson(); GwcBean bean = gson.fromJson(result, GwcBean.class); view.showdata(bean); } }); }}utils类的配置类mvp的view类public interface IView { public void showdata(GwcBean bean);}XML的mainactiv的<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="mygwc.bwei.com.mygwc.activity.MainActivity"><android.support.v7.widget.RecyclerView android:layout_above="@+id/foot" android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView><RelativeLayout android:layout_alignParentBottom="true" android:id="@+id/foot" android:layout_width="match_parent" android:layout_height="80dp"> <CheckBox android:id="@+id/allcheck" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/aa" android:layout_toRightOf="@+id/allcheck" android:textColor="@android:color/black" android:text="全选" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_marginLeft="20sp" android:layout_toRightOf="@+id/aa" android:id="@+id/body" android:layout_width="150dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_toLeftOf="@+id/js" > <TextView android:id="@+id/allnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="共asdsadasd件商品" android:layout_marginBottom="10dp" /> <TextView android:id="@+id/allprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="共adasdsa钱" /> </LinearLayout> <Button android:layout_alignParentRight="true" android:id="@+id/js" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="结算" android:textColor="@android:color/white" android:background="@android:color/holo_orange_light" /></RelativeLayout></RelativeLayout>recycle的item条目<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:id="@+id/head" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/shop_check" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/shop_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="京东自营" /></LinearLayout> <RelativeLayout android:layout_below="@+id/head" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/item_check" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/item_iv" android:layout_width="80dp" android:layout_height="80dp" android:layout_toRightOf="@+id/item_check" android:src="@mipmap/ic_launcher" /> <LinearLayout android:id="@+id/data" android:layout_toRightOf="@+id/item_iv" android:layout_width="200dp" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="杜蕾斯" /> <TextView android:id="@+id/item_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥56.0" /> <mygwc.bwei.com.mygwc.myview.MyPuls android:id="@+id/puls" android:layout_width="wrap_content" android:layout_height="wrap_content"></mygwc.bwei.com.mygwc.myview.MyPuls> </LinearLayout> <Button android:layout_toRightOf="@+id/data" android:id="@+id/del" android:layout_width="match_parent" android:layout_height="80dp" android:background="@android:color/holo_red_dark" android:text="删除" android:textColor="@android:color/white" /> </RelativeLayout></RelativeLayout>XML的自定义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="match_parent"><Button android:id="@+id/jian" android:layout_width="50dp" android:layout_height="50dp" android:text="-" /> <EditText android:id="@+id/plus_num" android:layout_width="60dp" android:layout_height="50dp" android:text="1" android:gravity="center" /> <Button android:id="@+id/jia" android:layout_width="50dp" android:layout_height="50dp" android:text="+" /></LinearLayout>
阅读全文
0 0
- mvp二级购物车的实现
- MVP实现二级购物车
- MVP实现二级购物车
- Android用MVP实现的二级列表购物车
- Android实现带商家的二级购物车+MVP
- MVP+Retrofit实现二级购物车
- 二级购物车+MVP
- MVP二级购物车
- MVP二级购物车
- MVP二级购物车
- MVP模式下的二级购物车
- 系统的MVP购物车(二级购物车全)
- MVP做二级购物车
- MVP模式二级购物车
- 购物车+MVP+二级列表
- Android二级购物车MVP实现(布局篇)
- android购物车二级列表实现+MVP+Okhttp
- MVP+OKhttp+拦截器+RecyclerView+自定义view 实现请求网络数据的二级列表购物车
- 四个字: 整 ~借 ~学 ~变
- 几个常用的算法学习网站
- CENTOS6.5 编译安装 LNMP
- 存储器(六)
- 从软件外包到阿里技术专家再到CTO,他究竟是如何一路晋升?
- mvp二级购物车的实现
- 吴恩达 《神经网络与深度学习》笔记(持续更新)
- Javascript模块化编程
- Ubuntu_SSH无密码登录
- C语言基础(二)
- 矩:数学矩-图像矩
- Mybatis的学习(二)
- CSP 2016_4_4 游戏
- Servlet