mvp模式实现列表

MainActivity:

 

package com.example.monizk2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ScrollView;

import com.example.monizk2.adapter.ShowAdapter;
import com.example.monizk2.bean.ShowBean;
import com.example.monizk2.presenter.ShowPresenterimpl;
import com.example.monizk2.view.MainView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements MainView{

    private PullToRefreshScrollView pull_listview;
    private RecyclerView recycle;
    private ShowPresenterimpl showPresenterimpl;
    private int page=1;
    private List data;
    private ShowAdapter adapter;

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

    private void init() {
        //初始化控件
        pull_listview = findViewById(R.id.pull_listview);
        recycle = findViewById(R.id.recycler);
        recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        showPresenterimpl = new ShowPresenterimpl(this);
        showPresenterimpl.getData(page);
        pull_listview.setMode(PullToRefreshBase.Mode.BOTH);
        pull_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                page=1;
                showPresenterimpl.getData(page);
                pull_listview.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                  page++;
                  showPresenterimpl.getData(page);
                  pull_listview.onRefreshComplete();
            }
        });
    }

    @Override
    public void onSuccess(final ShowBean showBean, int page) {
        data = showBean.getData();
        if(data!=null){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter = new ShowAdapter(MainActivity.this,data);
                    Log.e("ddd", "run: " + showBean.toString() );
                    recycle.setAdapter(adapter);
                    pull_listview.onRefreshComplete();
                }
            });
        }
    }

    @Override
    public void onError(int code) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        showPresenterimpl.onDestroys();
    }
}


view接口:

  
package com.example.monizk2.view;

import com.example.monizk2.bean.ShowBean;


public interface MainView {

    void onSuccess(ShowBean showBean, int page);
    void onError(int code);

}
 Model接口:

package com.example.monizk2.model;

import com.example.monizk2.bean.ShowBean;


public interface ShowModel {
    //成功
    void onSuccessData(ShowBean sbean,int page);
    //失败
    void onErrorData(int code);
}
Model实现类:
package com.example.monizk2.model;

import android.util.Log;

import com.example.monizk2.bean.ShowBean;
import com.google.gson.Gson;

import java.io.IOException;

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

public class ShowModelimpl {
   private String api="https://www.zhaoapi.cn/product/getProducts?pscid=";
    public void getdata(final ShowModel showOncilter, final int page){
        //创建okhttp
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                .build();
        //创建request
        Request request = new Request
                .Builder()
                .url(api+page)
                .build();
        //执行
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                showOncilter.onErrorData(1);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Log.i("aaa",string.toString());
                Gson gson=new Gson();
                ShowBean showBean = gson.fromJson(string, ShowBean.class);
                showOncilter.onSuccessData(showBean,page);
                Log.i("aaa",showBean.getData().get(0).getTitle());
            }
        });
    }
}

presenter接口:
   
package com.example.monizk2.presenter;


public interface ShowPresenter {
    void onDestroys();
}
Presenter实现类:

package com.example.monizk2.presenter;

import com.example.monizk2.MainActivity;
import com.example.monizk2.bean.ShowBean;
import com.example.monizk2.model.ShowModel;
import com.example.monizk2.model.ShowModelimpl;
import com.example.monizk2.view.MainView;


public class ShowPresenterimpl implements ShowPresenter{

    private MainView mainView;
    private final ShowModelimpl showModelimpl;

    public ShowPresenterimpl(MainView mainView) {
        this.mainView=mainView;
        this.showModelimpl = new ShowModelimpl();

    }
    public void getData(int page){
        showModelimpl.getdata(new ShowModel() {
            @Override
            public void onSuccessData(ShowBean sbean, int page) {
                mainView.onSuccess(sbean,page);
            }

            @Override
            public void onErrorData(int code) {
                mainView.onError(1);
            }
        },page);
    }
    @Override
    public void onDestroys() {
        if(mainView!=null){
            mainView=null;
        }
    }
}
Adapter

package com.example.monizk2.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.monizk2.R;
import com.example.monizk2.bean.ShowBean;

import java.util.List;

public class ShowAdapter extends RecyclerView.Adapter{

    private Context context;
    private List dataBeanList;

    public ShowAdapter(Context context, List dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @NonNull
    @Override
    public TextHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TextHolder(LayoutInflater.from(context).inflate(R.layout.showlayout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull TextHolder holder, int position) {
     //绑定数据
       holder.price.setText("优惠价:"+dataBeanList.get(position).getPrice());
       holder.title.setText(dataBeanList.get(position).getTitle());
       String path = dataBeanList.get(position).getImages().split("\\|")[0];
        Glide.with(context).load(path).into(holder.ion);
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }
    class TextHolder extends RecyclerView.ViewHolder{

        private TextView title;
        private ImageView ion;
        private  TextView price;

        public TextHolder(View itemView) {
            super(itemView);
            ion = itemView.findViewById(R.id.ion);
            title = itemView.findViewById(R.id.tv_title);
            price = itemView.findViewById(R.id.price);
        }
    }
}




你可能感兴趣的:(mvp模式实现列表)