recyclerview mvp 获取数据

封装OKhttp

package com.example.jingdongdemo.net;

import android.os.Handler;
import android.os.Looper;

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public class OkNetUtils {
    private static OkNetUtils instance;
    private final OkHttpClient okHttpClient;

    private final Handler handler;

    private OkNetUtils() {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
        handler = new Handler(Looper.getMainLooper());
    }
    public static OkNetUtils getInstance(){
        if (instance==null){
            instance = new OkNetUtils();
        }
        return instance;
    }

    public void doGet(String url, final OnNetListeren onNetListeren){
        //创建Request
        Request request = new Request.Builder().url(url).build();
        //发送请求
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            onNetListeren.fail(e);
                        }
                    });
            }

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

    }

}
model
package com.example.jingdongdemo.model;

import com.example.jingdongdemo.net.OnNetListeren;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public interface CommodityModel {
    void getAd(String url, OnNetListeren onNetListeren);
}
 
package com.example.jingdongdemo.model;

import com.example.jingdongdemo.bean.CommodityBean;
import com.example.jingdongdemo.net.OkNetUtils;
import com.example.jingdongdemo.net.OnNetListeren;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public class CommodityModelImp implements CommodityModel {
    @Override
    public void getAd(String url, final OnNetListeren onNetListeren) {
        OkNetUtils.getInstance().doGet(url, new OnNetListeren() {
            @Override
            public void success(String result) {
               onNetListeren.success(result);

            }

            @Override
            public void fail(Exception e) {
                onNetListeren.fail(e);
            }
        });
    }
}
presenter
package com.example.jingdongdemo.presenter;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public interface CommodityPresenter {
    void getAd();
}
 
package com.example.jingdongdemo.presenter;

import com.example.jingdongdemo.bean.CommodityBean;
import com.example.jingdongdemo.model.CommodityModelImp;
import com.example.jingdongdemo.net.API;
import com.example.jingdongdemo.net.OnNetListeren;
import com.example.jingdongdemo.ui.CommodityView;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public class CommodityPresenterImp implements CommodityPresenter {

    private final CommodityModelImp commodityModelImp;
    private CommodityView commodityView;
    public CommodityPresenterImp(CommodityView commodityView) {
        this.commodityView=commodityView;
        commodityModelImp = new CommodityModelImp();
    }

    @Override
    public void getAd() {
        commodityModelImp.getAd(API.AD_URL, new OnNetListeren() {
            @Override
            public void success(String result) {
                CommodityBean commodityBean = new Gson().fromJson(result, CommodityBean.class);
               // List<CommodityBean.DataBean> data = commodityBean.getData();
                commodityView.showBanner(commodityBean);
            }

            @Override
            public void fail(Exception e) {

            }
        });
    }

    public void detach(){
        if (commodityView!=null){
            commodityView=null;
        }
    }
}
view
package com.example.jingdongdemo.ui;

import com.example.jingdongdemo.bean.CommodityBean;

import java.util.List;

/**
 * Created by Administrator on 2018/4/18 0018.
 */

public interface CommodityView {
    void showBanner( CommodityBean commodityBean );

}
 
package com.example.jingdongdemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
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.jingdongdemo.R;
import com.example.jingdongdemo.adapter.CLassAdapter;
import com.example.jingdongdemo.adapter.MiaoShaAdapter;
import com.example.jingdongdemo.adapter.TiuJianAdapter;
import com.example.jingdongdemo.bean.ClassBean;
import com.example.jingdongdemo.bean.CommodityBean;
import com.example.jingdongdemo.presenter.ClassPresenterImp;
import com.example.jingdongdemo.presenter.CommodityPresenterImp;
import com.example.jingdongdemo.ui.ClassView;
import com.example.jingdongdemo.ui.CommodityView;
import com.example.jingdongdemo.ui.OnItemClickListeren;
import com.example.jingdongdemo.ui.activity.TiuJianWebActivity;
import com.example.jingdongdemo.ui.activity.WebActivity;
import com.example.jingdongdemo.utils.GlideImageLoader;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;

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

/**
 * Created by Administrator on 2018/4/17 0017.
 */

public class HomeFragment extends Fragment implements CommodityView ,ClassView{
    private Banner banner;
    private CommodityPresenterImp commodityPresenterImp;
    private RecyclerView rv_class;
    private ClassPresenterImp classPresenterImp;
    private MarqueeView marqueeView;
    private RecyclerView rv_miaosha;
    private RecyclerView rv_tiujian;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.homefragment, container, false);
        banner = view.findViewById(R.id.banner);
        //轮播图
        GlideImageLoader loader = new GlideImageLoader();
        banner.setImageLoader(loader);
        //获取数据
        commodityPresenterImp = new CommodityPresenterImp(this);
        commodityPresenterImp.getAd();
        classPresenterImp = new ClassPresenterImp(this);
        classPresenterImp.getCatagory();
        initFindViewById(view);
        return view;
    }
        //找控件
    private void initFindViewById(View view) {
        rv_class = view.findViewById(R.id.rv_class);
        marqueeView = view.findViewById(R.id.marqueeView);
        rv_miaosha = view.findViewById(R.id.rv_miaosha);
        rv_tiujian = view.findViewById(R.id.rv_tiujian);

        initMarqueeView();
        //设置布局管理
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2,RecyclerView.HORIZONTAL,false);
        rv_class.setLayoutManager(gridLayoutManager);
        //设置布局管理
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(getContext(),1,RecyclerView.HORIZONTAL,false);
        rv_miaosha.setLayoutManager(gridLayoutManager1);
        //设置布局管理
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(getContext(),2,RecyclerView.VERTICAL,false);
        rv_tiujian.setLayoutManager(gridLayoutManager2);
    }

    private void initMarqueeView() {
        List<String> info = new ArrayList<>();
        info.add("欢迎访问京东app");
        info.add("大家有没有在 听课");
        info.add("是不是还有人在睡觉");
        info.add("你妈妈在旁边看着呢");
        info.add("赶紧的好好学习吧 马上毕业了");
        info.add("你没有时间睡觉了");
        marqueeView.startWithList(info);

    }

    public void onDestroy() {
        super.onDestroy();
        commodityPresenterImp.detach();
    }
    @Override
    public void showBanner(CommodityBean commodityBean) {
        List<CommodityBean.DataBean> data = commodityBean.getData();
        List<String> urlList= new ArrayList<>();
        for (int i = 0; i <data.size() ; i++) {
            urlList.add(data.get(i).getIcon());
        }
        Log.e("zzzzz",urlList.toString());
        //设置图片集合
        banner.setImages(urlList);
        banner.start();

        //展示秒杀效果
        showMiaoSha(commodityBean);

        showTiuJian(commodityBean);
    }

    private void showTiuJian(CommodityBean commodityBean) {
        final List<CommodityBean.TuijianBean.ListBean> list = commodityBean.getTuijian().getList();
        TiuJianAdapter adapter = new TiuJianAdapter(getContext(),list);
        rv_tiujian.setAdapter(adapter);

        adapter.setOnItemClickListeren(new OnItemClickListeren() {
            @Override
            public void onItemClick(int position) {
                String detailUrl = list.get(position).getDetailUrl();
                Intent intent = new Intent(getActivity(), TiuJianWebActivity.class);
                intent.putExtra("detailUrl",detailUrl);
                startActivity(intent);
            }

            @Override
            public void onLongItemClick(int position) {

            }
        });


    }

    private void showMiaoSha(CommodityBean commodityBean) {
        CommodityBean.MiaoshaBean miaosha = commodityBean.getMiaosha();
        final List<CommodityBean.MiaoshaBean.ListBeanX> list = miaosha.getList();
        MiaoShaAdapter adapter = new MiaoShaAdapter(getContext(),list);
        rv_miaosha.setAdapter(adapter);

       adapter.setOnItemClickListeren(new OnItemClickListeren() {
           @Override
           public void onItemClick(int position) {
               String detailUrl = list.get(position).getDetailUrl();
               Intent intent = new Intent(getActivity(), WebActivity.class);
               intent.putExtra("detailUrl",detailUrl);
               startActivity(intent);
           }

           @Override
           public void onLongItemClick(int position) {

           }
       });


    }

    @Override
    public void showClass(List<ClassBean.DataBean> data) {
        //设置适配器
        CLassAdapter adapter = new CLassAdapter(getContext(),data);
        rv_class.setAdapter(adapter);



    }
}
adapter
 
package com.example.jingdongdemo.adapter;

import android.content.Context;
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.jingdongdemo.R;
import com.example.jingdongdemo.bean.ClassBean;
import com.example.jingdongdemo.bean.CommodityBean;
import com.example.jingdongdemo.ui.OnItemClickListeren;

import java.util.List;

/**
 * Created by Administrator on 2018/4/19 0019.
 */

public class MiaoShaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<CommodityBean.MiaoshaBean.ListBeanX> list;
    private final LayoutInflater inflater;
    private OnItemClickListeren onItemClickListeren;
    public MiaoShaAdapter(Context context, List<CommodityBean.MiaoshaBean.ListBeanX> list) {
        this.context = context;
        this.list = list; inflater = LayoutInflater.from(context);

    }

    public void setOnItemClickListeren(OnItemClickListeren onItemClickListeren){
        this.onItemClickListeren=onItemClickListeren;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.miaoshaitem, parent, false);
        MiaoShaViewHolder miaoShaViewHolder = new MiaoShaViewHolder(view);
        return miaoShaViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MiaoShaViewHolder miaoShaViewHolder = (MiaoShaViewHolder) holder;
        CommodityBean.MiaoshaBean.ListBeanX listBeanX = list.get(position);
        String url = listBeanX.getImages().split("\\|")[0];

        Glide.with(context).load(url).into(miaoShaViewHolder.miaosha_img);
        miaoShaViewHolder.miaosha_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListeren.onItemClick(position);
            }
        });



    }

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


    class MiaoShaViewHolder extends RecyclerView.ViewHolder{

        private final ImageView miaosha_img;

        public MiaoShaViewHolder(View itemView) {
            super(itemView);
            miaosha_img = itemView.findViewById(R.id.miaosha_img);

        }
    }
}
点击接口OnItemClickListeren
package com.example.jingdongdemo.ui;

/**
 * Created by Administrator on 2018/4/19 0019.
 */

public interface OnItemClickListeren {
    void onItemClick(int position);

    void onLongItemClick(int position);
}


package com.example.jingdongdemo.ui;

/**
 * Created by Administrator on 2018/4/19 0019.
 */

public interface OnItemClickListeren {
    void onItemClick(int position);

    void onLongItemClick(int position);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值