一 。
public class MainActivity extends AppCompatActivity implements CollectionInterFace.MyViewInterFace {
@BindView(R.id.banner)
Banner banner;
@BindView(R.id.recycler1)
RecyclerView recycler1;
@BindView(R.id.recycler2)
RecyclerView recycler2;
@BindView(R.id.recycler3)
RecyclerView recycler3;
List<Product.ResultBean.RxxpBean.CommodityListBean> list=new ArrayList<> ();
List<Product.ResultBean.MlssBean.CommodityListBeanXX> mlist=new ArrayList<Product.ResultBean.MlssBean.CommodityListBeanXX> ();
List<Product.ResultBean.PzshBean.CommodityListBeanX> glist=new ArrayList<Product.ResultBean.PzshBean.CommodityListBeanX> ();
private MyAdapter myAdapter;
private GyAdapter gyAdapter;
private PyAdapter pyAdapter;
Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
ButterKnife.bind (this);
CollectionInterFace.PresenterInterFace presenterInterFace=new Presenter (this);
LinearLayoutManager layoutManager = new LinearLayoutManager (this);
layoutManager.setOrientation (LinearLayoutManager.VERTICAL);
recycler1.setLayoutManager (layoutManager);
LinearLayoutManager layoutManager_2 = new LinearLayoutManager (this);
layoutManager_2.setOrientation (LinearLayoutManager.VERTICAL);
recycler2.setLayoutManager (layoutManager_2);
LinearLayoutManager layoutManager_3 = new LinearLayoutManager (this);
layoutManager_3.setOrientation (LinearLayoutManager.VERTICAL);
recycler3.setLayoutManager (layoutManager_3);
if (isWifiConnected (this)) {
presenterInterFace.toRequest ();
presenterInterFace.toMlss ();
presenterInterFace.toPzsh ();
} else {
List dayStepDao = App.daoSession.loadAll (GryBean.class);
for (int i = 0; i < dayStepDao.size (); i++) {
/*Product.ResultBean resultBean = new Product.ResultBean ();*/
/*resultBean.setName (dayStepDao.get (i).getTitle ());
resultBean.setImageUrl (dayStepDao.get (i).getImgUrl ());
mlist.add (resultBean);*/
}
}
myAdapter = new MyAdapter (list,this);
gyAdapter = new GyAdapter (glist,this);
pyAdapter = new PyAdapter (mlist,this);
recycler2.setAdapter (gyAdapter);
recycler1.setAdapter (myAdapter);
recycler3.setAdapter (pyAdapter);
presenterInterFace.toRequest ();
presenterInterFace.toMlss ();
presenterInterFace.toPzsh ();
}
@Override
public void toShow(Object o) {
Product product= (Product) o;
List<Product.ResultBean.RxxpBean.CommodityListBean> rlist=product.getResult ().getRxxp ().getCommodityList ();
list.addAll (rlist);
myAdapter.notifyDataSetChanged ();
}
@Override
public void toPhow(Object o) {
Product product= (Product) o;
List<Product.ResultBean.PzshBean.CommodityListBeanX> plist= product.getResult ().getPzsh ().getCommodityList ();
glist.addAll (plist);
gyAdapter.notifyDataSetChanged ();
}
@Override
public void toMhow(Object o) {
Product product= (Product) o;
List<Product.ResultBean.MlssBean.CommodityListBeanXX> clist=product.getResult ().getMlss ().getCommodityList ();
mlist.addAll (clist);
pyAdapter.notifyDataSetChanged ();
}
//判断网络可用
public boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworInfo = connectivityManager.getNetworkInfo (ConnectivityManager.TYPE_WIFI);
if (mNetworInfo != null) {
return mNetworInfo.isAvailable ();
}
}
return false;
}
}
二
public class OkHttp {
public static OkHttp utile;
Retrofit retrofit;
private OkHttp(){
retrofit = new Retrofit.Builder ()
.addCallAdapterFactory (RxJavaCallAdapterFactory.create ())
.addConverterFactory (GsonConverterFactory.create ())
.baseUrl ("http://172.17.8.100/")
.build ();
}
public static synchronized OkHttp getInstance(){
if(utile == null){
utile = new OkHttp ();
}
return utile;
}
public<T> T getApiService(Class<T> service){
return retrofit.create (service);
}
}
三
public class MyModel {
MyCallBack myCallBack;
public void toRequest(){
Api api = OkHttp.getInstance ().getApiService (Api.class);
api.getCircleContent (1,10)
.subscribeOn (Schedulers.io ())
.observeOn (AndroidSchedulers.mainThread ())
.subscribe (new Action1<ResponseBody> () {
@Override
public void call(ResponseBody responseBody) {
try {
String json = responseBody.string();
Gson gson = new Gson ();
Product product = gson.fromJson (json, Product.class);
myCallBack.success (product);
} catch (IOException e) {
e.printStackTrace ();
}
}
});
}
public void setMyCallBack(final MyCallBack myCallBack){
this.myCallBack=myCallBack;
}
public interface MyCallBack{
public void success(Object o);
}
}
四
public class Presenter implements CollectionInterFace.PresenterInterFace {
T tt;
MyModel model;
public Presenter(T t){
this.tt=t;
model = new MyModel ();
}
@Override
public void toRequest() {
model.setMyCallBack (new MyModel.MyCallBack () {
@Override
public void success(Object o) {
CollectionInterFace.MyViewInterFace myViewInterFace= (CollectionInterFace.MyViewInterFace) tt;
myViewInterFace.toShow (o);
}
});
model.toRequest ();
}
@Override
public void toPzsh() {
model.setMyCallBack (new MyModel.MyCallBack () {
@Override
public void success(Object o) {
CollectionInterFace.MyViewInterFace myViewInterFace= (CollectionInterFace.MyViewInterFace) tt;
myViewInterFace.toPhow (o);
}
});
model.toRequest ();
}
@Override
public void toMlss() {
model.setMyCallBack (new MyModel.MyCallBack () {
@Override
public void success(Object o) {
CollectionInterFace.MyViewInterFace myViewInterFace= (CollectionInterFace.MyViewInterFace) tt;
myViewInterFace.toMhow (o);
}
});
model.toRequest ();
}
}
五Api
public interface Api {
//http://172.17.8.100/small/commodity/v1/findCommodityListByLabel?labelId=1002&page=1&count=10
@GET("small/commodity/v1/commodityList")
Observable<ResponseBody> getCircleContent(@Query ("page")int page, @Query("count")int count);
/*@GET("small/commodity/v1/findCommodityListByLabel")
Observable<ResponseBody> getCircleContent(( @Query ("page")int page, @Query ("count")int count);*/
}
六 App
public class App extends Application {
public static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate ();
DaoMaster.DevOpenHelper devOpenHelper=new DaoMaster.DevOpenHelper(this,"mydb");
SQLiteDatabase sqLiteDatabase=devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(sqLiteDatabase);
DaoSession daoSession = daoMaster.newSession ();
Fresco.initialize(this);
}
}