效果图
然后,直接上代码吧
首先适配器的
package soexample.umeng.com.mvp.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import soexample.umeng.com.mvp.R;
import soexample.umeng.com.mvp.bean.MyData;
public class MyAdapter extends BaseAdapter {
private ArrayList<MyData.DataBean> datas;
private Context context;
public MyAdapter(ArrayList<MyData.DataBean> datas, Context context) {
this.datas = datas;
this.context = context;
}
public ArrayList<MyData.DataBean> getDatas() {
return datas;
}
public void setDatas(ArrayList<MyData.DataBean> datas) {
this.datas = datas;
notifyDataSetChanged();
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//holder类
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.ceshi, null);
holder = new ViewHolder();
holder.tv_title = convertView.findViewById(R.id.haha);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_title.setText(datas.get(position).getTitle());
return convertView;
}
class ViewHolder {
TextView tv_title;
}
}
base抽取基类
package soexample.umeng.com.mvp.base;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
protected abstract int setLayout();
protected abstract void initviews();
protected abstract void setOnClick();
protected abstract void proLogic();
void init() {
if (setLayout() != 0) {
setContentView(setLayout());
initviews();
setOnClick();
proLogic();
}
}
}
bean包
package soexample.umeng.com.mvp.bean;
import java.util.List;
public class MyData {
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private int aid;
private String createtime;
private String icon;
private Object productId;
private String title;
private int type;
private String url;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Object getProductId() {
return productId;
}
public void setProductId(Object productId) {
this.productId = productId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
接口回调
package soexample.umeng.com.mvp.callback;
public interface MyCallBack<T> {
//成功时回调
void setData(T data);
//失败时回调
void setError(T error);
}
M层的接口
package soexample.umeng.com.mvp.moudle;
import soexample.umeng.com.mvp.callback.MyCallBack;
public interface MyMoudle {
void startRespon(String url, MyCallBack callBack);
}
M层的实现类
package soexample.umeng.com.mvp.moudle;
import android.os.Handler;
import android.os.Message;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import soexample.umeng.com.mvp.bean.MyData;
import soexample.umeng.com.mvp.callback.MyCallBack;
import soexample.umeng.com.mvp.util.OKHttpUtil;
public class MyCallBckImpl implements MyMoudle {
private MyCallBack myCallBack;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsons = (String) msg.obj;
Gson gson = new Gson();
MyData myData = gson.fromJson(jsons, MyData.class);
myCallBack.setData(myData.getData());
}
};
@Override
public void startRespon(String url, MyCallBack mycallBack) {
this.myCallBack =mycallBack;
OKHttpUtil.postOkHttpUtil().asyncget(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
myCallBack.setError(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
handler.sendMessage(handler.obtainMessage(0,response.body().string()));
}
});
}
}
V层
package soexample.umeng.com.mvp.iview;
public interface IView<T> {
void success(T data);
void error(T error);
}
P层的接口
package soexample.umeng.com.mvp.ipresenter;
public interface IPresenter {
void startRequest(String url);
}
P层的实现类
package soexample.umeng.com.mvp.ipresenter;
import soexample.umeng.com.mvp.callback.MyCallBack;
import soexample.umeng.com.mvp.iview.IView;
import soexample.umeng.com.mvp.moudle.MyCallBckImpl;
public class IPersenterImpl implements IPresenter {
private IView iView;
private MyCallBckImpl myMoudle;
public IPersenterImpl(IView iView) {
this.iView = iView;
myMoudle = new MyCallBckImpl();
}
@Override
public void startRequest(final String url) {
myMoudle.startRespon(url, new MyCallBack() {
@Override
public void setData(Object data) {
iView.success(data);
}
@Override
public void setError(Object error) {
iView.error(error);
}
});
}
//防止内存泄漏
public void onDetecth() {
if (myMoudle != null) {
myMoudle = null;
}
if (iView != null) {
iView = null;
}
}
}
OKHttp的解析工具类,这里使用的是异步get解析
package soexample.umeng.com.mvp.util;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
public class OKHttpUtil {
private final OkHttpClient okHttpClient;
//首先创建一个okhttp对象
public OKHttpUtil() {
okHttpClient = new OkHttpClient();
}
//创建一个实现方法
public static OKHttpUtil postOkHttpUtil() {
return OKHolder.httputil;
}
//创建一个holder类
static class OKHolder {
private static final OKHttpUtil httputil = new OKHttpUtil();
}
public void asyncget(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
Mainactivity的
package soexample.umeng.com.mvp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.recker.flybanner.FlyBanner;
import java.util.ArrayList;
import soexample.umeng.com.mvp.adapter.MyAdapter;
import soexample.umeng.com.mvp.base.BaseActivity;
import soexample.umeng.com.mvp.bean.MyData;
import soexample.umeng.com.mvp.ipresenter.IPersenterImpl;
import soexample.umeng.com.mvp.iview.IView;
public class MainActivity<T> extends BaseActivity implements IView<T> {
private FlyBanner fly;
private ListView lv;
private ArrayList<MyData.DataBean> title
= new ArrayList<>();
private String murl = "http://www.zhaoapi.cn/ad/getAd";
private ArrayList<String> datas = new ArrayList<>();
private String[] img = {"http://www.zhaoapi.cn//images//quarter//ad1.png",
"http://www.zhaoapi.cn//images//quarter//ad2.png",
"http://www.zhaoapi.cn//images//quarter//ad3.png",
"http://www.zhaoapi.cn//images//quarter//ad4.png"};
private MyAdapter adapter;
private IPersenterImpl persenter;
@Override
protected int setLayout() {
return R.layout.activity_main;
}
@Override
protected void initviews() {
fly = findViewById(R.id.HttpBanner);
lv = findViewById(R.id.LV);
initData();
}
private void initData() {
for (int i = 0; i < img.length; i++) {
datas.add(img[i]);
}
fly.setImagesUrl(datas);
}
@Override
protected void setOnClick() {
}
@Override
protected void proLogic() {
persenter = new IPersenterImpl(this);
persenter.startRequest(murl);
}
@Override
public void onClick(View v) {
}
@Override
public void success(T data) {
title = (ArrayList<MyData.DataBean>) data;
adapter = new MyAdapter(title, this);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void error(T error) {
}
}
xml的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.recker.flybanner.FlyBanner
android:id="@+id/HttpBanner"
android:layout_width="match_parent"
android:layout_height="200dp"></com.recker.flybanner.FlyBanner>
<ListView
android:id="@+id/LV"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
需要用到的jar包
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.recker.flybanner:flybanner:1.3'
以上,就这么多吧.