MainActivity
public class MainActivity extends AppCompatActivity implements IContract.IView { @BindView(R.id.reView) RecyclerView reView; @BindView(R.id.reView2) RecyclerView reView2; @BindView(R.id.ctb_title_bar) CustromBar cb; private Context context; private PresenterImpl presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); context = this; //创建P层对象 presenter = new PresenterImpl(); //触发动作 presenter.attachView(this); presenter.reponseData(); cb.setOnCallBackLisenter(new CustromBar.OnCallBackLisenter() { @Override public void OnCallBack() { MainActivity.this.finish(); } }); cb.setOnMenuCallBackLisenter(new CustromBar.OnMenuCallBackLisenter() { @Override public void onMenuCallBack() { ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(cb, "alpha", 0.1f, 0.8f); objectAnimator.setDuration(2000); objectAnimator.start(); } }); } @Override public void showdata(Shopping reponseData) { //数据源 final ArrayList<Shopping.DataBean> dataBeanList = (ArrayList<Shopping.DataBean>) reponseData.getData(); Toast.makeText(context, "dataBeanList", Toast.LENGTH_SHORT).show(); //布局管理器 LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); reView.setLayoutManager(manager); //manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); //reView.setLayoutManager(this.manager); //创建适配器 MyAdapter adapter = new MyAdapter(R.layout.list_item, dataBeanList); adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { int cid = dataBeanList.get(position).getCid(); presenter.reponseGoodsData(cid); } }); reView.setAdapter(adapter); } @Override public void goodsData(Goods reponseGoodsData) { ArrayList<Goods.DataBean> beanList = (ArrayList<Goods.DataBean>) reponseGoodsData.getData(); LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); reView2.setLayoutManager(manager); ItemAdapter itemAdapter = new ItemAdapter(R.layout.review2_item, beanList); reView2.setAdapter(itemAdapter); } @Override protected void onDestroy() { super.onDestroy(); presenter.detachView(this); } } GoodsAdapter
public class GoodsAdapter extends BaseQuickAdapter<Goods.DataBean.ListBean,BaseViewHolder> { public GoodsAdapter(int layoutResId, @Nullable List<Goods.DataBean.ListBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, Goods.DataBean.ListBean item) { helper.setText(R.id.tv_goods_title, item.getName()); //Glide加载显示图片 ImageView iv_goods_icon = helper.getView(R.id.iv_goods_icon); Glide.with(mContext).load(item.getIcon()).into(iv_goods_icon); } }
ItemAdapter
public class ItemAdapter extends BaseQuickAdapter<Goods.DataBean,BaseViewHolder> { public ItemAdapter(int layoutResId, @Nullable List<Goods.DataBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, Goods.DataBean item) { helper.setText(R.id.title, item.getName()); RecyclerView rv_goods = helper.getView(R.id.reView_goods); //商品条目 List<Goods.DataBean.ListBean> beanList = item.getList(); //布局管理器 GridLayoutManager manager = new GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false); rv_goods.setLayoutManager(manager); //设置适配器 GoodsAdapter adapter = new GoodsAdapter(R.layout.review_goods_item, beanList); rv_goods.setAdapter(adapter); } }
MyAdapter
public class MyAdapter extends BaseQuickAdapter<Shopping.DataBean,BaseViewHolder> { public MyAdapter(int layoutResId, @Nullable List<Shopping.DataBean> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, Shopping.DataBean item) { helper.setText(R.id.text,item.getName()); } }
url接口类
public class Constant { //获取分类列表 public static final String SHOPPING_URL = "http://www.zhaoapi.cn/product/getCatagory"; //详情分类列表 public static final String GOODS_URL = "http://www.zhaoapi.cn/product/getProductCatagory"; }
契约类
public interface IContract { //V层 public interface IView{ //展示分类信息 public void showdata(Shopping responeData); //展示详情数据 public void goodsData(Goods reponseGoodsData); } //P层 public interface IPresenter<IView>{ //绑定 public void attachView(IContract.IView iView); //解绑 public void detachView(IContract.IView iView); //分类数据 public void reponseData(); //详细数据 public void reponseGoodsData(int cid); } //M层 public interface IModel{ public void containData(CallBack callBack); //请求详情数据 public void containGooodsData(int cid,CallBack CallBack); public interface CallBack { public void onCallBack(Goods GoodsBean); public void CallBack(Shopping ShoppingBean); } } }
P层
public class PresenterImpl implements IContract.IPresenter{ IContract.IView iView; private SoftReference<IContract.IView> softReference; private IModelImpl model; @Override public void attachView(IContract.IView iView) { this.iView = iView; softReference = new SoftReference<>(iView); model = new IModelImpl(); } @Override public void detachView(IContract.IView iView) { softReference.clear(); } @Override public void reponseData() { model.containData(new IContract.IModel.CallBack() { @Override public void onCallBack(Goods GoodsBean) { iView.goodsData(GoodsBean); } @Override public void CallBack(Shopping ShoppingBean) { iView.showdata(ShoppingBean); } }); } @Override public void reponseGoodsData(int cid) { model.containGooodsData(cid,new IContract.IModel.CallBack() { @Override public void onCallBack(Goods GoodsBean) { iView.goodsData(GoodsBean); } @Override public void CallBack(Shopping ShoppingBean) { iView.showdata(ShoppingBean); } }); } }
M层
public class IModelImpl implements IContract.IModel { @Override public void containData(final CallBack callBack) { OkGo.<String>get(Constant.SHOPPING_URL).execute(new StringCallback() { @Override public void onSuccess(Response<String> response) { //响应数据 String repsoneData = response.body().toString(); //Gson数据解析 Gson gson = new Gson(); Shopping shoppingBean = gson.fromJson(repsoneData, Shopping.class); //接口回调进行数据回传 callBack.CallBack(shoppingBean); } }); } public void containGooodsData(int cid,final CallBack callBack) { OkGo.<String>get(Constant.GOODS_URL).execute(new StringCallback() { @Override public void onSuccess(Response<String> response) { //响应数据 String repsoneGoodsData = response.body().toString(); //Gson数据解析 Gson gson = new Gson(); Goods GoodsBean = gson.fromJson(repsoneGoodsData, Goods.class); //接口回调进行数据回传 callBack.onCallBack(GoodsBean); } }); } }