使用MVPXlistView怎么实现展示上拉下拉效果-创新互联
使用MVPXlistView怎么实现展示上拉下拉效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联公司2013年至今,先为宜川等服务建站,宜川等地企业,进行企业商务咨询服务。为宜川企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。抽基类
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; } }
关于使用MVPXlistView怎么实现展示上拉下拉效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
分享名称:使用MVPXlistView怎么实现展示上拉下拉效果-创新互联
链接地址:http://scgulin.cn/article/hicds.html