mvp展示

//Contract接口
public interface ContarctIntface {
    //view层商品接口
    public interface ProductIntface{
        public void ShowDisplay(Object obj);
    }
    //p层的接口
    public interface  PresenterIntface{
        public void toProduct();
    }
}






//展示Activity

public class ProductActivity extends AppCompatActivity implements ContarctIntface.ProductIntface{
    //声明p层接口对象
    ContarctIntface.PresenterIntface presenterIntface;
    List<ProductBean.ProductData.Product>mlist=new ArrayList<>();
    MyAdapter myAdapter;
    RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_layout);
        recyclerView=findViewById(R.id.RecyclerView_id);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        myAdapter = new MyAdapter(mlist, this);
        recyclerView.setAdapter(myAdapter);
        presenterIntface=new MyPresenter(this);
        presenterIntface.toProduct();

    }

    @Override
    public void ShowDisplay(Object obj) {
    ProductBean productBean=(ProductBean)obj;
    mlist.addAll(productBean.data.data);
    myAdapter.notifyDataSetChanged();
    }
}







//Okhttp

public class OkhttpUtil {
   static OkhttpUtil util;
    OkHttpClient okHttpClient;
    //构造方法私有化
    private OkhttpUtil(){
        okHttpClient=new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
    }
    public static synchronized OkhttpUtil getInstance(){
        if(util==null){
            util=new OkhttpUtil();
        }
        return util;
    }
    //get
    public void doGet(Callback callback){
        Request request=new Request.Builder()
                .url("http://365jia.cn/news/api3/365jia/news/headline?page=1")
                .get()
                .build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);

    }
    public class MyInterceptor implements Interceptor{

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request=chain.request();
            Response response=chain.proceed(request);
            return response;
        }
    }
}










//MyModel

public class MyModel {
    public static final String TAG="MyModel";
    MyCallBack myCallBack;
    Handler  mhandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String json=(String) msg.obj;
           // Log.i(TAG, "handleMessage: "+json);
            Gson gson = new Gson();
            ProductBean productBean = gson.fromJson(json , ProductBean.class);
            myCallBack.success(productBean);
        }
    };
    public void getRequest(){
        OkhttpUtil util=OkhttpUtil.getInstance();
        util.doGet(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json=response.body().string();
                Message message=new Message();
                message.obj=json;
                mhandler.sendMessage(message);
            }
        });
    }

        public void setMyCallBack(MyCallBack myCallBack){
        this.myCallBack=myCallBack;
    }
    public interface MyCallBack{
        public  void success(Object obj);

    }
}











//适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

    List<ProductBean.ProductData.Product>list;
    int type=-1;
    Context mContext;
    public MyAdapter(List<ProductBean.ProductData.Product> list, Context context) {
        this.list = list;
        this.mContext=context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=null;
        if (type==1){
            view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_1_layout,null);
        }else {
            view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_2_layout,null);
        }
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {

        if (type==1){
            String title=list.get(i).title;
            String imagUrl="http://365jia.cn/uploads/"+list.get(i).pics.get(0);
            if (holder!=null&&holder.title_1!=null){
                holder.title_1.setText(title);
                Glide.with(mContext).load(imagUrl).into(holder.image_1);
            }

        }else {
            String title=list.get(i).title;
            String imagUrl="http://365jia.cn/uploads/"+list.get(i).pics_new.get(0);
            if (holder!=null&&holder.title_2!=null){
                holder.title_2.setText(title);
                Glide.with(mContext).load(imagUrl).into(holder.image_2);
            }

        }
    }

    @Override
    public int getItemCount() {
        Log.e("tag" ,""+list.size());
        if (list!=null){
            return  list.size();
        }
        return 0;
    }
    //重写条目类型的方法


    @Override
    public int getItemViewType(int position) {
        List<String>plist=list.get(position).pics_new;
        if (plist==null||plist.size()==0){
            type=1;
        }else {
            type=2;
        }
        return super.getItemViewType(position);

    }

    public class Holder extends RecyclerView.ViewHolder{

        public TextView title_1,title_2;
        public ImageView image_1,image_2;
        public Holder(@NonNull View itemView) {
            super(itemView);
            if (type==1){
                title_1=itemView.findViewById(R.id.title_1_id);
                image_1=itemView.findViewById(R.id.imag_1_id);
            }else {
                title_2=itemView.findViewById(R.id.title_2_id);
                image_2=itemView.findViewById(R.id.imag_2_id);
            }
        }
    }
}








//bean类

public class ProductBean {
    public  String httpStatusCode;
    public  String code;
    public ProductData data;
    public static  class ProductData{
        public String page;
        public String perpage;
        public String max_page;
        public String total;
        public List<Product>data;
        public static class Product {
            public String id;
            public String title;
            public String link;
            public String t;
            public String type;
            public String alias;
            public String pic_amount;
            public String style;
            public String comment_amount;
            public String source;
            public String type_sign;
            public String inner_news;
            public String views;
            public String comment_amount_label;
            public String views_label;
            public List<String>pics;
            public List<String>pics_new;
        }


    }
}








//P层

public class MyPresenter<T> implements ContarctIntface.PresenterIntface {
    //声明model对象
    MyModel myModel;
    T tt;
    //声明view接口对象
    ContarctIntface.ProductIntface productIntface;

    public MyPresenter( T t){
         this.tt=t;
         myModel=new MyModel();
    }
    @Override
    public void toProduct() {
        myModel.setMyCallBack(new MyModel.MyCallBack() {
            @Override
            public void success(Object obj) {
               ContarctIntface.ProductIntface productIntface=(ContarctIntface.ProductIntface) tt;
               productIntface.ShowDisplay(obj);
            }
        });
        myModel.getRequest();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值