public class ShopAdapter extends RecyclerView.Adapter {
private Context context;
private List<BaseBean> list = new ArrayList<>();
public ShopAdapter(Context context) {
this.context = context;
}
public void setList(List<BaseBean> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
int type = getItemViewType(i);
RecyclerView.ViewHolder viewHolder = null;
switch (type) {
//熱銷新品
case 0:
View viewrxxp = View.inflate(context, R.layout.sanite, null);
viewHolder = new MyViewHolderRxxp(viewrxxp);
break;
//品質生活
case 1:
View viewpzsh = View.inflate(context, R.layout.sanite, null);
viewHolder = new MyViewHolderRxxp(viewpzsh);
break;
//魔麗時尚
case 2:
View viewmlsh = View.inflate(context, R.layout.sanite, null);
viewHolder = new MyViewHolderRxxp(viewmlsh);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof MyViewHolderRxxp &&
list.get(i) instanceof ShopBean.ResultBean.RxxpBean) {//熱銷新品
ShopBean.ResultBean.RxxpBean rxxpBean = (ShopBean.ResultBean.RxxpBean) list.get(i);
((MyViewHolderRxxp) viewHolder).mTitle.setText(rxxpBean.getName());
((MyViewHolderRxxp) viewHolder).mLayoutTitle.setBackgroundColor(Color.RED);
RxxpAdapter rxxpAdapter = new RxxpAdapter(context, rxxpBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
((MyViewHolderRxxp) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolderRxxp) viewHolder).mRecycler.setAdapter(rxxpAdapter);
} else if (viewHolder instanceof MyViewHolderRxxp &&
list.get(i) instanceof ShopBean.ResultBean.MlssBean) {//魔力時尚
ShopBean.ResultBean.MlssBean mlssBean = (ShopBean.ResultBean.MlssBean) list.get(i);
((MyViewHolderRxxp) viewHolder).mTitle.setText(mlssBean.getName());
((MyViewHolderRxxp) viewHolder).mLayoutTitle.setBackgroundColor(Color.YELLOW);
MlssAdapter mlssAdapter = new MlssAdapter(context, mlssBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
((MyViewHolderRxxp) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolderRxxp) viewHolder).mRecycler.setAdapter(mlssAdapter);
} else if (viewHolder instanceof MyViewHolderRxxp &&
list.get(i) instanceof ShopBean.ResultBean.PzshBean) {//品質生活
ShopBean.ResultBean.PzshBean pzshBean = (ShopBean.ResultBean.PzshBean) list.get(i);
((MyViewHolderRxxp) viewHolder).mTitle.setText(pzshBean.getName());
((MyViewHolderRxxp) viewHolder).mLayoutTitle.setBackgroundColor(Color.BLUE);
PzshAdapter pzshAdapter = new PzshAdapter(context, pzshBean.getCommodityList());
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
((MyViewHolderRxxp) viewHolder).mRecycler.setLayoutManager(gridLayoutManager);
((MyViewHolderRxxp) viewHolder).mRecycler.setAdapter(pzshAdapter);
}
}
//多條目
@Override
public int getItemViewType(int position) {
if (list.get(position) instanceof ShopBean.ResultBean) {
return 0;
} else if (list.get(position) instanceof ShopBean.ResultBean) {
return 1;
} else {
return 2;
}
}
@Override
public int getItemCount() {
return list.size();
}
//熱銷新品
private class MyViewHolderRxxp extends RecyclerView.ViewHolder {
RelativeLayout mLayoutTitle;
TextView mTitle;
RecyclerView mRecycler;
public MyViewHolderRxxp(@NonNull View itemView) {
super(itemView);
mRecycler = (RecyclerView) itemView.findViewById(R.id.adapter_recycler);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mLayoutTitle = (RelativeLayout) itemView.findViewById(R.id.layout_title);
}
}
}
public class RxxpAdapter extends RecyclerView.Adapter<RxxpAdapter.RxxpViewHolder> {
private Context context;
private List<ShopBean.ResultBean.RxxpBean.CommodityListBean> commodityList = new ArrayList<>();
public RxxpAdapter(Context context, List<ShopBean.ResultBean.RxxpBean.CommodityListBean> commodityList) {
this.context = context;
this.commodityList = commodityList;
}
@NonNull
@Override
public RxxpViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.adapter_rxxp, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, final int i) {
rxxpViewHolder.mTitle.setText(commodityList.get(i).getCommodityName());
rxxpViewHolder.mDesc.setText(commodityList.get(i).getPrice() + "");
//Glide.with(context).load(commodityList.get(i).getMasterPic()).into(rxxpViewHolder.mImage);
rxxpViewHolder.mImage.setImageURI(commodityList.get(i).getMasterPic());
rxxpViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = commodityList.get(i).getCommodityName();
String pic = commodityList.get(i).getMasterPic();
String price = commodityList.get(i).getPrice()+"";
EventBus.getDefault().post(new EventBean(name,pic,price));;
}
});
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (SimpleDraweeView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<RelativeLayout
android:id="@+id/layout_title"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/adapter_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_title" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="120dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image_view"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#d43c3c" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>