欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

MVPXlistView展示上拉下拉效果

程序员文章站 2022-04-30 22:43:50
本文实例为大家分享了mvpxlistview上拉下拉展示的具体代码,供大家参考,具体内容如下 抽基类 package com.gs.gg.day8.back;...

本文实例为大家分享了mvpxlistview上拉下拉展示的具体代码,供大家参考,具体内容如下

抽基类

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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。