package com.bawei.mvp.view;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:07
*/
public interface MainView {
void sueccss(int type ,String data);
void fail(int type ,String error);
}
package com.bawei.mvp.model;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:08
*/
public interface MainModel {
interface CallBackLiseter{
void sueccss(int type ,String data);
void fail(int type ,String error);
}
void doBennr(int type,String url,CallBackLiseter callBackLiseter);
void doData(int type,String url,CallBackLiseter callBackLiseter);
}
package com.bawei.mvp.model;
import com.bawei.mvp.net.HttpUtils;
import java.util.HashMap;
import java.util.Map;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:10
*/
public class MainModelIml implements MainModel {
@Override
public void doBennr(int type, String url, CallBackLiseter callBackLiseter) {
doHttp( type, url, callBackLiseter);
}
@Override
public void doData(int type, String url, CallBackLiseter callBackLiseter) {
doHttp( type, url, callBackLiseter);
}
private void doHttp(final int type, String url, final CallBackLiseter callBackLiseter) {
new HttpUtils().get(url).reult(new HttpUtils.HttpListener() {
@Override
public void sueccss(String data) {
callBackLiseter.sueccss(type,data);
}
@Override
public void fail(String error) {
callBackLiseter.fail(type,error);
}
});
}
}
package com.bawei.mvp.perseter;
import com.bawei.mvp.model.MainModel;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:10
*/
public interface Mainp {
void doBennr(int type, String url);
void doData(int type, String url);
}
package com.bawei.mvp.perseter;
import com.bawei.mvp.model.MainModel;
import com.bawei.mvp.view.MainView;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:11
*/
public class MainpIml implements Mainp, MainModel.CallBackLiseter {
private MainModel mainModel;
private MainView mainView;
public MainpIml(MainModel mainModel, MainView mainView){
this.mainModel=mainModel;
this.mainView=mainView;
}
@Override
public void doBennr(int type, String url) {
mainModel.doBennr(type,url,this);
}
@Override
public void doData(int type, String url) {
mainModel.doData(type,url,this);
}
@Override
public void sueccss(int type, String data) {
mainView.sueccss(type,data);
}
@Override
public void fail(int type, String error) {
mainView.fail(type,error);
}
public void destroy(){
if (mainModel!=null){
mainModel=null;
}
if (mainView!=null){
mainView=null;
}
System.gc();
}
}
net
package com.bawei.mvp.net;
import java.util.Map;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:16
*/
public interface HttpSevice {
@GET
Observable<ResponseBody> get(@Url String url);
}
package com.bawei.mvp.net;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:15
*/
public class HttpUtils {
private String base_url="https://mobile.bwstudent.com";
public void setBase_url(String base_url){
this.base_url=base_url;
}
public HttpUtils get(String url){
HttpSevice sevice=getHttpSevice();
Observable<ResponseBody> ob = sevice.get(url);
send(ob);
return this;
}
private void send(Observable<ResponseBody> ob) {
ob.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new ObserverIml<ResponseBody>() {
@Override
public void onNext(ResponseBody responseBody) {
try {
String data= responseBody.string();
httpListener.sueccss(data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable e) {
String error = e.getMessage();
httpListener.fail(error);
}
});
}
private HttpSevice getHttpSevice() {
//添加ok拦截器和缓存
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"");
long size=1024*10;
OkHttpClient client=new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
}).cache(new Cache(file,size)).build();
Retrofit retrofit=new Retrofit.Builder()
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(base_url)
.build();
return retrofit.create(HttpSevice.class);
}
private HttpListener httpListener;
public void reult( HttpListener httpListener){
this.httpListener=httpListener;
}
public interface HttpListener{
void sueccss(String data);
void fail(String error);
}
}
package com.bawei.mvp.net;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:18
*/
public abstract class ObserverIml<T> implements Observer<T> {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public abstract void onNext(T t) ;
@Override
public abstract void onError(Throwable e) ;
@Override
public void onComplete() {
}
}
package com.bawei.bean;
import com.stx.xhb.xbanner.entity.SimpleBannerInfo;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 09:53
*/
public class BanerBean {
/**
* result : [{"imageUrl":"http://172.17.8.100/images/tech/banner/073040514318.jpg","jumpUrl":"wd://information?infoId=1","rank":1,"title":"关于滴滴顺风车事件的几点思考"},{"imageUrl":"http://172.17.8.100/images/tech/banner/205315638398.jpg","jumpUrl":"https://www.huxiu.com/article/266392.html","rank":2,"title":"38个区县大PK:重庆也要东南飞?"},{"imageUrl":"http://172.17.8.100/images/tech/banner/201540980386.jpg","jumpUrl":"https://www.huxiu.com/article/266362.html","rank":3,"title":"千股大跌,他财富一天缩水630亿元,你感受过这种绝望嘛"},{"imageUrl":"http://172.17.8.100/images/tech/banner/145637698331.jpg","jumpUrl":"https://www.huxiu.com/article/266201.html","rank":4,"title":"华为寻找AI"},{"imageUrl":"http://172.17.8.100/images/tech/banner/065326098728.jpg","jumpUrl":"https://www.huxiu.com/article/266398.html","rank":5,"title":"微信\u201c死于\u201d印度"},{"imageUrl":"http://172.17.8.100/images/tech/banner/20181026151647.png","jumpUrl":"http://172.17.8.100/htm/lottery/index.html","rank":5,"title":"抽奖"}]
* message : 查询成功
* status : 0000
*/
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean extends SimpleBannerInfo {
/**
* imageUrl : http://172.17.8.100/images/tech/banner/073040514318.jpg
* jumpUrl : wd://information?infoId=1
* rank : 1
* title : 关于滴滴顺风车事件的几点思考
*/
private String imageUrl;
private String jumpUrl;
private int rank;
private String title;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getJumpUrl() {
return jumpUrl;
}
public void setJumpUrl(String jumpUrl) {
this.jumpUrl = jumpUrl;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public Object getXBannerUrl() {
return null;
}
}
}
package com.bawei.bean;
import java.util.List;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 10:01
*/
public class ShopBean {
private String message;
private String status;
private List<ResultBean> result;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<ResultBean> getResult() {
return result;
}
public void setResult(List<ResultBean> result) {
this.result = result;
}
public static class ResultBean {
private int collection;
private int id;
private long releaseTime;
private int share;
private String source;
private String summary;
private String thumbnail;
private String title;
private int whetherAdvertising;
private int whetherCollection;
private int whetherPay;
private InfoAdvertisingVoBean infoAdvertisingVo;
public int getCollection() {
return collection;
}
public void setCollection(int collection) {
this.collection = collection;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getReleaseTime() {
return releaseTime;
}
public void setReleaseTime(long releaseTime) {
this.releaseTime = releaseTime;
}
public int getShare() {
return share;
}
public void setShare(int share) {
this.share = share;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getWhetherAdvertising() {
return whetherAdvertising;
}
public void setWhetherAdvertising(int whetherAdvertising) {
this.whetherAdvertising = whetherAdvertising;
}
public int getWhetherCollection() {
return whetherCollection;
}
public void setWhetherCollection(int whetherCollection) {
this.whetherCollection = whetherCollection;
}
public int getWhetherPay() {
return whetherPay;
}
public void setWhetherPay(int whetherPay) {
this.whetherPay = whetherPay;
}
public InfoAdvertisingVoBean getInfoAdvertisingVo() {
return infoAdvertisingVo;
}
public void setInfoAdvertisingVo(InfoAdvertisingVoBean infoAdvertisingVo) {
this.infoAdvertisingVo = infoAdvertisingVo;
}
public static class InfoAdvertisingVoBean {
/**
* content : 八维教育
* id : 1
* pic : http://mobile.bwstudent.com/images/tech/ad/bw.png
* url : http://www.bwie.com
*/
private String content;
private int id;
private String pic;
private String url;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
}
package com.bawei.adpter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bawei.R;
import com.bawei.bean.ShopBean;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 10:12
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MhV> {
private Context context;
private List<ShopBean.ResultBean> list = new ArrayList<>();
public MyAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public MhV onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.ban_item, null);
MhV mhV = new MhV(view);
return mhV;
}
@Override
public void onBindViewHolder(@NonNull MhV mhV, int i) {
mhV.simel.setImageURI(list.get(i).getThumbnail());
mhV.text1.setText(list.get(i).getTitle());
mhV.text2.setText(list.get(i).getSummary());
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<ShopBean.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
public class MhV extends RecyclerView.ViewHolder {
@BindView(R.id.simel)
SimpleDraweeView simel;
@BindView(R.id.text1)
TextView text1;
@BindView(R.id.text2)
TextView text2;
public MhV(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
package com.bawei;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.bawei.adpter.MyAdapter;
import com.bawei.bean.BanerBean;
import com.bawei.bean.ShopBean;
import com.bawei.mvp.model.MainModelIml;
import com.bawei.mvp.perseter.MainpIml;
import com.bawei.mvp.view.MainView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.stx.xhb.xbanner.XBanner;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements MainView {
@BindView(R.id.mXBanner)
XBanner mXBanner;
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
private MainpIml mainpIml;
private MyAdapter myAdapter;
private int page=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
myAdapter = new MyAdapter(this);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(myAdapter);
mainpIml = new MainpIml(new MainModelIml(), this);
mainpIml.doBennr(0,"/techApi/information/v1/bannerShow");
mainpIml.doData(1,"/techApi/information/v1/infoRecommendList?count=5&page="+page+"");
}
@Override
public void sueccss(int type, String data) {
if (type==0){
final BanerBean bean = new Gson().fromJson(data, BanerBean.class);
mXBanner.setBannerData(R.layout.frsco,bean.getResult());
mXBanner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
BanerBean.ResultBean b= (BanerBean.ResultBean) model;
((SimpleDraweeView) view).setImageURI(b.getImageUrl());
}
});
/* //点击事件
mXBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
@Override
public void onItemClick(XBanner banner, Object model, View view, int position) {
Toast.makeText(MainActivity.this,"dhhdhh",Toast.LENGTH_LONG).show();
}
});*/
}else if(type==1){
ShopBean bean = new Gson().fromJson(data, ShopBean.class);
myAdapter.setList(bean.getResult());
}
}
@Override
public void fail(int type, String error) {
mainpIml.fail(type,error);
}
@Override
protected void onDestroy() {
super.onDestroy();
mainpIml.destroy();
}
}
<?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.stx.xhb.xbanner.XBanner
android:id="@+id/mXBanner"
android:layout_width="match_parent"
android:layout_height="300dp"
app:isClipChildrenMode="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/simel"
android:layout_width="100dp"
android:layout_height="100dp"
app:roundedCornerRadius="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<com.facebook.drawee.view.SimpleDraweeView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:roundedCornerRadius="20dp">
</com.facebook.drawee.view.SimpleDraweeView>
package com.bawei;
import android.app.Application;
import android.os.Environment;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.cache.disk.DiskStorage;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import java.io.File;
/**
* 作者:$yangxiangrong
* <p>
* 2019/4/22 10:23
*/
public class MyAPP extends Application {
@Override
public void onCreate() {
super.onCreate();
//Fresco.initialize(this);
Fresco.initialize(this, ImagePipelineConfig.newBuilder(MyAPP .this)
.setMainDiskCacheConfig(
DiskCacheConfig.newBuilder(this)
.setBaseDirectoryPath(new File(Environment.getExternalStorageDirectory().getAbsolutePath()))
.setMaxCacheSize(10*1024*1024)//缓存大小
.build()
)
.build()
);
}
}