MVP XListView 상 당 김 효과 보 여주 기

본 고의 실례 는 여러분 에 게 MVP XListView 에서 끌 어 내리 기 전 시 된 구체 적 인 코드 를 공 유 했 습 니 다.여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
추출 기류

package com.gs.gg.day8.back;
 
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
 
public abstract class BackActivity extends AppCompatActivity {
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  init();
 }
 
 protected abstract int getLayout();
 protected abstract void initViews();
 protected abstract void setOnClick();
 protected abstract void processLogic();
 void init(){
  if (getLayout()!=0){
   setContentView(getLayout());
   initViews();
   setOnClick();
   processLogic();
  }else {
   throw new IllegalStateException("       ");
  }
 }
}
Model

package com.gs.gg.day8.model;
 
import com.gs.gg.day8.callbase.MyCallBase;
 
public interface Model {
 void getData(String mUrl,String parants,MyCallBase callBase);
}
MyCallBase

package com.gs.gg.day8.callbase;
 
import com.gs.gg.day8.bean.User;
 
public interface MyCallBase {
 void setData(User data);
 void setError(String error);
}
Modelimpl

package com.gs.gg.day8.model;
 
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
 
import com.google.gson.Gson;
import com.gs.gg.day8.R;
import com.gs.gg.day8.bean.User;
import com.gs.gg.day8.callbase.MyCallBase;
import com.gs.gg.day8.util.HttpUtils;
 
import java.net.URL;
 
public class Modelimpl implements Model {
 private Handler handler=new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   String JsonStr= (String) msg.obj;
   Gson gson=new Gson();
   User user= gson.fromJson(JsonStr,User.class);
   callBase.setData(user);
  }
 };
 private MyCallBase callBase;
 @Override
 public void getData(final String mUrl, String parants, final MyCallBase callBase) {
  this.callBase=callBase;
  new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     String json = HttpUtils.get(mUrl);
     handler.sendMessage(handler.obtainMessage(0,json));
    } catch (Exception e) {
     Looper.prepare();
     callBase.setError("  ");
     Looper.loop();
    }
   }
  }).start();
 }
}
HttpUtils

package com.gs.gg.day8.util;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class HttpUtils {
 public static String get(String mUrl) throws Exception {
  URL url=new URL(mUrl);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  //      
  connection.setRequestMethod("GET");
  connection.setConnectTimeout(5000);
  InputStream stream = connection.getInputStream();
  String inputStr = getInputStr(stream);
  return inputStr;
 }
 
 //       
 private static String getInputStr(InputStream stream) throws IOException {
  BufferedReader br = new BufferedReader(new InputStreamReader(stream));
  StringBuffer sb = new StringBuffer();
  String str = null;
  while ((str = br.readLine()) != null) {
   sb.append(str);
  }
  return sb.toString();
 }
}
IView

package com.gs.gg.day8.view;
 
import com.gs.gg.day8.bean.User;
 
public interface IView {
 void success(User data);
 void error(String error);
}
Presenter

package com.gs.gg.day8.presenter;
 
public interface Presenter {
 void startReponse(String url,String parants);
}
PresenterImpl

package com.gs.gg.day8.presenter;
 
import com.gs.gg.day8.bean.User;
import com.gs.gg.day8.callbase.MyCallBase;
import com.gs.gg.day8.model.Modelimpl;
import com.gs.gg.day8.view.IView;
 
public class PresenterImpl implements Presenter {
 private IView iView;
 private Modelimpl model;
 
 public PresenterImpl(IView iView) {
  this.iView = iView;
  model = new Modelimpl();
 }
 
 @Override
 public void startReponse(String mUrl, String parants) {
  model.getData(mUrl,parants, new MyCallBase() {
   @Override
   public void setData(User data) {
    iView.success(data);
   }
 
   @Override
   public void setError(String error) {
    iView.error(error);
   }
  });
 }
 public void onDotch(){
  if (iView!=null){
   iView=null;
  }
  if (model!=null){
   model=null;
  }
 }
}
MainActivity

package com.gs.gg.day8;
 
import android.widget.Toast;
 
import com.gs.gg.day8.adapter.MyAdapter;
import com.gs.gg.day8.back.BackActivity;
import com.gs.gg.day8.bean.User;
import com.gs.gg.day8.presenter.PresenterImpl;
import com.gs.gg.day8.view.IView;
import com.gs.gg.myxlist.me.maxwin.view.XListView;
 
import java.util.ArrayList;
 
public class MainActivity extends BackActivity implements IView,XListView.IXListViewListener {
 
 private XListView xlist_item;
 private PresenterImpl presenter;
 private ArrayList<User.DataBean> mlist=new ArrayList<>();
 private String muUrl="http://www.xieast.com/api/news/news.php?page=";
 private int i=1;
 private MyAdapter adapter;
 
 @Override
 protected int getLayout() {
  return R.layout.activity_main;
 }
 
 @Override
 protected void initViews() {
  xlist_item = findViewById(R.id.Xlist_item);
 }
 
 @Override
 protected void setOnClick() {
  xlist_item.setPullLoadEnable(true);
  xlist_item.setXListViewListener(this);
 }
 
 @Override
 protected void processLogic() {
  presenter = new PresenterImpl(this);
  adapter = new MyAdapter(MainActivity.this,mlist);
  xlist_item.setAdapter(adapter);
  presenter.startReponse(muUrl+i, null);
 }
 
 @Override
 public void success(User data) {
  mlist.addAll(data.getData());
  adapter.notifyDataSetChanged();
  stop();
 }
 
 @Override
 public void error(String error) {
  Toast.makeText(this, "  ", Toast.LENGTH_SHORT).show();
 }
 private void stop() {
  xlist_item.stopRefresh();
  xlist_item.stopLoadMore();
 }
 
 @Override
 public void onRefresh() {
  i = 1;
  mlist.clear();
  presenter.startReponse(muUrl + i, null);
 }
 
 @Override
 public void onLoadMore() {
  i++;
  presenter.startReponse(muUrl + i, null);
 }
}
MyAdapter

package com.gs.gg.day8.adapter;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.gs.gg.day8.R;
import com.gs.gg.day8.bean.User;
import com.nostra13.universalimageloader.core.ImageLoader;
 
import java.util.ArrayList;
 
public class MyAdapter extends BaseAdapter {
 public MyAdapter(Context context, ArrayList<User.DataBean> mlist) {
  this.context = context;
  this.mlist = mlist;
 }
 
 private Context context;
 private ArrayList<User.DataBean> mlist;
 
 @Override
 public int getCount() {
  return mlist.size();
 }
 
 @Override
 public Object getItem(int position) {
  return mlist.get(position);
 }
 
 @Override
 public long getItemId(int position) {
  return position;
 }
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  HolderView holder=null;
  if (convertView==null){
   convertView= View.inflate(context, R.layout.item,null);
   holder=new HolderView();
   holder.textName=convertView.findViewById(R.id.text);
   holder.imageName=convertView.findViewById(R.id.image);
   convertView.setTag(holder);
  }else {
   holder= (HolderView) convertView.getTag();
  }
  User.DataBean bean = mlist.get(position);
  holder.textName.setText(bean.getTitle());
  ImageLoader.getInstance().displayImage(mlist.get(position).getThumbnail_pic_s(),holder.imageName);
  return convertView;
 }
 class HolderView{
  TextView textName;
  ImageView imageName;
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기