public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements HotAdapter.HotClicklinsenter,StyleAdapter.StyleClicklinsenter,
LiveAdapter.LiveClicklinsenter{
private Context context;
private HomeBean homeBean;
private List<BannerBean.ResultBean> bannerbean;
private final int BANNER = 0;
private final int HOT = 1;
private final int STYLE = 2;
private final int LIVE = 3;
private List<String> list;
public HomeAdapter(Context context) {
this.context = context;
homeBean=new HomeBean();
bannerbean = new ArrayList<>();
}
public void setHomeBean(HomeBean homeBean) {
this.homeBean = homeBean;
notifyDataSetChanged();
}
public void setBannerbean(List<BannerBean.ResultBean> bannerbean) {
list = new ArrayList<>();
for (BannerBean.ResultBean resultBean : bannerbean) {
list.add(resultBean.getImageUrl());
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
if (getItemViewType(i)==BANNER){
view = LayoutInflater.from(context).inflate(R.layout.main_item_banner,viewGroup,false);
return new ViewHoder_banner(view);
}else if (getItemViewType(i)==HOT){
view = LayoutInflater.from(context).inflate(R.layout.main_item_rxsp,viewGroup,false);
return new ViewHoder_hot(view);
}else if (getItemViewType(i)==STYLE){
view = LayoutInflater.from(context).inflate(R.layout.main_item_mlss,viewGroup,false);
return new ViewHoder_style(view);
}else {
view = LayoutInflater.from(context).inflate(R.layout.main_item_pzsh,viewGroup,false);
return new ViewHoder_live(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int i) {
if (getItemViewType(i)==BANNER){
if (list!=null) {
((ViewHoder_banner) holder).xBanner.setData(list,null);
((ViewHoder_banner) holder).xBanner.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
Glide.with(context).load(list.get(position)).into((ImageView) view);
}
});
}
}else if (getItemViewType(i)==HOT){
HotAdapter hotAdapter = new HotAdapter(context, homeBean.result.rxxp.commodityList);
((ViewHoder_hot)holder).rxsp_rcve.setLayoutManager(new GridLayoutManager(context,3));
hotAdapter.setHotClicklinsenter(this);
((ViewHoder_hot)holder).rxsp_rcve.setAdapter(hotAdapter);
}else if (getItemViewType(i)==STYLE){
StyleAdapter styleAdapter = new StyleAdapter(context,homeBean.result.mlss.commodityList);
((ViewHoder_style)holder).mlss_rcve.setLayoutManager(new LinearLayoutManager(context));
styleAdapter.setStyleClicklinsenter(this);
((ViewHoder_style)holder).mlss_rcve.setAdapter(styleAdapter);
}else{
LiveAdapter liveAdapter = new LiveAdapter(context,homeBean.result.pzsh.commodityList);
((ViewHoder_live)holder).pzsh_rcve.setLayoutManager(new GridLayoutManager(context,2));
liveAdapter.setLiveClicklinsenter(this);
((ViewHoder_live)holder).pzsh_rcve.setAdapter(liveAdapter);
}
}
@Override
public int getItemViewType(int position) {
if (position==0){
return BANNER;
}else if (position==1){
return HOT;
}else if (position==2){
return STYLE;
}else {
return LIVE;
}
}
@Override
public int getItemCount() {
return homeBean==null?0:4;
}
private HomeClicklinsenter homeClicklinsenter;
public void setHomeClicklinsenter(HomeClicklinsenter homeClicklinsenter) {
this.homeClicklinsenter = homeClicklinsenter;
}
public interface HomeClicklinsenter{
void homeclick(String homeid);
}
@Override
public void hotclick(String hotid) {
homeClicklinsenter.homeclick(hotid);
}
@Override
public void liveclick(String hotid) {
homeClicklinsenter.homeclick(hotid);
}
@Override
public void styleclick(String hotid) {
homeClicklinsenter.homeclick(hotid);
}
class ViewHoder_banner extends RecyclerView.ViewHolder{
private final XBanner xBanner;
public ViewHoder_banner(View itemView) {
super(itemView);
xBanner = itemView.findViewById(R.id.xbanner);
}
}
class ViewHoder_hot extends RecyclerView.ViewHolder{
private final RecyclerView rxsp_rcve;
public ViewHoder_hot(View itemView) {
super(itemView);
rxsp_rcve = itemView.findViewById(R.id.rxsp_recy);
}
}
class ViewHoder_style extends RecyclerView.ViewHolder{
private final RecyclerView mlss_rcve;
public ViewHoder_style(View itemView) {
super(itemView);
mlss_rcve = itemView.findViewById(R.id.mlss_recy);
}
}
class ViewHoder_live extends RecyclerView.ViewHolder{
private final RecyclerView pzsh_rcve;
public ViewHoder_live(View itemView) {
super(itemView);
pzsh_rcve = itemView.findViewById(R.id.pzsh_recy);
}
}
}
第二个条目适配器
public class HotAdapter extends RecyclerView.Adapter<HotAdapter.Myvh> {
private Context context;
private List<HomeBean.ResultBean.listBean.CommodityListBean> list;
public HotAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_item_rxsp_item,parent,false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull Myvh holder, int position) {
Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
holder.name.setText(list.get(position).commodityName);
holder.price.setText(list.get(position).price+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final ImageView imageView;
private final TextView price;
private final TextView name;
public Myvh(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image1);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
}
}
}
第三个条目适配器
public class StyleAdapter extends RecyclerView.Adapter<StyleAdapter.Myvh> {
private Context context;
private List<HomeBean.ResultBean.listBean.CommodityListBean> list;
public StyleAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_item_mlss_item,parent,false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull Myvh holder, int position) {
Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
holder.name.setText(list.get(position).commodityName);
holder.price.setText(list.get(position).price+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final ImageView imageView;
private final TextView price;
private final TextView name;
public Myvh(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image1);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
}
}
}
第四个条目适配器
public class LiveAdapter extends RecyclerView.Adapter<LiveAdapter.Myvh> {
private Context context;
private List<HomeBean.ResultBean.listBean.CommodityListBean> list;
public LiveAdapter(Context context, List<HomeBean.ResultBean.listBean.CommodityListBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_item_pzsh_item,parent,false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull Myvh holder, int position) {
Glide.with(context).load(list.get(position).masterPic).into(holder.imageView);
holder.name.setText(list.get(position).commodityName);
holder.price.setText(list.get(position).price+"");
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final ImageView imageView;
private final TextView price;
private final TextView name;
public Myvh(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image1);
name = itemView.findViewById(R.id.name);
price = itemView.findViewById(R.id.price);
}
}
}
bean类
public class HomeBean {
public ResultBean result;
public String message;
public String status;
public static class ResultBean {
public listBean rxxp;
public listBean pzsh;
public listBean mlss;
public static class listBean {
public int id;
public String name;
public List<CommodityListBean> commodityList;
public static class CommodityListBean {
public int commodityId;
public String commodityName;
public String masterPic;
public int price;
public int saleNum;
}
}
}
}