一
package com.example.zhoukaomoni.api; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.bean.ProductBean; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; /** * Created by 。。。。 on 2018/11/3. */ public interface ApiService { @GET("home/getHome") Call<GoodsBean> getgoods(); @GET("product/getProductDetail") Call<ProductBean> getproduct(@Query("pid") int pid); }
二封装bean类
三
package com.example.zhoukaomoni.adapter; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.zhoukaomoni.R; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.view.ShowActivity; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; /** * Created by 。。。。 on 2018/11/3. */ public class GoodsAdaapter extends RecyclerView.Adapter<GoodsAdaapter.ViewHolder> { private Context context; private List<GoodsBean.DataBean.FenleiBean> list; public GoodsAdaapter(Context context, List<GoodsBean.DataBean.FenleiBean> list) { this.context = context; this.list = list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v=View.inflate(context, R.layout.item_goods,null); ViewHolder holder= new ViewHolder(v); return holder; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { holder.simpleDraweeView.setImageURI(list.get(position).getIcon()); holder.txtname.setText(list.get(position).getName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(context, ShowActivity.class); intent.putExtra("id",list.get(position).getCid()); context.startActivity(intent); } }); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder { private SimpleDraweeView simpleDraweeView; private TextView txtname; public ViewHolder(View itemView) { super(itemView); simpleDraweeView = itemView.findViewById(R.id.simple); txtname = itemView.findViewById(R.id.txt_title); } } }
四
package com.example.zhoukaomoni.httputils; import java.net.URL; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by 。。。。 on 2018/11/3. */ public class HttpUtils { private static final String Url="http://www.zhaoapi.cn/"; private Retrofit retrofit; private static final class contast{ private static HttpUtils instance = new HttpUtils(); } public static HttpUtils getinstance(){ return contast.instance; } private HttpUtils(){ retrofit = new Retrofit.Builder() .baseUrl(Url) .addConverterFactory(GsonConverterFactory.create()) .client(buildokHttpClient()) .build(); } private OkHttpClient buildokHttpClient(){ return new OkHttpClient.Builder() .readTimeout(5000, TimeUnit.MILLISECONDS) .writeTimeout(5000,TimeUnit.MILLISECONDS) .build(); } public Retrofit getretrofit(){ return retrofit; } public <T> T create(Class<T> aClass){ return retrofit.create(aClass); } }
五
package com.example.zhoukaomoni.callback; /** * Created by 。。。。 on 2018/11/3. */ public interface ICallback { void OnSuccess(Object o); void OnFailed(Exception e); }
六
package com.example.zhoukaomoni.model; import com.example.zhoukaomoni.api.ApiService; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.bean.ProductBean; import com.example.zhoukaomoni.callback.ICallback; import com.example.zhoukaomoni.httputils.HttpUtils; import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; /** * Created by 。。。。 on 2018/11/3. */ public class GoodsModel { int id; public void Goods(final ICallback Callback){ ApiService apiService= HttpUtils.getinstance().create(ApiService.class); Call<GoodsBean> getgoods = apiService.getgoods(); getgoods.enqueue(new Callback<GoodsBean>() { @Override public void onResponse(Call<GoodsBean> call, Response<GoodsBean> response) { if(response.isSuccessful()){ GoodsBean body = response.body(); Callback.OnSuccess(body); } } @Override public void onFailure(Call<GoodsBean> call, Throwable t) { Callback.OnFailed((Exception) t); } }); } public void Product(final ICallback callback,int id){ ApiService apiService=HttpUtils.getinstance().create(ApiService.class); Call<ProductBean> getproduct=apiService.getproduct(id); getproduct.enqueue(new Callback<ProductBean>() { @Override public void onResponse(Call<ProductBean> call, Response<ProductBean> response) { if(response.isSuccessful()){ ProductBean body = response.body(); callback.OnSuccess(body); } } @Override public void onFailure(Call<ProductBean> call, Throwable t) { callback.OnFailed((Exception) t); } }); } }
七
数据加载View
package com.example.zhoukaomoni.view; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.bean.ProductBean; import java.util.List; /** * Created by 。。。。 on 2018/11/3. */ public interface IView { void onsuccess(List<GoodsBean.DataBean.FenleiBean> list); void onfailed(Exception e); }
商品展示View
package com.example.zhoukaomoni.view; import com.example.zhoukaomoni.bean.ProductBean; import java.util.List; /** * Created by 。。。。 on 2018/11/3. */ public interface PView { void onfailed(Exception e); void onsuccess(ProductBean.DataBean data); }
八
package com.example.zhoukaomoni.presenter; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.bean.ProductBean; import com.example.zhoukaomoni.callback.ICallback; import com.example.zhoukaomoni.model.GoodsModel; import com.example.zhoukaomoni.view.IView; import com.example.zhoukaomoni.view.PView; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; /** * Created by 。。。。 on 2018/11/3. */ public class GoodsPresenter { private IView iv; private GoodsModel model; private PView piv; private GoodsModel pModel; public void pattach(PView piv){ this.piv=piv; pModel = new GoodsModel(); } public void attach(IView iv){ this.iv=iv; model = new GoodsModel(); } public void getgoods(){ Type type=new TypeToken<GoodsBean>(){}.getType(); model.Goods(new ICallback() { @Override public void OnSuccess(Object o) { GoodsBean goodsBean= (GoodsBean) o; iv.onsuccess(goodsBean.getData().getFenlei()); } @Override public void OnFailed(Exception e) { iv.onfailed(e); } }); } public void getproduct(int id){ pModel.Product(new ICallback() { @Override public void OnSuccess(Object o) { ProductBean productBean= (ProductBean) o; piv.onsuccess(productBean.getData()); } @Override public void OnFailed(Exception e) { piv.onfailed(e); } },id); } }
九
package com.example.zhoukaomoni.view; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import com.example.zhoukaomoni.R; import com.example.zhoukaomoni.adapter.GoodsAdaapter; import com.example.zhoukaomoni.bean.GoodsBean; import com.example.zhoukaomoni.presenter.GoodsPresenter; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements IView { @BindView(R.id.recycler_view) RecyclerView recyclerView; private GoodsAdaapter adaapter; private GoodsPresenter goodsPresenter; private List<GoodsBean.DataBean.FenleiBean> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); RecyclerView.LayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); lists = new ArrayList<>(); goodsPresenter = new GoodsPresenter(); goodsPresenter.attach(this); goodsPresenter.getgoods(); adaapter = new GoodsAdaapter(this, lists); recyclerView.setAdapter(adaapter); } @Override public void onsuccess(List<GoodsBean.DataBean.FenleiBean> list) { if(list!=null){ lists.clear(); lists.addAll(list); adaapter.notifyDataSetChanged(); } } @Override public void onfailed(Exception e) { } }
十
package com.example.zhoukaomoni.view; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.TextView; import com.example.zhoukaomoni.R; import com.example.zhoukaomoni.bean.ProductBean; import com.example.zhoukaomoni.presenter.GoodsPresenter; import com.facebook.drawee.view.SimpleDraweeView; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; public class ShowActivity extends AppCompatActivity implements PView { @BindView(R.id.drawee) SimpleDraweeView drawee; @BindView(R.id.txt_title) TextView txtTitle; private List<ProductBean.DataBean> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); ButterKnife.bind(this); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); lists = new ArrayList<>(); int id = getIntent().getIntExtra("id", 21); GoodsPresenter goodsPresenter = new GoodsPresenter(); goodsPresenter.pattach(this); goodsPresenter.getproduct(id); } @Override public void onfailed(Exception e) { } @Override public void onsuccess(ProductBean.DataBean data) { drawee.setImageURI(data.getImages().split("\\|")[0]); txtTitle.setText(data.getTitle()); } }