本文实例为大家分享了MVPXlistView上拉下拉展示的具体代码,供大家参考,具体内容如下
抽基类
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制package com.gs.gg.day8.presenter;
public interface Presenter {
void startReponse(String url,String parants);
}
PresenterImpl
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。