效果图:
依赖:
//retrofit2的依赖 compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' //rxJava2的依赖 compile 'io.reactivex.rxjava2:rxandroid:2.0.1' // Because RxAndroid releases are few and far between, it is recommended you also // explicitly depend on RxJava's latest version for bug fixes and new features. compile 'io.reactivex.rxjava2:rxjava:2.1.7' //拦截器的依赖 compile 'com.squareup.okhttp3:logging-interceptor:3.9.1' //recyclerview的依赖 implementation 'com.android.support:recyclerview-v7:26.1.0' //eventbus的依赖 compile 'org.greenrobot:eventbus:3.0.0' //Fresco的依赖 compile 'com.facebook.fresco:fresco:1.5.0' //ButterKnifr依赖 compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
MyApp:
public class MyApp extends Application { public static SharedPreferences preferences; public static SharedPreferences.Editor edit; @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); preferences = getSharedPreferences("user", MODE_PRIVATE); edit = preferences.edit(); } }----------------商品列表--------------------
model层:
public interface IProductModel { public void getProduct(String pscid, String page, OnNetListener<ProductBean> onNetListener); }
public class ProductModel implements IProductModel { @Override public void getProduct(String pscid, String page, final OnNetListener<ProductBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getApi(); serviceApi.getProduct(pscid, page) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<ProductBean>() { @Override public void accept(ProductBean productBean) throws Exception { onNetListener.onSuccess(productBean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { onNetListener.onFailure(throwable); } }); } }Presenter层:
public class ProductPresenter { private IProductView iProductView; private final IProductModel iProductModel; public ProductPresenter(IProductView iProductView) { this.iProductView = iProductView; iProductModel = new ProductModel(); } public void getProduct() { iProductModel.getProduct("39", "1", new OnNetListener<ProductBean>() { @Override public void onSuccess(ProductBean productBean) { if (productBean.getCode().equals("0")) { iProductView.showProduct(productBean); } } @Override public void onFailure(Throwable throwable) { } }); } }View层:
public interface IProductView { public void showProduct(ProductBean productBean); }activity:
public class BaseActivity extends AppCompatActivity { Unbinder unbinder; @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); unbinder = ButterKnife.bind(this); } @Override protected void onDestroy() { super.onDestroy(); if (unbinder != null) { unbinder.unbind(); } } }
public class MainActivity extends BaseActivity implements IProductView { @BindView(R.id.products_rlv) RecyclerView productsRlv; private ProductPresenter productPresenter; private ProductElvAdapter productElvAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); productsRlv.setLayoutManager(new LinearLayoutManager(this)); productPresenter = new ProductPresenter(this); productPresenter.getProduct(); } @Override public void showProduct(ProductBean productBean) { final List<ProductBean.DataBean> data = productBean.getData(); productElvAdapter = new ProductElvAdapter(this, data); productsRlv.setAdapter(productElvAdapter); productElvAdapter.setOnClick(new ProductElvAdapter.OnClick() { @Override public void Onclick(String pid) { Intent intent = new Intent(MainActivity.this, ProductDetailActivity.class); intent.putExtra("pid", pid); startActivity(intent); } }); } }adapter:
public class ProductElvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ProductBean.DataBean> dataList; private LayoutInflater inflater; private OnClick onClick; private int pid; public interface OnClick { void Onclick(String pid); } public void setOnClick(OnClick onClick) { this.onClick = onClick; } public ProductElvAdapter(Context context, List<ProductBean.DataBean> dataList) { this.context = context; this.dataList = dataList; inflater = LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.product_rlv_item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder myViewHolder = (MyViewHolder) holder; final ProductBean.DataBean dataBean = dataList.get(position); myViewHolder.tvProductPrice.setText("¥" + dataBean.getPrice()); myViewHolder.tvProductSubhead.setText(dataBean.getSubhead()); myViewHolder.tvProductTitle.setText(dataBean.getTitle()); String[] images = dataBean.getImages().split("\\!"); myViewHolder.productSdv.setImageURI(images[0]); pid = dataBean.getPid(); myViewHolder.pLineat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClick.Onclick(pid+""); } }); } @Override public int getItemCount() { return dataList.size(); } class MyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.tv_product_price) TextView tvProductPrice; @BindView(R.id.tv_product_subhead) TextView tvProductSubhead; @BindView(R.id.tv_product_title) TextView tvProductTitle; @BindView(R.id.products_sdv) SimpleDraweeView productSdv; @BindView(R.id.product_lineat) RelativeLayout pLineat; public MyViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }XML布局:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.disanzhouzhoukaomoni.view.activity.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/products_rlv" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>适配器布局:product_rlv_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/product_lineat" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="250dp" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/products_sdv" android:layout_width="130dp" android:layout_height="150dp" android:layout_gravity="center" app:placeholderImage="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv_product_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:padding="5dp" android:text="¥15151.0" /> <TextView android:id="@+id/tv_product_subhead" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:padding="5dp" android:text="高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!" /> <TextView android:id="@+id/tv_product_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:padding="5dp" android:text="一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray" /> </RelativeLayout>
Bean类
public class ProductBean { private String msg; private String code; private String page; private List<DataBean> data; 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; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { private int 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 int getBargainPrice() { return bargainPrice; } public void setBargainPrice(int 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层:
public interface IProductDetailModel { public void ProductDetail(String pid, OnNetListener<ProductDetailBean> onNetListener); public void addCart(String uid, String pid, OnNetListener<AddCartBean> onNetListener); }
public class ProductDetailModel implements IProductDetailModel { @Override public void ProductDetail(String pid, final OnNetListener<ProductDetailBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getApi(); serviceApi.getProductDetail(pid) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<ProductDetailBean>() { @Override public void accept(ProductDetailBean productDetailBean) throws Exception { onNetListener.onSuccess(productDetailBean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { onNetListener.onFailure(throwable); } }); } @Override public void addCart(String uid, String pid, final OnNetListener<AddCartBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getApi(); serviceApi.addCart(uid, pid) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<AddCartBean>() { @Override public void accept(AddCartBean addCartBean) throws Exception { onNetListener.onSuccess(addCartBean); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { onNetListener.onFailure(throwable); } }); } }
Presenter层:
public class ProductDetailPresenter { private IDetailView iDetailView; private final IProductDetailModel iProductDetailModel; public ProductDetailPresenter(IDetailView iDetailView) { this.iDetailView = iDetailView; iProductDetailModel = new ProductDetailModel(); } public void getDetail(String pid) { iProductDetailModel.ProductDetail(pid, new OnNetListener<ProductDetailBean>() { @Override public void onSuccess(ProductDetailBean productDetailBean) { iDetailView.getDetail(productDetailBean); } @Override public void onFailure(Throwable throwable) { } }); } public void addCart(String uid, String pid) { iProductDetailModel.addCart(uid, pid, new OnNetListener<AddCartBean>() { @Override public void onSuccess(AddCartBean addCartBean) { iDetailView.addCart(addCartBean); } @Override public void onFailure(Throwable throwable) { } }); } }
View层:
public interface IDetailView { public void getDetail(ProductDetailBean productDetailBean); public void addCart(AddCartBean addCartBean); }
activity:
public class ProductDetailActivity extends AppCompatActivity implements IDetailView { @BindView(R.id.xq_rlv) RecyclerView xqRlv; @BindView(R.id.iv_getcart) ImageView ivGetcart; @BindView(R.id.bt_addcart) Button btAddcart; private ProductDetailPresenter detailPresenter; private String pid; private int uid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_detail); ButterKnife.bind(this); Intent intent = getIntent(); pid = intent.getStringExtra("pid"); detailPresenter = new ProductDetailPresenter(this); detailPresenter.getDetail(pid); LinearLayoutManager layoutManager = new LinearLayoutManager(this); xqRlv.setLayoutManager(layoutManager); uid = MyApp.preferences.getInt("uid", 0); } @OnClick({R.id.iv_getcart, R.id.bt_addcart}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.iv_getcart: Intent intent = new Intent(ProductDetailActivity.this, CartActivity.class); startActivity(intent); break; case R.id.bt_addcart: detailPresenter.addCart(uid + "", pid); break; } } @Override public void getDetail(ProductDetailBean productDetailBean) { ProductDetailBean.DataBean dataBean = productDetailBean.getData(); List<ProductDetailBean.DataBean> dataBeanList = new ArrayList<>(); dataBeanList.add(dataBean); DetailRlvAdapter rlvAdapter = new DetailRlvAdapter(this, dataBeanList); xqRlv.setAdapter(rlvAdapter); } @Override public void addCart(AddCartBean addCartBean) { if ("0".equals(addCartBean.getCode())) { Toast.makeText(getApplicationContext(), addCartBean.getMsg(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), addCartBean.getMsg(), Toast.LENGTH_SHORT).show(); } } }
adapter:
public class DetailRlvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ProductDetailBean.DataBean> dataList; private LayoutInflater inflater; public DetailRlvAdapter(Context context, List<ProductDetailBean.DataBean> dataList) { this.context = context; this.dataList = dataList; inflater = LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.detail_rlv_adapter_item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder myViewHolder = (MyViewHolder) holder; ProductDetailBean.DataBean dataBean = dataList.get(position); String[] images = dataBean.getImages().split("\\!"); myViewHolder.mDetailAdapterSdv.setImageURI(images[0]); myViewHolder.mTvDetailTitle.setText(dataBean.getTitle()); myViewHolder.mTvDetailPrice.setText("¥" + dataBean.getPrice()); myViewHolder.mTvDetailSj.setText("我是商家" + dataBean.getSellerid()); } @Override public int getItemCount() { if (dataList == null) { return 0; } return dataList.size(); } class MyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.detail_adapter_sdv) SimpleDraweeView mDetailAdapterSdv; @BindView(R.id.tv_detail_title) TextView mTvDetailTitle; @BindView(R.id.tv_detail_price) TextView mTvDetailPrice; @BindView(R.id.tv_detail_sj) TextView mTvDetailSj; public MyViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } }XML布局:
activity布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.disanzhouzhoukaomoni.view.activity.ProductDetailActivity"> <ImageView android:id="@+id/iv_top" android:layout_width="match_parent" android:layout_height="250dp" android:background="@drawable/b_tou" /> <android.support.v7.widget.RecyclerView android:id="@+id/xq_rlv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/iv_top" /> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <ImageView android:layout_width="65dp" android:layout_height="match_parent" android:background="@drawable/b_kefu" /> <ImageView android:layout_width="65dp" android:layout_height="match_parent" android:background="@drawable/b_shoucang" /> <ImageView android:id="@+id/iv_getcart" android:layout_width="65dp" android:layout_height="match_parent" android:background="@drawable/gouwuche" /> <Button android:id="@+id/bt_addcart" android:layout_width="150dp" android:layout_height="match_parent" android:background="#FDC302" android:text="添加到购物车" android:textColor="#fff" android:textSize="16sp" /> <Button android:layout_width="120dp" android:layout_height="match_parent" android:background="#FF6A04" android:text="立即购买" android:textColor="#fff" android:textSize="16sp" /> </LinearLayout> </RelativeLayout>适配器布局:
detail_rlv_adapter_item.xml
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/detail_adapter_sdv" android:layout_width="match_parent" android:layout_height="300dp" app:placeholderImage="@mipmap/ic_launcher" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_detail_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="费卡机发电电费卡机发电电费卡机发电" /> <TextView android:id="@+id/tv_detail_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="¥1321.0" android:textColor="#E2791E" /> <TextView android:id="@+id/tv_detail_sj" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="我是商家11" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="70dp" android:layout_alignParentRight="true" android:src="@drawable/b_fenxiang" /> </RelativeLayout> </LinearLayout>Bean类:
public class ProductDetailBean { 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 int 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 int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } } public static class DataBean { private int 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 int getBargainPrice() { return bargainPrice; } public void setBargainPrice(int 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; } } }