主頁面
public class FragMentHome extends Fragment implements MianView {
private RecyclerView mRecycler;
private MainAdapter mainAdapter;
private MainPresenterIml mainPresenterIml;
private DialogView mDialogView;
private List<BaseBean> baseBeanList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frammenthome,container,false);
mRecycler = view.findViewById(R.id.recycler);
//進度框
mDialogView = view.findViewById(R.id.dialog);
mainAdapter = new MainAdapter(getActivity());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecycler.setLayoutManager(linearLayoutManager);
mRecycler.setAdapter(mainAdapter);
mainPresenterIml = new MainPresenterIml(new MainModelIml(), this);
mainPresenterIml.doBanner(0,"http://172.17.8.100/small/commodity/v1/bannerShow");
mainPresenterIml.doHomeList(1,"http://172.17.8.100/small/commodity/v1/commodityList");
return view;
}
@Override
public void success(int type, String data) {
if (type==0){
BannerBean bannerBean = new Gson().fromJson(data, BannerBean.class);
baseBeanList.add(bannerBean);
}else {
HomeListBean homeListBean = new Gson().fromJson(data, HomeListBean.class);
HomeListBean.ResultBean.RxxpBean rxxpBean = homeListBean.getResult().getRxxp();
HomeListBean.ResultBean.PzshBean pzshBean = homeListBean.getResult().getPzsh();
HomeListBean.ResultBean.MlssBean mlssBean = homeListBean.getResult().getMlss();
baseBeanList.add(rxxpBean);
baseBeanList.add(pzshBean);
baseBeanList.add(mlssBean);
mainAdapter.setList(baseBeanList);
}
}
@Override
public void fail(int type, String error) {
}
@Override
public void showDialog() {
mDialogView.setVisibility(View.VISIBLE);
}
@Override
public void hintDialog() {
mDialogView.setVisibility(View.GONE);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<bw.bawei.com.lijimei041.DialogView
android:id="@+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
public class MainAdapter extends RecyclerView.Adapter {
private Context context;
private List<BaseBean> list = new ArrayList<>();
public MainAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
int type = getItemViewType(i);
RecyclerView.ViewHolder viewHolder = null;
switch (type){
case 0://Banner圖
View view0 = View.inflate(context, R.layout.banneritem, null);
viewHolder = new MyViewHolder_0(view0);
break;
case 1://熱銷新品
View view1 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view1);
break;
case 2://魔力時尚
View view2 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view2);
break;
case 3://品質生活
View view3 = View.inflate(context, R.layout.adapter_rxxp_mlss_pzss, null);
viewHolder = new MyViewHolder_1(view3);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof MyViewHolder_0 && list.get(i) instanceof BannerBean) {//加載Banner
BannerBean bannerBean = (BannerBean) list.get(i);
((MyViewHolder_0) viewHolder).xBanner.setBannerData(bannerBean.getResult());
((MyViewHolder_0) viewHolder).xBanner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
BannerBean.ResultBean bean = (BannerBean.ResultBean) model;
Glide.with(context).load(bean.getImageUrl()).into((ImageView) view);
}
});
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.RxxpBean) {//熱銷新品
HomeListBean.ResultBean.RxxpBean rxxpBean = (HomeListBean.ResultBean.RxxpBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(rxxpBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.RED);
RxxpAdapter rxxpAdapter = new RxxpAdapter(context, rxxpBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(rxxpAdapter);
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.MlssBean) {//魔力時尚
HomeListBean.ResultBean.MlssBean mlssBean = (HomeListBean.ResultBean.MlssBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(mlssBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.YELLOW);
MlssAdapter mlssAdapter = new MlssAdapter(context, mlssBean.getCommodityList());
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(linearLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(mlssAdapter);
} else if (viewHolder instanceof MyViewHolder_1 &&
list.get(i) instanceof HomeListBean.ResultBean.PzshBean) {//品質生活
HomeListBean.ResultBean.PzshBean pzshBean = (HomeListBean.ResultBean.PzshBean) list.get(i);
((MyViewHolder_1) viewHolder).mTitle.setText(pzshBean.getName());
((MyViewHolder_1) viewHolder).mLayoutTitle.setBackgroundColor(Color.BLUE);
PzshAdapter pzshAdapter = new PzshAdapter(context, pzshBean.getCommodityList());
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
((MyViewHolder_1) viewHolder).mRecycler.setLayoutManager(gridLayoutManager);
((MyViewHolder_1) viewHolder).mRecycler.setAdapter(pzshAdapter);
}
}
@Override
public int getItemViewType(int position) {
int type=0;
if (list.get(position) instanceof BannerBean){
type=0;
}else if (list.get(position) instanceof HomeListBean.ResultBean){
type=1;
}else if (list.get(position) instanceof HomeListBean.ResultBean){
type =2;
}else {
type=3;
}
return type;
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<BaseBean> list) {
this.list=list;
notifyDataSetChanged();
}
private class MyViewHolder_0 extends RecyclerView.ViewHolder{
XBanner xBanner;
public MyViewHolder_0(@NonNull View itemView) {
super(itemView);
xBanner = itemView.findViewById(R.id.xbanner);
}
}
private class MyViewHolder_1 extends RecyclerView.ViewHolder {
RelativeLayout mLayoutTitle;
TextView mTitle;
RecyclerView mRecycler;
public MyViewHolder_1(@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);
}
}
}
//子適配器
/**
* author:AbnerMing
* date:2019/4/1
* 魔力時尚的適配器
*/
public class MlssAdapter extends RecyclerView.Adapter<MlssAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.MlssBean.CommodityListBeanXX> commodityList = new ArrayList<>();
public MlssAdapter(Context context, List<HomeListBean.ResultBean.MlssBean.CommodityListBeanXX> 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_mlss, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, 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);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
/**
* author:AbnerMing
* date:2019/4/1
* 品質生活的適配器
*/
public class PzshAdapter extends RecyclerView.Adapter<PzshAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.PzshBean.CommodityListBeanX> commodityList = new ArrayList<>();
public PzshAdapter(Context context, List<HomeListBean.ResultBean.PzshBean.CommodityListBeanX> 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_pzss, null);
RxxpViewHolder rxxpViewHolder = new RxxpViewHolder(view);
return rxxpViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RxxpViewHolder rxxpViewHolder, 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);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) itemView.findViewById(R.id.image_view);
mTitle = (TextView) itemView.findViewById(R.id.tv_title);
mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
}
}
}
**
* author:AbnerMing
* date:2019/4/1
* 熱銷新品的適配器
*/
public class RxxpAdapter extends RecyclerView.Adapter<RxxpAdapter.RxxpViewHolder> {
private Context context;
private List<HomeListBean.ResultBean.RxxpBean.CommodityListBean> commodityList = new ArrayList<>();
public RxxpAdapter(Context context, List<HomeListBean.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, 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);
}
@Override
public int getItemCount() {
return commodityList.size();
}
public class RxxpViewHolder extends RecyclerView.ViewHolder {
private ImageView mImage;
private TextView mTitle, mDesc;
public RxxpViewHolder(@NonNull View itemView) {
super(itemView);
mImage = (ImageView) 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:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
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>
<?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"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:id="@+id/image_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_desc"
android:textColor="#d43c3c"
/>
</LinearLayout>
</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">
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/image_view"
android:ellipsize="end"
android:lines="1"
android:text="111" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_title"
android:textColor="#d43c3c" />
</RelativeLayout>
</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: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>