《依赖》
implementation ‘com.jakewharton:butterknife:9.0.0-rc1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:9.0.0-rc1’
implementation ‘com.squareup.retrofit2:adapter-rxjava2:2.4.0’
implementation ‘com.squareup.retrofit2:converter-gson:2.4.0’
implementation ‘io.reactivex.rxjava2:rxandroid:2.1.0’
implementation ‘com.facebook.fresco:fresco:1.11.0’
implementation ‘com.github.xiaohaibin:XBanner:1.6.6’
implementation project(’:xrecyclerview’)
《Fragment购物车首页展示代码》
public class Frag_01 extends Fragment implements Views {
private XRecyclerView xrecycler;
private PresenterIml presenterIml;
private ShouYeAdapter adapter;
private ArrayList baseBeans = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_01, container, false);
xrecycler = (XRecyclerView) view.findViewById(R.id.xrecycler);
presenterIml = new PresenterIml(new ModelIml(), this);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
adapter = new ShouYeAdapter(getActivity());
xrecycler.setLayoutManager(linearLayoutManager);
xrecycler.setAdapter(adapter);
presenterIml.doget(0, "/small/commodity/v1/bannerShow", null);
}
//调用presenter层的内存泄漏方法
@Override
public void onDestroy() {
super.onDestroy();
presenterIml.destory();
}
@Override
public void success(int type, String data) {
if (type == 0) {
Gson gson = new Gson();
BannerBean bannerBean = gson.fromJson(data, BannerBean.class);
baseBeans.add(bannerBean);
presenterIml.doget(1, "/small/commodity/v1/commodityList", null);
} else {
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(data, ShopBean.class);
ShopBean.ResultBean.MlssBean mlss = shopBean.getResult().getMlss();
ShopBean.ResultBean.PzshBean pzsh = shopBean.getResult().getPzsh();
ShopBean.ResultBean.RxxpBean rxxp = shopBean.getResult().getRxxp();
baseBeans.add(rxxp);
baseBeans.add(mlss);
baseBeans.add(pzsh);
adapter.setList(baseBeans);
}
}
@Override
public void fail(int type, String error) {
}
}
《购物车首页代码bean类需要另写一个接口BaseBean让其bean类中继承BaseBean 》
public interface BaseBean {
}
《使用fresco显示图片需要》
新建一个类继承application Fresco.initialize(this)配置调用 然后在清单文件中name一下新建的类名
以下是配置Fresco与自定义缓存路径代码
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this, ImagePipelineConfig.newBuilder(App.this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()))
.setMaxCacheSize(10 * 1024 * 1024)
.build()
).build());
}
}
《Xbanner使用fresco显示还需要设置一个布局执行强转使用》
强转使用代码在以下首页适配器中
android:layout_width=“match_parent”
android:layout_height=“200dp” />
《ShouYeAdapter 主页面适配器》
public class ShouYeAdapter extends RecyclerView.Adapter {
private final Context context;
private ArrayList list = new ArrayList<>();
public ShouYeAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder viewHolder = null;
switch (i) {
case 0:
//xbanner布局
View view = View.inflate(context, R.layout.adapter_itme0, null);
viewHolder = new ViewHolder_01(view);
break;
case 1:
case 2:
case 3:
//title布局
View view1 = View.inflate(context, R.layout.adapter_itme, null);
viewHolder = new ShopViewHolder(view1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
BaseBean baseBean = list.get(i);
if (baseBean instanceof BannerBean) {
ViewHolder_01 mviewHolder_01 = (ViewHolder_01) viewHolder;
BannerBean mbannerBean = (BannerBean) baseBean;
mviewHolder_01.xbanner.setBannerData(R.layout.image_itme, mbannerBean.getResult());
mviewHolder_01.xbanner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
BannerBean.ResultBean bean = (BannerBean.ResultBean) model;
((SimpleDraweeView) view).setImageURI(bean.getImageUrl());
}
});
} else if (baseBean instanceof ShopBean.ResultBean.RxxpBean) {
ShopViewHolder shopViewHolder_01 = (ShopViewHolder) viewHolder;
ShopBean.ResultBean.RxxpBean rxxpBean = (ShopBean.ResultBean.RxxpBean) baseBean;
shopViewHolder_01.title.setText("热销新品");
} else if (baseBean instanceof ShopBean.ResultBean.MlssBean) {
ShopViewHolder shopViewHolder_02 = (ShopViewHolder) viewHolder;
ShopBean.ResultBean.MlssBean mlssBean = (ShopBean.ResultBean.MlssBean) baseBean;
shopViewHolder_02.title.setText("魔力时尚");
MlssAdapter mlssAdapter = new MlssAdapter(context, mlssBean.getCommodityList());
LinearLayoutManager mlssManager = new LinearLayoutManager(context);
mlssManager.setOrientation(LinearLayoutManager.VERTICAL);
shopViewHolder_02.recycler.setLayoutManager(mlssManager);
shopViewHolder_02.recycler.setAdapter(mlssAdapter);
mlssAdapter.notifyDataSetChanged();
} else if (baseBean instanceof ShopBean.ResultBean.PzshBean) {
ShopViewHolder shopViewHolder_03 = (ShopViewHolder) viewHolder;
ShopBean.ResultBean.PzshBean pzshBean = (ShopBean.ResultBean.PzshBean) baseBean;
shopViewHolder_03.title.setText("品质生活");
}
}
@Override
public int getItemViewType(int position) {
BaseBean baseBean = list.get(position);
if (baseBean instanceof BannerBean) {
return 0;
} else if (baseBean instanceof ShopBean.ResultBean.RxxpBean) {
return 1;
} else if (baseBean instanceof ShopBean.ResultBean.MlssBean) {
return 2;
} else {
return 3;
}
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(ArrayList list) {
this.list = list;
notifyDataSetChanged();
}
//Banner
private class ViewHolder_01 extends RecyclerView.ViewHolder {
XBanner xbanner;
public ViewHolder_01(@NonNull View itemView) {
super(itemView);
xbanner = (XBanner) itemView.findViewById(R.id.xbanner);
}
}
//热销新品 魔力时尚 品质生活
private class ShopViewHolder extends RecyclerView.ViewHolder {
TextView title;
RecyclerView recycler;
public ShopViewHolder(@NonNull View itemView) {
super(itemView);
recycler = (RecyclerView) itemView.findViewById(R.id.recycler);
title = (TextView) itemView.findViewById(R.id.text_title);
}
}
}
《魔丽时尚数据适配器》
public class MlssAdapter extends RecyclerView.Adapter
private final Context context;
private List xxlist = new ArrayList<>();
public MlssAdapter(Context context, List xxList) {
this.context = context;
this.xxlist = xxList;
}
@NonNull
@Override
public MlssAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.mlss_itme, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MlssAdapter.ViewHolder viewHolder, int i) {
viewHolder.simp.setImageURI(xxlist.get(i).getMasterPic());
viewHolder.text_01.setText(xxlist.get(i).getCommodityName());
viewHolder.text_02.setText(xxlist.get(i).getPrice() + "");
}
@Override
public int getItemCount() {
return xxlist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
SimpleDraweeView simp;
TextView text_01, text_02;
public ViewHolder(@NonNull View itemView) {
super(itemView);
simp = (SimpleDraweeView) itemView.findViewById(R.id.mlss_simp);
text_01 = (TextView) itemView.findViewById(R.id.mlss_text_01);
text_02 = (TextView) itemView.findViewById(R.id.mlss_text_02);
}
}
}
《adapter数据列表布局》
《adapter轮播图布局》
android:layout_height=“match_parent”>
《魔力时尚数据展示布局》
日销新品 与品质生活布局与布局与代码写法大致相同