首先是他用到的框架
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.squareup.retrofit2:converter-gson:2.2.0' compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.2.0' compile 'io.reactivex.rxjava2:rxjava:2.0.7' compile 'com.squareup.okhttp3:okhttp:3.7.0' compile 'com.squareup.okhttp3:logging-interceptor:3.6.0' compile 'com.youth.banner:banner:1.4.9'
************************************************
网络请求net包下,作用:封装网络请求
public interface Api { public static final String HOST="http://120.27.23.105"; public static final String XHOST="https://www.zhaoapi.cn"; } **************************
public interface OnNetListner<T> { //成功调用的方法 void onSuccess(T t); //失败调用的方法 void onFailed(Exception e); }****************************************
//我用的是io的,还有ix的,别弄错了 import io.reactivex.Observable; import retrofit2.http.POST; import retrofit2.http.Query; public interface RetrofitServer { @POST("/user/login") Observable<SingnBean> sign(@Query("mobile") String mobile, @Query("password") String pwd); @POST("/user/reg") Observable<RegisBean> regis(@Query("mobile") String mobile, @Query("password") String pwd); //首页广告(轮播图+京东秒杀+最底部的为你推荐) @POST("/ad/getAd") Observable<SgouyeBean> shouye(); @POST("/product/getCatagory") Observable<FlleftBean> flleftbean(); //商品分类 @POST("/product/getCatagory") Observable<CatagoryBean> cara(); //商品子分类 @POST("/product/getProductCatagory") Observable<ProducatagoryBean> producatagory(@Query("cid") String cid); //当前子分类下的商品列表 @POST("/product/getProducts") Observable<ProListBean> lisproduts(@Query("pscid") String pscid, @Query("source") String sourc); //.商品详情 @POST("/product/getProductDetail") Observable<DetailBean> detail(@Query("pid") String pid, @Query("source") String source); //加入购物车 @POST("/product/addCart") Observable<AddBean> adds(@Query("pid") String pid, @Query("uid") String uid, @Query("source") String source); //查询购物车 @POST("/product/getCarts") Observable<CartsBean> carts(@Query("uid") String uid, @Query("source") String source); //搜索购物车 @POST("/product/searchProducts") Observable<SpSouSbean> spss(@Query("keywords") String keywords, @Query("page") String page, @Query("source") String source); } *********************************************
public class Retrofitss { private static OkHttpClient okHttpClient; private static RetrofitServer server; static { getOkHttpClient(); } public static OkHttpClient getOkHttpClient() { if (okHttpClient == null) { synchronized (Retrofitss.class) { if (okHttpClient == null) { okHttpClient = new OkHttpClient(); } } } return okHttpClient; } public static RetrofitServer getServer() { if (server == null) { synchronized (OkHttpClient.class) { if (server == null) { server = onCreate(RetrofitServer.class, Api.XHOST); } } } return server; } public static <T> T onCreate(Class<T> tClass, String url) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(url) .addConverterFactory(GsonConverterFactory.create(new Gson())) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(okHttpClient) .build(); return retrofit.create(tClass); } } //下面的是自己的项目一部分,主要看思路 **************************** 在Model层的简单使用,看一下思路就可以了,下面的就是使用到项目中
public interface IAddModel { void getadd(String uid, String pid, String source, OnNetListner<AddBean> onNetListner); } ********************************
public class AddModel implements IAddModel { @Override public void getadd(String uid, String pid, String source, final OnNetListner<AddBean> onNetListner) { // AddBean类里的数据是用https://www.zhaoapi.cn/product/addCart?pid=1&uid=这个地方写你自己登陆成功反回数据上的uid //source=android公共参数 Observable<AddBean> adds = Retrofitss.getServer().adds(pid, uid, source); adds.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer<AddBean>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(AddBean addBean) { onNetListner.onSuccess(addBean); } @Override public void onError(Throwable e) { onNetListner.onFailed((Exception) e); } @Override public void onComplete() { } }); } } ********************************** p层的使用
public class AddPrensenter { private IDetaileActivity activity; private final IAddModel model; public AddPrensenter(IDetaileActivity activity) { this.activity = activity; model = new AddModel(); } //防止内存泄漏 public void ond(){ if (activity!=null){ activity=null; } } public void geta(String pid,String uid,String source){ model.getadd(uid, pid, source, new OnNetListner<AddBean>() { @Override public void onSuccess(AddBean addBean) { if (activity!=null) { activity.shoaw(addBean); } } @Override public void onFailed(Exception e) { } }); } }****************************************c
v层的使用
public interface IDetaileActivity { //这个类接口方法的逻辑就不写了,太多了 void show(DetailBean detailBean); //我们用的是这个 void shoaw(AddBean addBean); } ************************************************
import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.jingdong.MainActivity; import com.example.jingdong.R; import com.example.jingdong.classss.bean.AddBean; import com.example.jingdong.classss.bean.DetailBean; import com.example.jingdong.classss.presenter.AddPrensenter; import com.example.jingdong.classss.presenter.DetailPresenter; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class DetailActivity extends AppCompatActivity implements IDetaileActivity { //Banner控件生成的,别看起来多,都是自动生成的,很简单,自己搜一下studio的banner控件自动生成 @BindView(R.id.ivBack) ImageView ivBack; @BindView(R.id.rbGoods) RadioButton rbGoods; @BindView(R.id.rbDetails) RadioButton rbDetails; @BindView(R.id.rbAppraise) RadioButton rbAppraise; @BindView(R.id.rg) RadioGroup rg; @BindView(R.id.ivShare) ImageView ivShare; @BindView(R.id.ivMsg) ImageView ivMsg; /* @BindView(R.id.vp) RecyclerView vp;*/ @BindView(R.id.llSupplier) LinearLayout llSupplier; @BindView(R.id.llShop) LinearLayout llShop; @BindView(R.id.llAttention) LinearLayout llAttention; @BindView(R.id.llCard) LinearLayout llCard; @BindView(R.id.tvAddCard) TextView tvAddCard; @BindView(R.id.detail_im) ImageView detailIm; @BindView(R.id.detail_titel) TextView detailTitel; @BindView(R.id.detail_price) TextView detailPrice; private DetailPresenter presenter; private List<DetailBean> detailBeans; private AddPrensenter addPrensenter; private String pid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); ButterKnife.bind(this); //这里时通过key值,也就是自己起的名字找的传过来的pid,我起的名字就叫pid Intent intent = getIntent(); pid = intent.getStringExtra("pid"); //和p层进行交互 presenter = new DetailPresenter(this); presenter.getda(pid, "android"); //这个是我们用到的P层 addPrensenter = new AddPrensenter(this); } //防止内存泄漏,调用P层自己写的方法把v层的引用判空,在生命周期 //结束的时候这个引用判空,就是我们退出这个APP的时候 @Override protected void onDestroy() { super.onDestroy(); presenter.onDestroys(); addPrensenter.ond(); } @Override public void show(DetailBean detailBean) { DetailBean.DataBean data = detailBean.getData(); //取得他的图片和价格和标题,图片框架用的glide String[] split = data.getImages().split("\\|"); Glide.with(this).load(split[0]).into(detailIm); detailTitel.setText(data.getSubhead()); detailPrice.setText("¥" + data.getPrice()); } //提示加入购物车成功 @Override public void shoaw(AddBean addBean) { Toast.makeText(this,addBean.getMsg()+"",Toast.LENGTH_LONG).show(); } //这里我是写的加入购物车和跳转购物车 @OnClick({R.id.llCard, R.id.tvAddCard}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.llCard: startActivity(new Intent(this, MainActivity.class)); break; //加入购物车 case R.id.tvAddCard: //取出uid,这个uid早先登录的时候用sharedpreferences储存进去,现在使用 SharedPreferences sp = getSharedPreferences("UIDS", Context.MODE_PRIVATE); addPrensenter.geta(pid,sp.getString("uid",""),"android" ); break; } } //图标控件,点击以后finish掉这个页面,达到返回上一页的效果 @OnClick(R.id.ivBack) public void onViewClicked() { finish(); } } ********************************* 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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.jingdong.classss.view.DetailActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivBack" android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/leftjiantou" /> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <RadioButton android:id="@+id/rbGoods" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="商品" /> <RadioButton android:id="@+id/rbDetails" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="详情" /> <RadioButton android:id="@+id/rbAppraise" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="评价" /> </RadioGroup> <ImageView android:id="@+id/ivShare" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="50dp" android:layout_marginRight="20dp" android:background="@drawable/share" /> <ImageView android:id="@+id/ivMsg" android:layout_width="30dp" android:layout_height="26dp" android:layout_marginRight="10dp" android:background="@drawable/sandian" /> </LinearLayout> <LinearLayout android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageView android:id="@+id/detail_im" android:layout_width="match_parent" android:layout_height="240dp" /> <TextView android:id="@+id/detail_titel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <TextView android:id="@+id/detail_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#f00" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:background="#ffffff" android:orientation="horizontal"> <LinearLayout android:id="@+id/llSupplier" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="供应商" /> </LinearLayout> <LinearLayout android:id="@+id/llShop" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="店铺" /> </LinearLayout> <LinearLayout android:id="@+id/llAttention" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关注" /> </LinearLayout> <LinearLayout android:id="@+id/llCard" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="购物车" /> </LinearLayout> <TextView android:id="@+id/tvAddCard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#ff3660" android:gravity="center" android:text="加入购物车" android:textColor="#ffffff" /> </LinearLayout> </LinearLayout>