MVPXlistView展示上拉下拉效果
本文实例为大家分享了MVPXlistView上拉下拉展示的具体代码,供大家参考,具体内容如下
抽基类
packagecom.gs.gg.day8.back; importandroid.os.Bundle; importandroid.support.annotation.Nullable; importandroid.support.v7.app.AppCompatActivity; publicabstractclassBackActivityextendsAppCompatActivity{ @Override protectedvoidonCreate(@NullableBundlesavedInstanceState){ super.onCreate(savedInstanceState); init(); } protectedabstractintgetLayout(); protectedabstractvoidinitViews(); protectedabstractvoidsetOnClick(); protectedabstractvoidprocessLogic(); voidinit(){ if(getLayout()!=0){ setContentView(getLayout()); initViews(); setOnClick(); processLogic(); }else{ thrownewIllegalStateException("请加载布局文件"); } } }
Model
packagecom.gs.gg.day8.model; importcom.gs.gg.day8.callbase.MyCallBase; publicinterfaceModel{ voidgetData(StringmUrl,Stringparants,MyCallBasecallBase); }
MyCallBase
packagecom.gs.gg.day8.callbase; importcom.gs.gg.day8.bean.User; publicinterfaceMyCallBase{ voidsetData(Userdata); voidsetError(Stringerror); }
Modelimpl
packagecom.gs.gg.day8.model; importandroid.os.Handler; importandroid.os.Looper; importandroid.os.Message; importcom.google.gson.Gson; importcom.gs.gg.day8.R; importcom.gs.gg.day8.bean.User; importcom.gs.gg.day8.callbase.MyCallBase; importcom.gs.gg.day8.util.HttpUtils; importjava.net.URL; publicclassModelimplimplementsModel{ privateHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); StringJsonStr=(String)msg.obj; Gsongson=newGson(); Useruser=gson.fromJson(JsonStr,User.class); callBase.setData(user); } }; privateMyCallBasecallBase; @Override publicvoidgetData(finalStringmUrl,Stringparants,finalMyCallBasecallBase){ this.callBase=callBase; newThread(newRunnable(){ @Override publicvoidrun(){ try{ Stringjson=HttpUtils.get(mUrl); handler.sendMessage(handler.obtainMessage(0,json)); }catch(Exceptione){ Looper.prepare(); callBase.setError("异常"); Looper.loop(); } } }).start(); } }
HttpUtils
packagecom.gs.gg.day8.util; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.net.HttpURLConnection; importjava.net.URL; publicclassHttpUtils{ publicstaticStringget(StringmUrl)throwsException{ URLurl=newURL(mUrl); HttpURLConnectionconnection=(HttpURLConnection)url.openConnection(); //设置请求方式 connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); InputStreamstream=connection.getInputStream(); StringinputStr=getInputStr(stream); returninputStr; } //把流转成字符串 privatestaticStringgetInputStr(InputStreamstream)throwsIOException{ BufferedReaderbr=newBufferedReader(newInputStreamReader(stream)); StringBuffersb=newStringBuffer(); Stringstr=null; while((str=br.readLine())!=null){ sb.append(str); } returnsb.toString(); } }
IView
packagecom.gs.gg.day8.view; importcom.gs.gg.day8.bean.User; publicinterfaceIView{ voidsuccess(Userdata); voiderror(Stringerror); }
Presenter
packagecom.gs.gg.day8.presenter; publicinterfacePresenter{ voidstartReponse(Stringurl,Stringparants); }
PresenterImpl
packagecom.gs.gg.day8.presenter; importcom.gs.gg.day8.bean.User; importcom.gs.gg.day8.callbase.MyCallBase; importcom.gs.gg.day8.model.Modelimpl; importcom.gs.gg.day8.view.IView; publicclassPresenterImplimplementsPresenter{ privateIViewiView; privateModelimplmodel; publicPresenterImpl(IViewiView){ this.iView=iView; model=newModelimpl(); } @Override publicvoidstartReponse(StringmUrl,Stringparants){ model.getData(mUrl,parants,newMyCallBase(){ @Override publicvoidsetData(Userdata){ iView.success(data); } @Override publicvoidsetError(Stringerror){ iView.error(error); } }); } publicvoidonDotch(){ if(iView!=null){ iView=null; } if(model!=null){ model=null; } } }
MainActivity
packagecom.gs.gg.day8; importandroid.widget.Toast; importcom.gs.gg.day8.adapter.MyAdapter; importcom.gs.gg.day8.back.BackActivity; importcom.gs.gg.day8.bean.User; importcom.gs.gg.day8.presenter.PresenterImpl; importcom.gs.gg.day8.view.IView; importcom.gs.gg.myxlist.me.maxwin.view.XListView; importjava.util.ArrayList; publicclassMainActivityextendsBackActivityimplementsIView,XListView.IXListViewListener{ privateXListViewxlist_item; privatePresenterImplpresenter; privateArrayListmlist=newArrayList<>(); privateStringmuUrl="http://www.xieast.com/api/news/news.php?page="; privateinti=1; privateMyAdapteradapter; @Override protectedintgetLayout(){ returnR.layout.activity_main; } @Override protectedvoidinitViews(){ xlist_item=findViewById(R.id.Xlist_item); } @Override protectedvoidsetOnClick(){ xlist_item.setPullLoadEnable(true); xlist_item.setXListViewListener(this); } @Override protectedvoidprocessLogic(){ presenter=newPresenterImpl(this); adapter=newMyAdapter(MainActivity.this,mlist); xlist_item.setAdapter(adapter); presenter.startReponse(muUrl+i,null); } @Override publicvoidsuccess(Userdata){ mlist.addAll(data.getData()); adapter.notifyDataSetChanged(); stop(); } @Override publicvoiderror(Stringerror){ Toast.makeText(this,"异常",Toast.LENGTH_SHORT).show(); } privatevoidstop(){ xlist_item.stopRefresh(); xlist_item.stopLoadMore(); } @Override publicvoidonRefresh(){ i=1; mlist.clear(); presenter.startReponse(muUrl+i,null); } @Override publicvoidonLoadMore(){ i++; presenter.startReponse(muUrl+i,null); } }
MyAdapter
packagecom.gs.gg.day8.adapter; importandroid.content.Context; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.BaseAdapter; importandroid.widget.ImageView; importandroid.widget.TextView; importcom.gs.gg.day8.R; importcom.gs.gg.day8.bean.User; importcom.nostra13.universalimageloader.core.ImageLoader; importjava.util.ArrayList; publicclassMyAdapterextendsBaseAdapter{ publicMyAdapter(Contextcontext,ArrayListmlist){ this.context=context; this.mlist=mlist; } privateContextcontext; privateArrayList mlist; @Override publicintgetCount(){ returnmlist.size(); } @Override publicObjectgetItem(intposition){ returnmlist.get(position); } @Override publiclonggetItemId(intposition){ returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ HolderViewholder=null; if(convertView==null){ convertView=View.inflate(context,R.layout.item,null); holder=newHolderView(); holder.textName=convertView.findViewById(R.id.text); holder.imageName=convertView.findViewById(R.id.image); convertView.setTag(holder); }else{ holder=(HolderView)convertView.getTag(); } User.DataBeanbean=mlist.get(position); holder.textName.setText(bean.getTitle()); ImageLoader.getInstance().displayImage(mlist.get(position).getThumbnail_pic_s(),holder.imageName); returnconvertView; } classHolderView{ TextViewtextName; ImageViewimageName; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。