view层
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import com.bwei.mvprecycleviewokhttplogin.adapter.MyAdapter; import com.bwei.mvprecycleviewokhttplogin.bean.Bean; import com.bwei.mvprecycleviewokhttplogin.viewlistener.ViewListener; import com.liaoinstan.springview.widget.SpringView; public class MainActivity extends AppCompatActivity implements ViewListener{ private RecyclerView recycel; private SpringView spring; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 recycel = (RecyclerView) findViewById(R.id.recycle); spring = (SpringView) findViewById(R.id.springview); //recycle做适配 myAdapter = new MyAdapter(this); recycel.setAdapter(myAdapter); } @Override public void backsuccess(final Bean bean) { //调用适配好的方法======注意线程操作 runOnUiThread(new Runnable() { @Override public void run() { myAdapter.refreshDate(bean); } }); } @Override public void backfaile(int code) { } }
view 接口
public interface ViewListener { //成功的方法 public void backsuccess(Bean bean); //失败的方法 public void backfaile(int code); }
Presenter层
import com.bwei.mvprecycleviewokhttplogin.bean.Bean; import com.bwei.mvprecycleviewokhttplogin.modellistener.ModelListener; import com.bwei.mvprecycleviewokhttplogin.viewlistener.ViewListener; import model.Modelmain; /** * 陈敏慧 * Created by d on 2017/10/14. */ public class Presentermain { ViewListener viewListener; Modelmain modelmain; public Presentermain(ViewListener viewListener) { this.viewListener = viewListener; this.modelmain=new Modelmain(); } public void getdate(boolean up){ modelmain.getDate(up, new ModelListener() { @Override public void success(Bean bean) { viewListener.backsuccess(bean); } @Override public void faile(int code) { viewListener.backfaile(code); } }); } }
Model层
public class Modelmain { //获得网络请求 public void getDate(boolean up, final ModelListener modelListener){ OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://qhb.2dyt.com/Bwei/news?type=9&postkey=109rff1d1AK").build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { //调用model监听里的失败方法 modelListener.faile(1); } @Override public void onResponse(Call call, Response response) throws IOException { String s = response.body().string(); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); //调用midel监听里的成功方法 modelListener.success(bean); } }); } }
model接口
public interface ModelListener { public void success(Bean bean); public void faile(int code); }