一,依赖
//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'
二,权限
<uses-permission android:name="android.permission.INTERNET" />
三,mian 布局
<?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="ywf.bwei.com.lianxi4.Main2Activity"> <ImageView android:id="@+id/iv_top" android:layout_width="match_parent" android:layout_height="250dp" /> <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" /> <ImageView android:layout_width="65dp" android:layout_height="match_parent" /> <ImageView android:id="@+id/iv_getcart" android:layout_width="65dp" android:layout_height="match_parent" /> <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 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" /> </RelativeLayout> </LinearLayout>
bean 层 ProductDetailBean
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;
}
}
}
三,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 层 IProductDetailModel
public interface IProductDetailModel { public void ProductDetail(String pid, OnNetListener<ProductDetailBean> onNetListener); }
model 层 ProductDetailModel
public class ProductDetailModel implements IProductDetailModel { @Override public void ProductDetail(String pid, final OnNetListener<ProductDetailBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getServiceApi(); 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); } }); } }persenter 层 ProductDetailPresenter
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) { } }); } }
view 层 IDetailView
public interface IDetailView { public void getDetail(ProductDetailBean productDetailBean); }
adapter 层 DetailRlvAdapter
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); } } }
utils 层 OnNetListener
public interface OnNetListener<T> { void onSuccess(T t); void onFailure(Throwable throwable); }
utils 层 RetrofitHelper
public class RetrofitHelper { private static Retrofit retrofit; private static ServiceApi serviceApi; /** * 初始化Retrofit 单例模式 */ private static Retrofit getRetrofit() { //线程安全 if (retrofit == null) { synchronized (RetrofitHelper.class) { if (retrofit == null) { //设置拦截器 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); //初始化OkHttpClient OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(logging)//添加拦截器 .addInterceptor(new MyInterceptor()) .connectTimeout(50, TimeUnit.SECONDS)//设置连接超时 .build(); //初始化Retrofit retrofit = new Retrofit.Builder() .baseUrl(UrlsApi.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } } } return retrofit; } public static ServiceApi getServiceApi() { if (serviceApi == null) { synchronized (RetrofitHelper.class) { if (serviceApi == null) { serviceApi = getRetrofit().create(ServiceApi.class); } } } return serviceApi; } /** * 添加公共参数拦截器 */ static class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); HttpUrl httpUrl = request .url() .newBuilder() .addQueryParameter("source", "android") .build(); Request requestNew = request .newBuilder() .url(httpUrl) .build(); return chain.proceed(requestNew); } } }
utils 层 ServiceApi
public interface ServiceApi { @GET(UrlsApi.PRODUCTS) Observable<ProductBean> getProduct(@Query("pscid") String pscid, @Query("page") String page); @GET(UrlsApi.xiangqing) Observable<ProductDetailBean> getProductDetail(@Query("pid") String pid); }utils 层 UrlsApi
public class UrlsApi { public static final String BASE_URL = "https://www.zhaoapi.cn/"; public static final String PRODUCTS = "product/getProducts";//商品列表 public static final String xiangqing = "product/getProductDetail";//商品详情 }mian函数public class Main2Activity 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_main2); 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: break; case R.id.bt_addcart: Intent intent = new Intent(Main2Activity.this, Main22Activity.class); startActivity(intent); 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); } }
在清单文件中 name