1. 程式人生 > >詳情頁面【仿】淘寶App

詳情頁面【仿】淘寶App

activity_detai.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height
="0dp" android:layout_weight="1" android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="500dp" /> <TextView android:id
="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textSize="20dp" />
<TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height
="wrap_content" android:text="TextView" android:textColor="#f4053d" android:textSize="20dp" />
</LinearLayout> <Button android:id="@+id/goshop" android:layout_width="match_parent" android:layout_height="50dp" android:layout_margin="5dp" android:background="#fc7802" android:text="加入購物車" android:textColor="#fff" /> </LinearLayout>

bean資料夾:資料解析

AddBean.java

public class AddBean {

    /**
     * msg : 加購成功
     * code : 0
     */

    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;
    }

DetaiBean.java

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;
        }

model資料夾:資料層

DetaiModel.java

public class DetaiModel implements IModel {
    private DetaiPresenter presenter;

    public DetaiModel(DetaiPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void get(String baseurl, Map<String, String> map) {
        Flowable<DetaiBean> flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).detaiget(map);
        presenter.getData(flowable);
    }

    public void get2(String baseurl, Map<String, String> map) {
        Flowable<AddBean> flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).addget(map);
        presenter.getData2(flowable);
    }
}

view資料夾:檢視層

DetaiActivity.java

public class DetaiActivity extends AppCompatActivity implements IView {

    @BindView(R.id.image)
    SimpleDraweeView image;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.goshop)
    Button goshop;
    private DetaiPresenter detaiPresenter;
    private String pid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detai);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        pid = intent.getStringExtra("pid");
        //https://www.zhaoapi.cn/product/getProductDetail?source=android&pid=1
        detaiPresenter = new DetaiPresenter(this,this);
        Map<String,String> map = new HashMap<>();
        map.put("source","android");
        map.put("pid", pid);
        detaiPresenter.get("https://www.zhaoapi.cn/",map);
    }

    @OnClick(R.id.goshop)
    public void onViewClicked() {
        //http://120.27.23.105/product/addCart?source=android&uid=1653&pid=1
        Map<String,String> map = new HashMap<>();
        map.put("source","android");
        map.put("uid","1653");
        map.put("pid",pid);
        detaiPresenter.get2("http://120.27.23.105/",map);
    }

    @Override
    public void onSuccess(Object o) {
        DetaiBean d = (DetaiBean) o;
        DetaiBean.DataBean data = d.getData();
        if(data!=null){
            String[] split = data.getImages().split("!");
            image.setImageURI(Uri.parse(split[0]));
            tvTitle.setText(data.getTitle());
            tvPrice.setText(data.getPrice()+"");
        }
    }

    @Override
    public void onFailed(Exception e) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(detaiPresenter!=null){
            detaiPresenter.detatch();
        }
    }
}

persenter資料夾:邏輯層

public class DetaiPresenter implements BasePresenter {
    private IView iv;
    private Context context;
    private DisposableSubscriber<DetaiBean> disposableSubscriber;
    private DisposableSubscriber<AddBean> disposableSubscriber1;

    public DetaiPresenter(IView iv, Context context) {
        this.iv = iv;
        this.context = context;
    }

    @Override
    public void get(String baseurl, Map<String, String> map) {
        DetaiModel model = new DetaiModel(this);
        model.get(baseurl, map);
    }

    @Override
    public void get2(String baseurl, Map<String, String> map) {
        DetaiModel model = new DetaiModel(this);
        model.get2(baseurl, map);
    }

    public void getData(Flowable<DetaiBean> flowable) {
        disposableSubscriber = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<DetaiBean>() {
                    @Override
                    public void onNext(DetaiBean detaiBean) {
                        iv.onSuccess(detaiBean);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getData2(Flowable<AddBean> flowable) {
        disposableSubscriber1 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<AddBean>() {
                    @Override
                    public void onNext(AddBean addBean) {
                        Toast.makeText(context, addBean.getMsg(), Toast.LENGTH_SHORT).show();
                        if (addBean.getCode().equals("0")) {
//                            context.startActivity(new Intent(context, ShoppingActivity.class));
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //防止記憶體洩漏
    public void detatch() {
        if (iv != null) {
            iv = null;
        }
        if (disposableSubscriber != null) {
            if (!disposableSubscriber.isDisposed()) {
                disposableSubscriber.dispose();
            }
        }
        if (disposableSubscriber1 != null) {
            if (!disposableSubscriber1.isDisposed()) {
                disposableSubscriber1.dispose();
            }
        }
    }