1. 程式人生 > >仿京東的首頁+mvp+輪播圖

仿京東的首頁+mvp+輪播圖

————首先寫一下Bean類

public class MessageBean<T> {
    private String msg;
    private String code;
    private T data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
public class Shopper {
    private String sellerid;
    private String sellerName;
    private List<Product> list;

    public String getSellerid() {
        return sellerid;
    }

    public void setSellerid(String sellerid) {
        this.sellerid = sellerid;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public List<Product> getList() {
        return list;
    }

    public void setList(List<Product> list) {
        this.list = list;
    }
}

————View層

public interface IView {

    void failed(Exception e);

    void getBanner(MessageBean<List<Banner>> banners);

    void getProduct(MessageBean<List<Shopper>> products);
}

————INetCallBack類

public interface INetCallBack {

    void success(Object obj);

    void failed(Exception e);
}

————Model層

public class HomeModel {
    public void getData(String url, ICallBack callBack, Type type) {
        HttpUtils.getInstance().get(url, callBack, type);
    }
}

————Presenter層

public class HomePresenter {
    private IView iv;
    private HomeModel model;
    public void attach(IView iv){
        this.iv = iv;
        model = new HomeModel();
    }
    public void getBanner(){
        String url = "http://www.zhaoapi.cn/ad/getAd";
        Type type = new TypeToken<MessageBean<List<Banner>>>() {
        }.getType();
        model.getData(url, new ICallBack() {
            @Override
            public void onSuccess(Object obj) {
                MessageBean<List<Banner>> data = (MessageBean<List<Banner>>) obj;
                iv.getBanner(data);
            }

            @Override
            public void onFailed(Exception e) {
                iv.failed(e);
            }
        },type);
    }
    public void getProduct(){
        String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
        Type type = new TypeToken<MessageBean<List<Shopper>>>() {
        }.getType();
        model.getData(url, new ICallBack() {
            @Override
            public void onSuccess(Object obj) {
                MessageBean<List<Shopper>> data = (MessageBean<List<Shopper>>) obj;
                iv.getProduct(data);
            }

            @Override
            public void onFailed(Exception e) {
                iv.failed(e);
            }
        },type);
    }
    public void detach(){
        if (iv!= null){
            iv = null;
        }
    }
}

————輪播圖的介面卡

public class BannerAdapter extends PagerAdapter {
    private Context context;
    private List<Banner> list;

    public BannerAdapter(Context context, List<Banner> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView img = new ImageView(context);
        Glide.with(context).load(StringUtils.https2Http(list.get(position).getIcon())).into(img);
        container.addView(img);
        return img;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

—————下面列表的介面卡

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{
    private Context context;
    private List<Product> list;

    public ProductAdapter(Context context, List<Product> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(context, R.layout.item_product, null);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = list.get(position);
        String[] strings = product.getImages().split("\\|");
        Glide.with(context)
                .load(StringUtils.https2Http(strings[0]))
                .into(holder.imgProduct);
        holder.txtTitle.setText(product.getTitle());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgProduct;
        private TextView txtTitle;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgProduct = itemView.findViewById(R.id.img_product);
            txtTitle = itemView.findViewById(R.id.txt_title);
        }
    }
}

—————實現方法

public class ShouFragment extends Fragment implements IView {

    private ViewPager vp;
    private RecyclerView rec_view;
    private List<Banner> bannerList;
    private BannerAdapter bannerAdapter;

    private List<Product> productList;
    private ProductAdapter productAdapter;

    private HomePresenter presenter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_shou,container,false);
        vp = view.findViewById(R.id.vp);
        rec_view = view.findViewById(R.id.rec_view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bannerList = new ArrayList<>();
        bannerAdapter = new BannerAdapter(getActivity(), bannerList);
        vp.setAdapter(bannerAdapter);
        productList = new ArrayList<>();
        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        rec_view.setLayoutManager(layoutManager);
        productAdapter = new ProductAdapter(getActivity(), productList);
        rec_view.setAdapter(productAdapter);
        presenter = new HomePresenter();
        presenter.attach(this);
        presenter.getBanner();
        presenter.getProduct();
    }

    @Override
    public void failed(Exception e) {
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getBanner(MessageBean<List<Banner>> banners) {
        if (banners != null){
            List<Banner> data = banners.getData();
            if (data != null){
                bannerList.clear();
                bannerList.addAll(data);
                bannerAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getProduct(MessageBean<List<Shopper>> products) {
        List<Shopper> shoppers = products.getData();
        if (shoppers != null){
            productList.clear();
            for (Shopper shopper : shoppers){
                List<Product> list = shopper.getList();
                if (list != null){
                    productList.addAll(list);
                }
            }
            productAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null){
            presenter.detach();
        }
    }
}

—————不好意思,忘了,最後是佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <android.support.v4.view.ViewPager
       android:id="@+id/vp"
       android:layout_width="match_parent"
       android:layout_height="150dp">
   </android.support.v4.view.ViewPager>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rec_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>