android - mvp实现商品详情页面【仿】京东App

需求:mvp实现

  • activity_details
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ff3660"
        android:gravity="center"
        android:text="商品详情"
        android:textColor="#ffffff"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

    <TextView
        android:id="@+id/tvBargainPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/tvPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <TextView
            android:id="@+id/tvCart"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#33000000"
            android:gravity="center"
            android:text="购物车" />

        <TextView
            android:id="@+id/tvAddCart"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#33000000"
            android:gravity="center"
            android:text="加入购物车" />
    LinearLayout>
LinearLayout>

view层

  • DetailsActivity
public class DetailsActivity extends AppCompatActivity implements IDetailsListener, View.OnClickListener {
    private DetailsShopPresenter detailsPresenter;
    private ImageView mIv;
    private TextView mTvBargainPrice;
    private TextView mTvPrice;
    private TextView mTvCart;
    private TextView mTvAddCart;
    private DetailsAddPresenter addCartPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        initView();
        detailsPresenter = new DetailsShopPresenter(this);
        addCartPresenter = new DetailsAddPresenter(this);
        detailsPresenter.getProductDetail();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detailsPresenter.dettach();
        addCartPresenter.dettach();
    }

    @Override
    public void show(DetailsShopBean detailsBean) {
        String images = detailsBean.getData().getImages();
        String[] split = images.split("\\|");
        Glide.with(this).load(split[0]).into(mIv);
        TextPaint paint = mTvBargainPrice.getPaint();
        paint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        mTvBargainPrice.setText("原价:" + detailsBean.getData().getBargainPrice());
        mTvPrice.setText("优惠价:" + detailsBean.getData().getPrice());
    }

    @Override
    public void show(String str) {
        Toast.makeText(DetailsActivity.this, str, Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        mIv = (ImageView) findViewById(R.id.iv);
        mTvBargainPrice = (TextView) findViewById(R.id.tvBargainPrice);
        mTvPrice = (TextView) findViewById(R.id.tvPrice);
        mTvCart = (TextView) findViewById(R.id.tvCart);
        mTvAddCart = (TextView) findViewById(R.id.tvAddCart);
        mTvCart.setOnClickListener(this);
        mTvAddCart.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tvCart:
                Intent intent = new Intent(DetailsActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.tvAddCart:
                addCartPresenter.addCart();
                break;
        }
    }
    • IDetailsListener
public interface IDetailsListener {
    void show(DetailsShopBean detailsBean);

    void show(String str);
}

presenter层

    • DetailsShopPresenter
// 购物车
public class DetailsShopPresenter {
    private IDetailsListener iMainListener;
    private DetailsService detailsService;

    public DetailsShopPresenter(IDetailsListener iMainListener) {
        this.iMainListener = iMainListener;
        detailsService = new DetailsModel();
    }

    public void dettach() {
        iMainListener = null;
    }

    public void getProductDetail() {
        Map params = new HashMap<>();
        params.put("pid", "71");
        detailsService.getProductDetail(params, new OnNetListener() {
            @Override
            public void onSuccess(DetailsShopBean detailsBean) {
                if (iMainListener != null) {
                    iMainListener.show(detailsBean);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
    • DetailsAddPresenter
// 加入购物车
public class DetailsAddPresenter {
    private IDetailsListener iMainListener;
    private DetailsAddService addCartService;

    public DetailsAddPresenter(IDetailsListener iMainListener) {
        this.iMainListener = iMainListener;
        addCartService = new DetailsAddModel();
    }

    public void dettach() {
        iMainListener = null;
    }

    public void addCart() {
        Map params = new HashMap<>();
        params.put("pid", "71");
        params.put("uid", "39");
        addCartService.addCart(params, new OnNetListener() {
            @Override
            public void onSuccess(DetailsAddBean addCartBean) {
                if (iMainListener != null) {
                    iMainListener.show(addCartBean.getCode().equals("0") ? "添加成功了" : "添加失败了");
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

model层

  • DetailsAddService
public interface DetailsAddService {
    void addCart(Map params, OnNetListener onNetListener);
}
  • DetailsAddModel
public class DetailsAddModel implements DetailsAddService {
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void addCart(Map params, final OnNetListener onNetListener) {
        OkHttpUtils.getOkHttpUtils().doPost(Api.str1, params, 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 DetailsAddBean addCartBean = new Gson().fromJson(string, DetailsAddBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(addCartBean);
                    }
                });
            }
        });
    }
  • DetailsService
public interface DetailsService {
    void getProductDetail(Map params, OnNetListener onNetListener);
}
  • DetailsModel
public class DetailsModel implements DetailsService {
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void getProductDetail(Map params, final OnNetListener onNetListener) {
        OkHttpUtils.getOkHttpUtils().doPost(Api.str1, params, 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 DetailsShopBean detailsBean = new Gson().fromJson(string, DetailsShopBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(detailsBean);
                    }
                });
            }
        });
    }
}

bean包

  • DetailsAddBean
public class DetailsAddBean {
    private String msg;
    private String code;

    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;
    }
}
  • DetailsShopBean
public class DetailsShopBean {
    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 {
        private String description;
        private String icon;
        private String name;
        private int productNums;
        private double 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 double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        public int getSellerid() {
            return sellerid;
        }

        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }
    }

    public static class DataBean {
        private double bargainPrice;
        private String createtime;
        private String detailUrl;
        private String images;
        private int itemtype;
        private int pid;
        private double 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 double getPrice() {
            return price;
        }

        public void setPrice(double 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;
        }
    }

你可能感兴趣的:(Android,Studio)