首页轮播+多条目+viewPager

本文详细介绍了一个Android应用的布局设计,包括主界面布局、Fragment布局、广告轮播图、多条目布局及热销商品子条目等。同时,深入解析了API调用流程,从定义API地址到使用OkHttp进行网络请求,再到Model、View、Presenter模式的实现,最后展示了MainActivity和HomeFragment的具体代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

fragmentHome

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/xr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        ></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

banner

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

   <com.recker.flybanner.FlyBanner
       android:id="@+id/flybnner"
       android:layout_width="0dp"
       android:layout_height="180dp"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       ></com.recker.flybanner.FlyBanner>

</android.support.constraint.ConstraintLayout>

多条目的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="热销商品"
        android:gravity="center"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/rx_xr"
        />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rx_xr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        ></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

热销商品子条目

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="6dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <ImageView
            android:id="@+id/img"
            android:layout_width="100dp"
            android:layout_height="108dp"
            android:src="@mipmap/ic_launcher"
            android:paddingLeft="3dp"
            android:paddingRight="3dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            />
        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/img"
            android:textSize="12sp"
            android:text="名字"
            android:layout_marginLeft="10dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:maxLines="1"
            />
        <TextView
            android:id="@+id/price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/name"
            android:textSize="14sp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintLeft_toLeftOf="parent"
            android:textColor="#f00"
            android:text="价格"
            app:layout_constraintRight_toRightOf="parent"
            />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

品质生活

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="品质生活"
        android:gravity="center"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/rx_xr"
        />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/pz_xr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        ></android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>

api

package com.example.dell.shouye.api;

public class GoodsApi {
    public static final String XBanner_Api="http://172.17.8.100/small/commodity/v1/bannerShow";
    public static final String Home_Api="http://172.17.8.100/small/commodity/v1/commodityList";
}

utils

package com.example.dell.shouye.utils;

import android.os.Handler;

import com.example.dell.shouye.callback.OkHttpUtilsCallBack;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtils {
    private Handler handler=new Handler();
    private static OkHttpUtils okHttpUtils;
    private final OkHttpClient okHttpClient;

    //创建私有构造
    private OkHttpUtils(){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS)
                .connectTimeout(5,TimeUnit.SECONDS)
                .build();
    }
    public static OkHttpUtils getInstance(){
        if (okHttpUtils==null){
            synchronized (OkHttpUtils.class){
                if (okHttpUtils==null){
                    okHttpUtils=new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }

   //doPost方法
    public void doGet(String url, final OkHttpUtilsCallBack okHttpUtilsCallBack){
         Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                       okHttpUtilsCallBack.onFilure("网络异常");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String result = response.body().string();
                handler.post(new Runnable() {
                  @Override
                  public void run() {
                     okHttpUtilsCallBack.onSuccess(result);
                  }
              });
            }
        });
    }
}

contract

package com.example.dell.shouye.contract;

import com.example.dell.shouye.callback.OkHttpUtilsCallBack;

import java.util.HashMap;

public interface GoodsContract {

    //m层
    interface IModle{
       void BannerModle(HashMap<String,String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack);
       void ProductModle(HashMap<String,String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack);
    }
    //v层
    interface IView{
        void bannerSuccess(String result);
        void bnnerFilure(String msg);
        void productSuccess(String result);
        void productFilure(String msg);
    }
    //p层
    abstract class IPresenter{
        public abstract void bnnerPresenter(HashMap<String,String> map);
        public abstract void productPresenter(HashMap<String,String> map);
    }
}

modle

package com.example.dell.shouye.modle;

import com.example.dell.shouye.api.GoodsApi;
import com.example.dell.shouye.callback.OkHttpUtilsCallBack;
import com.example.dell.shouye.contract.GoodsContract;
import com.example.dell.shouye.utils.OkHttpUtils;

import java.util.HashMap;

public class GoodsModle implements GoodsContract.IModle {

    @Override
    public void BannerModle(HashMap<String, String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack) {
        OkHttpUtils.getInstance().doGet(GoodsApi.XBanner_Api, new OkHttpUtilsCallBack() {
            @Override
            public void onSuccess(String result) {
                okHttpUtilsCallBack.onSuccess(result);
            }

            @Override
            public void onFilure(String msg) {
                   okHttpUtilsCallBack.onFilure(msg);
            }
        });
    }

    @Override
    public void ProductModle(HashMap<String, String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack) {
        OkHttpUtils.getInstance().doGet(GoodsApi.Home_Api, new OkHttpUtilsCallBack() {
            @Override
            public void onSuccess(String result) {
                okHttpUtilsCallBack.onSuccess(result);
            }

            @Override
            public void onFilure(String msg) {
                okHttpUtilsCallBack.onFilure(msg);
            }
        });
    }
}

presenter

package com.example.dell.shouye.presenter;

import com.example.dell.shouye.callback.OkHttpUtilsCallBack;
import com.example.dell.shouye.contract.GoodsContract;
import com.example.dell.shouye.modle.GoodsModle;

import java.util.HashMap;

public class GoodsPresenter extends GoodsContract.IPresenter{
    private GoodsModle goodsModle;
    private GoodsContract.IView iView;

    public GoodsPresenter(GoodsContract.IView iView) {
        this.iView = iView;
        this.goodsModle=new GoodsModle();
    }

    @Override
    public void bnnerPresenter(HashMap<String, String> map) {
        goodsModle.BannerModle(map, new OkHttpUtilsCallBack() {
            @Override
            public void onSuccess(String result) {
                 if (iView!=null){
                     iView.bannerSuccess(result);
                 }
            }

            @Override
            public void onFilure(String msg) {
                 if (iView!=null){
                     iView.bnnerFilure(msg);
                 }
            }
        });
    }

    @Override
    public void productPresenter(HashMap<String, String> map) {
        goodsModle.ProductModle(map, new OkHttpUtilsCallBack() {
            @Override
            public void onSuccess(String result) {
                if (iView!=null){
                    iView.productSuccess(result);
                }
            }

            @Override
            public void onFilure(String msg) {
                  if (iView!=null){
                      iView.productFilure(msg);
                  }
            }
        });
    }
}

MainActivity

package com.example.dell.shouye;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.example.dell.shouye.fragment.HomeFragment;
import com.example.dell.shouye.fragment.OtherFragment;

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    pager.setCurrentItem(0);
                    //mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    pager.setCurrentItem(1);
                    //mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    pager.setCurrentItem(2);
                    //mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.navigation_hom:
                    pager.setCurrentItem(3);
                    return true;
                case R.id.navigation_das:
                    pager.setCurrentItem(4);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager= (ViewPager) findViewById(R.id.pager);
        final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                switch (i){
                    case 0:
                        return new HomeFragment();
                    case 1:
                        return new OtherFragment();
                    case 2:
                        return new OtherFragment();
                    case 3:
                        return new OtherFragment();
                    case 4:
                        return new OtherFragment();
                }
                return null;
            }

            @Override
            public int getCount() {
                return 5;
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0:
                        navigation.setSelectedItemId(R.id.navigation_home);
                    case 1:
                        navigation.setSelectedItemId(R.id.navigation_dashboard);
                    case 2:
                        navigation.setSelectedItemId(R.id.navigation_notifications);
                    case 3:
                        navigation.setSelectedItemId(R.id.navigation_hom);
                    case 4:
                        navigation.setSelectedItemId(R.id.navigation_das);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

}

HomeFragment

package com.example.dell.shouye.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dell.shouye.R;
import com.example.dell.shouye.adapter.MyAdapter;
import com.example.dell.shouye.bean.BannerBean;
import com.example.dell.shouye.bean.ProductBean;
import com.example.dell.shouye.contract.GoodsContract;
import com.example.dell.shouye.presenter.GoodsPresenter;
import com.google.gson.Gson;
import java.util.HashMap;

public class HomeFragment extends Fragment implements GoodsContract.IView {

    private RecyclerView recy;
    private MyAdapter myAdapter;
    private GoodsPresenter goodsPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, container, false);
        return view;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recy = view.findViewById(R.id.xr);

        recy.setLayoutManager(new LinearLayoutManager(getActivity()));

        //添加适配器
        myAdapter = new MyAdapter(getActivity());


        //实例化p层
        goodsPresenter = new GoodsPresenter(this);
        goodsPresenter.bnnerPresenter(new HashMap<String, String>());
        goodsPresenter.productPresenter(new HashMap<String, String>());
    }

    //banner轮播图的方法
    @Override
    public void bannerSuccess(String result) {
        BannerBean bannerBean = new Gson().fromJson(result, BannerBean.class);
            myAdapter.setBnner(bannerBean.result);
            recy.setAdapter(myAdapter);


        Log.e("tag","view bannerSuccess = " + result);
    }

    @Override
    public void bnnerFilure(String msg) {


    }

    //展示列表的成功失败方法
    @Override
    public void productSuccess(String result) {
        ProductBean productBean = new Gson().fromJson(result, ProductBean.class);
            myAdapter.setList(productBean);
            recy.setAdapter(myAdapter);

    }

    @Override
    public void productFilure(String msg) {

    }
}

MyAdapter

package com.example.dell.shouye.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dell.shouye.R;
import com.example.dell.shouye.bean.BannerBean;
import com.example.dell.shouye.bean.ProductBean;
import com.recker.flybanner.FlyBanner;

import java.util.ArrayList;
import java.util.List;


public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<BannerBean.Data> resultBean;
   private ProductBean productBean;

    public MyAdapter(Context context) {
        this.context = context;
        this.resultBean=new ArrayList<>();
    }

    public void setList(ProductBean productBean) {
        this.productBean=productBean;
        notifyDataSetChanged();
    }
    private final int BANNER = 0;
    private List<String> list;
    public void setBnner(List<BannerBean.Data> result) {
         list=new ArrayList<>();
         for (BannerBean.Data k:result){
             list.add(k.imageUrl);
         }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (getItemViewType(i)==BANNER){
            View view = LayoutInflater.from(context).inflate(R.layout.activity_banner, viewGroup, false);
            return new BannerHolder(view);
        }else if (getItemViewType(i)==1){
            View view = LayoutInflater.from(context).inflate(R.layout.activity_rxsp, viewGroup, false);
            return new RxspHolder(view);
        }else if(getItemViewType(i)==2){
            View view = LayoutInflater.from(context).inflate(R.layout.activity_pzsh, viewGroup, false);
            return new PzshViewHolder(view);
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.activity_mlss, viewGroup, false);
            return new MlssHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (getItemViewType(i)==BANNER){
            if (list!=null){
                ((BannerHolder) viewHolder).flaBanner.setImagesUrl(list);
            }
            //热销商品
        }else if(getItemViewType(i)==1){
            RespAdapter respAdapter = new RespAdapter(context,productBean.getResult().getRxxp().getCommodityList());
            ((RxspHolder)viewHolder).rexXr.setLayoutManager(new GridLayoutManager(context,3));
            ((RxspHolder)viewHolder).rexXr.setAdapter(respAdapter);

            //品质生活
        }else if (getItemViewType(i)==2){
            PzshAdapter pzshAdapter = new PzshAdapter(context,productBean.getResult().getPzsh().getCommodityList());
            ((PzshViewHolder)viewHolder).pzshXr.setLayoutManager(new LinearLayoutManager(context));
            ((PzshViewHolder)viewHolder).pzshXr.setAdapter(pzshAdapter);
        }else{
            //魔力时尚
            MLssAdapter mLssAdapter = new MLssAdapter(context,productBean.getResult().getMlss().getCommodityList());
            ((MlssHolder)viewHolder).mlssXr.setLayoutManager(new GridLayoutManager(context,2));
            ((MlssHolder)viewHolder).mlssXr.setAdapter(mLssAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
      if (position==BANNER){
          return BANNER;
      }else if (position==1){
          return 1;
      }else if (position==2){
          return 2;
      }else{
          return 3;
      }
    }

    //轮播图
   class BannerHolder extends RecyclerView.ViewHolder{

       private final FlyBanner flaBanner;

       public BannerHolder(View view) {
           super(view);
           flaBanner = view.findViewById(R.id.flybnner);
       }
   }

   //热销商品
   class RxspHolder extends RecyclerView.ViewHolder{

       private final RecyclerView rexXr;

       public RxspHolder(View view) {
           super(view);
           rexXr = view.findViewById(R.id.rx_xr);
       }
   }
  //品质生活
  class PzshViewHolder extends RecyclerView.ViewHolder{

      private final RecyclerView pzshXr;;

      public PzshViewHolder(View view) {
          super(view);
          pzshXr = view.findViewById(R.id.pz_xr);
      }
  }
     //魔力时尚
    class MlssHolder extends RecyclerView.ViewHolder{

         private final RecyclerView mlssXr;

         public MlssHolder(View view) {
            super(view);
             mlssXr = view.findViewById(R.id.ml_xr);
         }
    }
}

品质生活Adaapter

package com.example.dell.shouye.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.shouye.R;
import com.example.dell.shouye.bean.ProductBean;

import java.util.List;

public class PzshAdapter extends RecyclerView.Adapter<PzshAdapter.PzshHolder> {
    private Context context;
    private List<ProductBean.ResultBean.PzshBean.CommodityListBeanX> list;

    public PzshAdapter(Context context, List<ProductBean.ResultBean.PzshBean.CommodityListBeanX> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public PzshHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_pzsh_item, viewGroup, false);
        PzshHolder pzshHolder = new PzshHolder(view);
        return pzshHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull PzshHolder pzshHolder, int i) {
        ProductBean.ResultBean.PzshBean.CommodityListBeanX commodityListBeanX = list.get(i);
        pzshHolder.nameVH.setText(commodityListBeanX.getCommodityName());
        pzshHolder.priceVh.setText(commodityListBeanX.getPrice()+"");
        Glide.with(context).load(commodityListBeanX.getMasterPic()).into(pzshHolder.img);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class PzshHolder extends RecyclerView.ViewHolder {

        private final ImageView img;
        private final TextView nameVH;
        private final TextView priceVh;

        public PzshHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            nameVH = itemView.findViewById(R.id.name);
            priceVh = itemView.findViewById(R.id.price);
        }
    }
}

热销商品Adaapter

package com.example.dell.shouye.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.shouye.R;
import com.example.dell.shouye.bean.ProductBean;

import java.util.List;

public class RespAdapter extends RecyclerView.Adapter<RespAdapter.RespHolder> {
   private Context context;
   private List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list;

    public RespAdapter(Context context, List<ProductBean.ResultBean.RxxpBean.CommodityListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RespHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_pzsh_item, viewGroup, false);
        RespHolder respHolder = new RespHolder(view);
        return respHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RespHolder respHolder, int i) {
        ProductBean.ResultBean.RxxpBean.CommodityListBean commodityListBean = list.get(i);
        respHolder.name.setText(commodityListBean.getCommodityName());
        respHolder.price.setText(commodityListBean.getPrice()+"");
        Glide.with(context).load(commodityListBean.getMasterPic()).into(respHolder.img);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class RespHolder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final ImageView img;
        private final TextView price;

        public RespHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            img = itemView.findViewById(R.id.img);
            price = itemView.findViewById(R.id.price);
        }
    }
}

魔力时尚Adapter

package com.example.dell.shouye.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.shouye.R;
import com.example.dell.shouye.bean.ProductBean;

import java.util.List;

public class MLssAdapter extends RecyclerView.Adapter<MLssAdapter.MlssHolder> {
  private Context context;
  private List<ProductBean.ResultBean.MlssBean.CommodityListBeanXX> list;

    public MLssAdapter(Context context, List<ProductBean.ResultBean.MlssBean.CommodityListBeanXX> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MlssHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_mlss_item, viewGroup, false);
        MlssHolder mlssHolder = new MlssHolder(view);
        return mlssHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MlssHolder mlssHolder, int i) {
        ProductBean.ResultBean.MlssBean.CommodityListBeanXX commodityListBeanXX = list.get(i);
        mlssHolder.nameTv.setText(commodityListBeanXX.getCommodityName());
        mlssHolder.priceTv.setText(commodityListBeanXX.getPrice()+"");
        Glide.with(context).load(commodityListBeanXX.getMasterPic()).into(mlssHolder.imgTv);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class
    MlssHolder extends RecyclerView.ViewHolder {

        private final TextView nameTv;
        private final ImageView imgTv;
        private final TextView priceTv;

        public MlssHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.name);
            imgTv = itemView.findViewById(R.id.img);
            priceTv = itemView.findViewById(R.id.price);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值