Fragment中:
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.List; import butterknife.BindView; import bwie.com.month627.R; import bwie.com.month627.classify.adapter.LeftAdapter; import bwie.com.month627.classify.adapter.RightAdapter; import bwie.com.month627.classify.bean.ClaBean; import bwie.com.month627.classify.bean.ClaChildBean; import bwie.com.month627.classify.model.ClasModel; import bwie.com.month627.classify.presenter.ClasPresenter; import bwie.com.month627.classify.view.interfaces.IClasView; import bwie.com.mybase.base.BaseFragment; import bwie.com.mybase.mvp.BaseModel; public class ClassifyFragment extends BaseFragmentimplements IClasView { @BindView(R.id.left_classify) RecyclerView leftClassify; @BindView(R.id.right_classify) RecyclerView rightClassify; @Override protected void initDate() { presenter.getClas(); } @Override protected BaseModel initModel() { return new ClasModel(); } @Override protected ClasPresenter initPresenter() { return new ClasPresenter(); } @Override protected void initView() { leftClassify.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false)); rightClassify.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false)); } @Override protected int isLayoutId() { return R.layout.classify_frag; } @Override public void onSuccess(final ClaBean claBean) { final LeftAdapter adapter = new LeftAdapter(getContext(),claBean.getData()); leftClassify.addItemDecoration(new DividerItemDecoration(getContext(),RecyclerView.VERTICAL)); leftClassify.setAdapter(adapter); adapter.changeCheck(0,true); presenter.getClaChild(claBean.getData().get(0).getCid()); adapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() { @Override public void setClickListenner(int pos) { adapter.changeCheck(pos, true); presenter.getClaChild(claBean.getData().get(pos).getCid()); } }); } @Override public void onSuccess2(ClaChildBean claChildBean) { List list = claChildBean.getData(); RightAdapter adapter = new RightAdapter(getContext(),list); rightClassify.setAdapter(adapter); } @Override public void onFault(String s) { } }
左边的bean类
import java.util.List; public class ClaBean { /** * msg : * code : 0 * data : [{"cid":1,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/shop.png","ishome":1,"name":"京东超市"},{"cid":2,"createtime":"2017-10-10T19:41:39","icon":"http://120.27.23.105/images/category/qqg.png","ishome":1,"name":"全球购"},{"cid":3,"createtime":"2017-10-10T19:45:11","icon":"http://120.27.23.105/images/category/phone.png","ishome":1,"name":"手机数码"},{"cid":5,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/man.png","ishome":1,"name":"男装"},{"cid":6,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girl.png","ishome":1,"name":"女装"},{"cid":7,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/manshoe.png","ishome":1,"name":"男鞋"},{"cid":8,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/girlshoe.png","ishome":1,"name":"女鞋"},{"cid":9,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/shirt.png","ishome":1,"name":"内衣配饰"},{"cid":10,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/bag.png","ishome":1,"name":"箱包手袋"},{"cid":11,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/beauty.png","ishome":1,"name":"美妆个护"},{"cid":12,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/jewel.png","ishome":1,"name":"钟表珠宝"},{"cid":13,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/luxury.png","ishome":1,"name":"奢侈品"},{"cid":14,"createtime":"2017-10-10T20:12:03","icon":"http://120.27.23.105/images/category/computer.png","ishome":1,"name":"电脑办公"},{"cid":15,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"家用电器"},{"cid":16,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"食品生鲜"},{"cid":17,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"酒水饮料"},{"cid":18,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"母婴童装"},{"cid":19,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"玩具乐器"},{"cid":20,"createtime":"2017-09-29T10:13:48","icon":"http://120.27.23.105/images/icon.png","ishome":0,"name":"医药保健"}] */ private String msg; private String code; private Listdata; 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 getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * cid : 1 * createtime : 2017-10-10T19:41:39 * icon : http://120.27.23.105/images/category/shop.png * ishome : 1 * name : 京东超市 */ private int cid; private String createtime; private String icon; private int ishome; private String name; private boolean checked;//表示是否选中 public boolean getChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } 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 int getIshome() { return ishome; } public void setIshome(int ishome) { this.ishome = ishome; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }
右边的bean类
import java.util.List; public class ClaChildBean { /** * msg : 获取子分类成功 * code : 0 * data : [{"cid":"2","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"手机","pcid":6,"pscid":39},{"icon":"http://120.27.23.105/images/icon.png","name":"笔记本","pcid":6,"pscid":40},{"icon":"http://120.27.23.105/images/icon.png","name":"平板电脑","pcid":6,"pscid":41},{"icon":"http://120.27.23.105/images/icon.png","name":"游戏机","pcid":6,"pscid":42},{"icon":"http://120.27.23.105/images/icon.png","name":"摄影摄像","pcid":6,"pscid":43}],"name":"数码家电","pcid":"6"},{"cid":"2","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"男表","pcid":7,"pscid":44},{"icon":"http://120.27.23.105/images/icon.png","name":"女表","pcid":7,"pscid":45},{"icon":"http://120.27.23.105/images/icon.png","name":"儿童手表","pcid":7,"pscid":46},{"icon":"http://120.27.23.105/images/icon.png","name":"运动手表","pcid":7,"pscid":47},{"icon":"http://120.27.23.105/images/icon.png","name":"电子表","pcid":7,"pscid":48},{"icon":"http://120.27.23.105/images/icon.png","name":"情侣表","pcid":7,"pscid":49},{"icon":"http://120.27.23.105/images/icon.png","name":"石英表","pcid":7,"pscid":50},{"icon":"http://120.27.23.105/images/icon.png","name":"机械表","pcid":7,"pscid":51}],"name":"钟表","pcid":"7"},{"cid":"2","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"奢品箱包","pcid":8,"pscid":52},{"icon":"http://120.27.23.105/images/icon.png","name":"珠宝饰品","pcid":8,"pscid":53},{"icon":"http://120.27.23.105/images/icon.png","name":"运动户外","pcid":8,"pscid":54}],"name":"服饰奢品","pcid":"8"},{"cid":"2","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"面部护肤","pcid":9,"pscid":55},{"icon":"http://120.27.23.105/images/icon.png","name":"洗发护发","pcid":9,"pscid":56},{"icon":"http://120.27.23.105/images/icon.png","name":"身体护理","pcid":9,"pscid":57},{"icon":"http://120.27.23.105/images/icon.png","name":"口腔护理","pcid":9,"pscid":58},{"icon":"http://120.27.23.105/images/icon.png","name":"女性护理","pcid":9,"pscid":59},{"icon":"http://120.27.23.105/images/icon.png","name":"清洁用品","pcid":9,"pscid":60}],"name":"美妆个护","pcid":"9"}] */ private String msg; private String code; private Listdata; 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 getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * cid : 2 * list : [{"icon":"http://120.27.23.105/images/icon.png","name":"手机","pcid":6,"pscid":39},{"icon":"http://120.27.23.105/images/icon.png","name":"笔记本","pcid":6,"pscid":40},{"icon":"http://120.27.23.105/images/icon.png","name":"平板电脑","pcid":6,"pscid":41},{"icon":"http://120.27.23.105/images/icon.png","name":"游戏机","pcid":6,"pscid":42},{"icon":"http://120.27.23.105/images/icon.png","name":"摄影摄像","pcid":6,"pscid":43}] * name : 数码家电 * pcid : 6 */ private String cid; private String name; private String pcid; private List list; 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 getList() { return list; } public void setList(List list) { this.list = list; } public static class ListBean { /** * icon : http://120.27.23.105/images/icon.png * name : 手机 * pcid : 6 * pscid : 39 */ private String icon; private String name; private int pcid; private int pscid; 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 int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } } } }
model层:
import bwie.com.month627.classify.bean.ClaBean; import bwie.com.month627.classify.bean.ClaChildBean; import bwie.com.month627.utils.RetrofitFactory; import bwie.com.mybase.mvp.BaseModel; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; public class ClasModel extends BaseModel { public void getClas(final IClasModel iClasModel){ RetrofitFactory.getINSTANCE().getMyApi() .getClas() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ClaBean claBean) { iClasModel.onSuccess(claBean); } @Override public void onError(Throwable e) { iClasModel.onFault(e+""); } @Override public void onComplete() { } }); } public void getClaChile( int cid,final IClasModel iClasModel){ RetrofitFactory.getINSTANCE().getMyApi() .getChild(cid) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer () { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(ClaChildBean claChildBean) { iClasModel.onSuccess2(claChildBean); } @Override public void onError(Throwable e) { iClasModel.onFault(e+""); } @Override public void onComplete() { } }); } public interface IClasModel{ void onSuccess(ClaBean claBean); void onSuccess2(ClaChildBean claChildBean); void onFault(String s); } }
presenter层:
public class ClasPresenter extends BasePresenter, IClasView> { public void getClas() { model.getClas(new ClasModel.IClasModel() { @Override public void onSuccess(ClaBean claBean) { view.onSuccess(claBean); } @Override public void onSuccess2(ClaChildBean claChildBean) { } @Override public void onFault(String s) { view.onFault(s); } }); } public void getClaChild(int cid) { model.getClaChile(cid, new ClasModel.IClasModel() { @Override public void onSuccess(ClaBean claBean) { } @Override public void onSuccess2(ClaChildBean claChildBean) { view.onSuccess2(claChildBean); } @Override public void onFault(String s) { view.onFault(s); } }); } }
左边的适配器:
public class LeftAdapter extends RecyclerView.Adapter{ private Context context; private List list; private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public LeftAdapter(Context context, List list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.left_rcv,null); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) { ((MyHolder)holder).textView.setText(list.get(position).getName()); ((MyHolder)holder).relativeLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onItemClickListener.setClickListenner(position); } }); } @Override public int getItemCount() { return list.size(); } class MyHolder extends RecyclerView.ViewHolder { TextView textView; RelativeLayout relativeLayout; public MyHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.text); relativeLayout = itemView.findViewById(R.id.left_item); } } public interface OnItemClickListener{ void setClickListenner(int pos); } public void changeCheck(int position, boolean bool) { //先还原一下 for (int i = 0; i < list.size(); i++) { list.get(i).setChecked(false); } ClaBean.DataBean dataBean = list.get(position); dataBean.setChecked(bool); //刷新界面 notifyDataSetChanged(); } }
右边的适配器:
public class RightAdapter extends RecyclerView.Adapter{ private Context context; private List list; private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public RightAdapter(Context context, List list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.right_rcv,null); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { ((MyHolder)holder).recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)); RightItemAdapter adapter = new RightItemAdapter(context,list); ((MyHolder)holder).recyclerView.setAdapter(adapter); } @Override public int getItemCount() { return list.size(); } class MyHolder extends RecyclerView.ViewHolder { RecyclerView recyclerView; public MyHolder(View itemView) { super(itemView); recyclerView = itemView.findViewById(R.id.right_rcv); } } public interface OnItemClickListener{ void setonclicklistener(int pos); } }
右边商家的适配器:
public class RightItemAdapter extends RecyclerView.Adapter{ private Context context; private List list; public RightItemAdapter(Context context, List list) { this.context = context; this.list = list; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.right_item,null); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { ((MyHolder)holder).textView.setText(list.get(position).getName()); ((MyHolder)holder).recyclerView.setLayoutManager(new GridLayoutManager(context,5,RecyclerView.VERTICAL,false)); RightItem2Adapter adapter = new RightItem2Adapter(context,list.get(position).getList()); ((MyHolder)holder).recyclerView.setAdapter(adapter); } @Override public int getItemCount() { return list.size(); } class MyHolder extends RecyclerView.ViewHolder { TextView textView; RecyclerView recyclerView; public MyHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.name); recyclerView = itemView.findViewById(R.id.right_item); } } }
右边商家下的子商品的适配器:
public class RightItem2Adapter extends RecyclerView.Adapter{ private Context context; private List listBeans; private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } public RightItem2Adapter(Context context, List listBeans) { this.context = context; this.listBeans = listBeans; } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.right_item2,null); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { Uri uri=Uri.parse(listBeans.get(position).getIcon()); ((MyHolder)holder).sdv.setImageURI(uri); ((MyHolder)holder).textView.setText(listBeans.get(position).getName()); } @Override public int getItemCount() { return listBeans.size(); } class MyHolder extends RecyclerView.ViewHolder { SimpleDraweeView sdv; TextView textView; public MyHolder(View itemView) { super(itemView); sdv = itemView.findViewById(R.id.sdv); textView = itemView.findViewById(R.id.text); } } public interface OnItemClickListener{ void setClickListener(int pos); } }
View接口
public interface IClasView extends IBaseView { void onSuccess(ClaBean claBean); void onSuccess2(ClaChildBean claChildBean); void onFault(String s); }