// activity_main.xml
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.zhang.myapplication.MainActivity"> <LinearLayout android:paddingTop="30dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <android.support.v7.widget.RecyclerView android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/lift_RecyclerView" android:layout_weight="2" ></android.support.v7.widget.RecyclerView> <android.support.v7.widget.RecyclerView android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/rihge_RecyclerView" android:layout_weight="8" ></android.support.v7.widget.RecyclerView> </LinearLayout> <com.zhang.myapplication.TitlBar android:layout_width="match_parent" android:layout_height="wrap_content"></com.zhang.myapplication.TitlBar> </RelativeLayout>
//lift.xml 左边的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:background="@drawable/bbbb" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lift_name" /> </LinearLayout>//rihget.xml 右边1级表
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:paddingBottom="10dp" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="6dp" android:background="#ff0000" android:id="@+id/text_rihget" /> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/RecyclerView_rihget" ></android.support.v7.widget.RecyclerView> </LinearLayout>//rihgeti_zi.xml 右边2级表
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <ImageView android:layout_width="45dp" android:layout_height="45dp" android:id="@+id/img_rihget_zi" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_rihget_zi" /> </LinearLayout>
// MainActivity
public class MainActivity extends AppCompatActivity implements IView<MseBean<List<LiftBean>>>,IMyRihgetview { // @BindView(R.id.lift_RecyclerView) RecyclerView liftRecyclerView; // @BindView(R.id.rihge_RecyclerView) RecyclerView rihgeRecyclerView; int cidi=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); liftRecyclerView= findViewById(R.id.lift_RecyclerView); rihgeRecyclerView= findViewById(R.id.rihge_RecyclerView); liftRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); shujiuqinqui(); shujiuqinqui2(); } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void getNet(NNN e){ Log.i("@@@2",4566+""); if (e==NNN.KO){ shujiuqinqui(); shujiuqinqui2(); } } private void shujiuqinqui() { MyLiftPresenter myLiftPresenter = new MyLiftPresenter(); Map<String ,String> map = new HashMap<>(); myLiftPresenter.getLift(new MyModel(),this,map); } private void shujiuqinqui2() { MyRihgetPresenter myRihgetPresenter = new MyRihgetPresenter(); Map<String ,String> map = new HashMap<>(); map.put("cid",cidi+""); myRihgetPresenter.getLift(new MyRihgeModel(),this,map); } @Subscribe(threadMode=ThreadMode.MAIN) public void cccc(Integer cid){ cidi= cid; shujiuqinqui2(); } @Override public void onSuccess(MseBean<List<LiftBean>> listMseBean) { List<LiftBean> t = listMseBean.getData(); int size = t.size(); Mydapte mydapte=new Mydapte(t,this); liftRecyclerView.setLayoutManager(new LinearLayoutManager(this)); liftRecyclerView.setAdapter(mydapte); } @Override public void onError(String ss) { Toast.makeText(this,ss,Toast.LENGTH_SHORT).show(); } //二级qinqui @Override public void onSuccess(List<RihgetBen> list) { Mydapte2 mydapte2 = new Mydapte2(list, this); rihgeRecyclerView.setLayoutManager(new LinearLayoutManager(this)); rihgeRecyclerView.setAdapter(mydapte2); } //适配 class Mydapte extends RecyclerView.Adapter<Mydapte.MyviewHoder>{ List<LiftBean> t; Context context; int ii=0; public Mydapte(List<LiftBean> t, Context context) { this.t = t; this.context = context; } @Override public MyviewHoder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(context).inflate(R.layout.lift, parent, false); MyviewHoder myviewHoder = new MyviewHoder(inflate); return myviewHoder; } @Override public void onBindViewHolder(MyviewHoder holder, final int position) { holder.name.setText(t.get(position).getName()); holder.name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ii=position; long cid1 = t.get(position).getCid(); int cid =(int) cid1; // EventBus EventBus.getDefault().post(cid); notifyDataSetChanged(); } }); if (ii==position){ holder.name.setBackgroundColor(Color.RED); }else { holder.name.setBackgroundColor(Color.WHITE); } } @Override public int getItemCount() { return t.size(); } class MyviewHoder extends RecyclerView.ViewHolder { TextView name; public MyviewHoder(View itemView) { super(itemView); name=itemView.findViewById(R.id.lift_name); } } } //右边适配 class Mydapte2 extends RecyclerView.Adapter<Mydapte2.MyViewHoder>{ List<RihgetBen> list; Context context; public Mydapte2(List<RihgetBen> list, Context context) { this.list = list; this.context = context; } @Override public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(context).inflate(R.layout.rihget, parent, false); MyViewHoder myViewHoder = new MyViewHoder(inflate); return myViewHoder; } @Override public void onBindViewHolder(MyViewHoder holder, int position) { holder.text.setText(list.get(position).getName()); holder.recyclerView.setLayoutManager(new GridLayoutManager(context,3)); List<ListBean> list = this.list.get(position).getList(); Mydapte3 mydapte3 = new Mydapte3(list, context); holder.recyclerView.setAdapter(mydapte3); } @Override public int getItemCount() { return list.size(); } class MyViewHoder extends RecyclerView.ViewHolder{ RecyclerView recyclerView; TextView text; public MyViewHoder(View itemView) { super(itemView); text = itemView.findViewById(R.id.text_rihget); recyclerView = itemView.findViewById(R.id.RecyclerView_rihget); } } } //右边适配子 class Mydapte3 extends RecyclerView.Adapter<Mydapte3.MyViewHoder3>{ List<ListBean> list; Context context; public Mydapte3( List<ListBean> list, Context context) { this.list = list; this.context = context; } @Override public MyViewHoder3 onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(context).inflate(R.layout.rihgeti_zi, parent, false); MyViewHoder3 myViewHoder = new MyViewHoder3(inflate); return myViewHoder; } @Override public void onBindViewHolder(MyViewHoder3 holder, int position) { holder.text.setText(list.get(position).getName()); Glide.with(context).load(list.get(position).getIcon()).into(holder.img); } @Override public int getItemCount() { return list.size(); } class MyViewHoder3 extends RecyclerView.ViewHolder{ ImageView img; TextView text; public MyViewHoder3(View itemView) { super(itemView); text = itemView.findViewById(R.id.text_rihget_zi); img = itemView.findViewById(R.id.img_rihget_zi); } } } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
/** * Myapp * author:Created by WangZhiQiang on 2018/4/20. */ public class Myapp extends Application { public static Context context; private NetWorkStateReceiver receiver; List<LiftBean> data=new ArrayList<>(); private Map<String,List<RihgetBen> > map= new HashMap(); public static Myapp myapp; @Override public void onCreate() { super.onCreate(); myapp=this; context=this; //android.net.conn.CONNECTIVITY_CHANGE IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); receiver = new NetWorkStateReceiver(); registerReceiver(receiver, intentFilter); } public Map<String, List<RihgetBen>> getMap(){ return map; } public List<LiftBean> getdata(){ return data; } @Override public void onTerminate() { super.onTerminate(); if (receiver != null) { unregisterReceiver(receiver); } } }
/** * RetrofitUtil 工具类 * author:Created by WangZhiQiang on 2018/4/18. */ public class RetrofitUtil{ private static RetrofitUtil retrofitUtil; private RetrofitUtil(){ } public static RetrofitUtil getRetrofitUtil(){ if (retrofitUtil==null){ synchronized (RetrofitUtil.class){ if (retrofitUtil==null){ retrofitUtil=new RetrofitUtil(); } } } return retrofitUtil; } public Reservics getPost(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(Consents.URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); Reservics reservics = retrofit.create(Reservics.class); return reservics; } }
/** * Reservics Retrofit * author:Created by WangZhiQiang on 2018/4/18. */ public interface Reservics { @GET("product/getCatagory") Flowable<MseBean<List<LiftBean>>> getGetLift(); @POST("product/getProductCatagory") @FormUrlEncoded Flowable <MseBean<List<RihgetBen>>> getRihget(@FieldMap Map<String, String> map); }/** * 检测当前的网络状态 * author:Created by WangZhiQiang on 2018/4/20. */ public class NetUtil { Context context; //检测当前的网络状态 public boolean genNet(){ int kitkat = Build.VERSION_CODES.KITKAT; if (kitkat<21){ return checkState_23(); }else { return checkState_23orNew(); } } //API版本23以下时调用此方法进行检测 //因为API23后getNetworkInfo(int networkType)方法被弃用 public boolean checkState_23(){ context=Myapp.context; //步骤1:通过Context.getSystemService(Context.CONNECTIVITY_SERVICE)获得ConnectivityManager对象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //步骤2:获取ConnectivityManager对象对应的NetworkInfo对象 //NetworkInfo对象包含网络连接的所有信息 //步骤3:根据需要取出网络连接信息 //获取WIFI连接的信息 NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); Boolean isWifiConn = networkInfo.isConnected(); //获取移动数据连接的信息 networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); Boolean isMobileConn = networkInfo.isConnected(); return isWifiConn||isMobileConn; } // API 23及以上时调用此方法进行网络的检测 // getAllNetworks() 在API 21后开始使用 //步骤非常类似 @TargetApi(Build.VERSION_CODES.LOLLIPOP) public boolean checkState_23orNew(){ //获得ConnectivityManager对象 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //获取所有网络连接的信息 Network[] networks = connMgr.getAllNetworks(); //用于存放网络连接信息 StringBuilder sb = new StringBuilder(); //通过循环将网络信息逐个取出来 for (int i=0; i < networks.length; i++){ //获取ConnectivityManager对象对应的NetworkInfo对象 NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]); boolean connected = networkInfo.isConnected(); return true; } return false; } }/** * GrennDaoUtil * author:Created by WangZhiQiang on 2018/4/20. */ public class GrennDaoUtil { private DaoMaster daoMaster; private DaoSession daoSession; private SQLiteDatabase sqLiteDatabase; private DaoMaster.DevOpenHelper devOpenHelper; private static volatile GrennDaoUtil mInstance; private GrennDaoUtil() { devOpenHelper = new DaoMaster.DevOpenHelper(Myapp.context, "dd.db"); sqLiteDatabase = devOpenHelper.getWritableDatabase(); daoMaster = new DaoMaster(sqLiteDatabase); daoSession = daoMaster.newSession(); } public static GrennDaoUtil getInstance() { if(mInstance ==null) { synchronized(GrennDaoUtil.class) { if(mInstance ==null) { mInstance= new GrennDaoUtil(); } } } return mInstance; } public LiftBeanDao getLiftBeanDao() { LiftBeanDao liftBeanDao = daoSession.getLiftBeanDao(); return liftBeanDao; } public RihgetBenDao getRihgetBenDao(){ RihgetBenDao rihgetBenDao = daoSession.getRihgetBenDao(); return rihgetBenDao; } public ListBeanDao getLisBenDao(){ ListBeanDao listBeanDao = daoSession.getListBeanDao(); return listBeanDao; } }/** * 存储接口 * author:Created by WangZhiQiang on 2018/4/18. */ public class Consents { public static final String URL="https://www.zhaoapi.cn/"; }
/** * BroadcastReceiver广播 * author:Created by WangZhiQiang on 2018/4/20. */ public class NetWorkStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { boolean b = new NetUtil().genNet(); Log.i("45656",b+""); EventBus.getDefault().post(b?NNN.KO:NNN.NO); } }/** * 枚举 * author:Created by WangZhiQiang on 2018/4/20. */
public enum NNN { KO, NO }/** * IView * author:Created by WangZhiQiang on 2018/4/20. */ public interface IView<T> { void onSuccess(T t); void onError(String ss); }/** * IMyRihgetview * author:Created by WangZhiQiang on 2018/4/20. */ public interface IMyRihgetview { void onSuccess(List<RihgetBen> list); }/** * MyRihgetPresenter * author:Created by WangZhiQiang on 2018/4/20. */ public class MyRihgetPresenter { public void getLift(IModel<List<RihgetBen>> iModel, final IMyRihgetview iView, Map<String,String> map){ iModel.getNetConSente(map, new IModelJieKou<List<RihgetBen>>() { @Override public void onSuccess(List<RihgetBen> list) { iView.onSuccess(list); } @Override public void onError(String ss) { } }); } }/** * MyLiftPresenter * author:Created by WangZhiQiang on 2018/4/20. */ public class MyLiftPresenter { public void getLift(IModel<MseBean<List<LiftBean>>> iModel, final IView<MseBean<List<LiftBean>>> iView, Map<String,String>map){ iModel.getNetConSente(map, new IModelJieKou<MseBean<List<LiftBean>>>() { @Override public void onSuccess(MseBean<List<LiftBean>> listMseBean) { iView.onSuccess(listMseBean); } @Override public void onError(String ss) { iView.onError(ss); } }); } }/** * MyRihgeModel * author:Created by WangZhiQiang on 2018/4/20. */ public class MyRihgeModel implements IModel<List<RihgetBen>>{ private RihgetBenDao rihgetBenDao; private ListBeanDao lisBenDao; @Override public void getNetConSente(Map<String, String> map, final IModelJieKou<List<RihgetBen>> iModel) { final String s = map.get("cid"); rihgetBenDao = GrennDaoUtil.getInstance().getRihgetBenDao(); lisBenDao = GrennDaoUtil.getInstance().getLisBenDao(); final Map<String, List<RihgetBen>> map1 = Myapp.myapp.getMap(); if (map1.size()>0){ List<RihgetBen> rihgetBens = map1.get(s); if (rihgetBens!=null) { iModel.onSuccess(rihgetBens); }else { List<RihgetBen> sqlit = getSqlit(s); if (sqlit!=null){ iModel.onSuccess(sqlit); } } }else { List<RihgetBen> sqlit = getSqlit(s); if (sqlit!=null){ iModel.onSuccess(sqlit); } } //数据库取 Reservics post = RetrofitUtil.getRetrofitUtil().getPost(); final Flowable<MseBean<List<RihgetBen>>> rihget = post.getRihget(map); rihget.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<MseBean<List<RihgetBen>>>() { @Override public void onNext(MseBean<List<RihgetBen>> listMseBean) { List<RihgetBen> data = listMseBean.getData(); iModel.onSuccess(data); Log.d("#eeeeee#",data.size()+""); map1.put(s,data);//内存 //一级表 RihgetBenDao rihgetBenDao = GrennDaoUtil.getInstance().getRihgetBenDao(); rihgetBenDao.insertOrReplaceInTx(data); //二级表 for (RihgetBen datum : data) { ListBeanDao lisBenDao = GrennDaoUtil.getInstance().getLisBenDao(); lisBenDao.insertOrReplaceInTx(datum.getList()); } } @Override public void onError(Throwable t) { iModel.onError(t.toString()); Log.d("#eeeeee#","失败"); } @Override public void onComplete() { } }); } public List<RihgetBen> getSqlit( String s){ List<RihgetBen> list = null; if (rihgetBenDao.queryBuilder().list().size() > 0) { list = rihgetBenDao.queryBuilder().where(RihgetBenDao.Properties.Cid.eq(s)).list(); if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { String pcid = list.get(i).getPcid(); if (lisBenDao != null) { List<ListBean> list1 = lisBenDao.queryBuilder().where(ListBeanDao.Properties.Pcid.eq(pcid)).list(); list.get(i).setList(list1); } } } } return list; } }/** * MyModel * author:Created by WangZhiQiang on 2018/4/20. */ public class MyModel implements IModel<MseBean<List<LiftBean>>> { @Override public void getNetConSente(Map<String, String> map, final IModelJieKou<MseBean<List<LiftBean>>> iModelJieKou) { LiftBeanDao liftBeanDao = GrennDaoUtil.getInstance().getLiftBeanDao(); final List<LiftBean> getdata = Myapp.myapp.getdata(); if (getdata.size()>0){//内存取 MseBean<List<LiftBean>> listMseBean = new MseBean<>(); listMseBean.setData(getdata); iModelJieKou.onSuccess(listMseBean); Log.i("内存取","2333"); }else {//数据库取 if (liftBeanDao!=null){ List<LiftBean> list = liftBeanDao.queryBuilder().list(); getdata.addAll(list);//存入内存 if (list.size()>0){ MseBean<List<LiftBean>> listMseBean = new MseBean<>(); listMseBean.setData(list); iModelJieKou.onSuccess(listMseBean); Log.i("数据库取","2333"); } } } Reservics post = RetrofitUtil.getRetrofitUtil().getPost(); Log.d("@#@#111","123335"); Flowable<MseBean<List<LiftBean>>> getLift = post.getGetLift(); getLift.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<MseBean<List<LiftBean>>>() { @Override public void onNext(MseBean<List<LiftBean>> listMseBean) { List<LiftBean> ee = listMseBean.getData(); iModelJieKou.onSuccess(listMseBean); //数据库 LiftBeanDao liftBeanDao = GrennDaoUtil.getInstance().getLiftBeanDao(); liftBeanDao.insertOrReplaceInTx(ee); //内存 getdata.addAll(ee); } @Override public void onError(Throwable t) { iModelJieKou.onError(t.toString()); } @Override public void onComplete() { } }); } }/** * IModelJieKou * author:Created by WangZhiQiang on 2018/4/20. */ public interface IModelJieKou<T> { void onSuccess(T t); void onError(String ss); }/** * IModel * author:Created by WangZhiQiang on 2018/4/20. */ public interface IModel<T> { void getNetConSente(Map<String,String>map,IModelJieKou<T> iModel); }/** * RihgetBen * author:Created by WangZhiQiang on 2018/4/20. */ @Entity public class RihgetBen { private String cid; private String name; private String pcid; @Transient private List<ListBean> list; @Generated(hash = 527460331) public RihgetBen(String cid, String name, String pcid) { this.cid = cid; this.name = name; this.pcid = pcid; } @Generated(hash = 1987072194) public RihgetBen() { } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPcid() { return pcid; } public void setPcid(String pcid) { this.pcid = pcid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } }/** * MseBean 最外层bean类 * author:Created by WangZhiQiang on 2018/4/20. */ public class MseBean<T> { private String code; private String msg; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } }/** * ListBean * author:Created by WangZhiQiang on 2018/4/20. */ @Entity public class ListBean { private String icon; private String name; private int pcid; @Id private Long pscid; @Generated(hash = 136404071) public ListBean(String icon, String name, int pcid, Long pscid) { this.icon = icon; this.name = name; this.pcid = pcid; this.pscid = pscid; } @Generated(hash = 777734033) public ListBean() { } public String getIcon() { return icon; } public void setIcon(String icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPcid() { return pcid; } public void setPcid(int pcid) { this.pcid = pcid; } public Long getPscid() { return pscid; } public void setPscid(Long pscid) { this.pscid = pscid; } }** * LiftBean * author:Created by WangZhiQiang on 2018/4/20. */ @Entity public class LiftBean { /** * cid : 1 * createtime : 2017-10-10T19:41:39 * icon : http://120.27.23.105/images/category/shop.png * ishome : 1 * name : 京东超市 */ @Id private Long cid; private String createtime; private String icon; private int ishome; private String name; @Generated(hash = 1457007141) public LiftBean(Long cid, String createtime, String icon, int ishome, String name) { this.cid = cid; this.createtime = createtime; this.icon = icon; this.ishome = ishome; this.name = name; } @Generated(hash = 1829345023) public LiftBean() { } public Long getCid() { return this.cid; } public void setCid(Long cid) { this.cid = cid; } public String getCreatetime() { return this.createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getIcon() { return this.icon; } public void setIcon(String icon) { this.icon = icon; } public int getIshome() { return this.ishome; } public void setIshome(int ishome) { this.ishome = ishome; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }