最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
MVPXlistView展示上拉下拉效果
时间:2022-06-25 22:46:15 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了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 ArrayListmlist=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, ArrayListmlist) { this.context = context; this.mlist = mlist; } private Context context; private ArrayList 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; } }
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22