首先用到的依赖有
//okgo
implementation 'com.lzy.net:okgo:3.0.4'
implementation 'org.kie.modules:com-google-code-gson:6.5.0.Final'
//butterknife快速查找资源的依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//Brvah:RecyclerView快速开发框架
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
///glide图片加载框架
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
brvah使用需要在最外的gradle里面写入
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
清单文件下的网络权限
<uses-permission android:name="android.permission.INTERNET"/>
下面开始写布局
主布局
<android.support.v7.widget.RecyclerView
android:id="@+id/fl_recyle"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/xq_recyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
>
</android.support.v7.widget.RecyclerView>
分类的xml
<TextView
android:id="@+id/recy_text"
android:layout_margin="5dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
右侧的条目的xml
<TextView
android:id="@+id/busimnss_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商家"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/busimnss_recyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
商家的条目信息
<ImageView
android:id="@+id/goods_img"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/goods_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商品1"
/>
契约类
public interface IContant {
//V层
public interface IView {
//展示详情数据
public void flshowData(String fl);
//详情
public void xqshowData(String xq);
}
//P层
public interface IPresenter<IView> {
//绑定
public void attachView(IView iView);
//解绑
public void detachView(IView iView);
//分类
public void flrequest();
//详情
public void xqrequest(int cid);
}
//M层
public interface IModel {
//分类
public void flresquest(OnCallBack oncallback);
//请求详情数据
public void xqresponse(int cid, OnCallBack callback);
//回调
public interface OnCallBack {
public void onCallBack(String fl);
}
}
}
P层
public class IPersenter implements IContant.IPresenter<IContant.IView> {
private IModel iModel;
private SoftReference<IModel> reference;
IContant.IView iView;
@Override
public void attachView(IContant.IView iView) {
this.iView =iView;
iModel = new IModel();
reference = new SoftReference<>(iModel);
}
@Override
public void detachView(IContant.IView iView) {
reference.clear();
}
//分类的P
@Override
public void flrequest() {
iModel.flresquest(new IContant.IModel.OnCallBack() {
@Override
public void onCallBack(String fl) {
iView.flshowData(fl);
}
});
}
//详情的P层
@Override
public void xqrequest(int cid) {
iModel.xqresponse(cid,new IContant.IModel.OnCallBack() {
@Override
public void onCallBack(String xq) {
iView.xqshowData(xq);
}
});
}
}
M层
public class IModel implements IContant.IModel {
@Override
public void flresquest(final OnCallBack oncallback) {
OkGo.<String>get(Conters.FLINTER_URL).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String fl = response.body().toString();
Log.i("yyy", "onSuccess: "+fl);
oncallback.onCallBack(fl);
}
});
}
@Override
public void xqresponse(int cid, final OnCallBack callback) {
OkGo.<String>get(Conters.XQINTER_URL).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String xq = response.body().toString();
Log.i("yyy", "onSuccess: "+xq);
callback.onCallBack(xq);
}
});
}
}
activity界面
public class HomeActivity extends AppCompatActivity implements IContant.IView {
@BindView(R.id.xq_recyle)
RecyclerView xqRecyle;
@BindView(R.id.fl_recyle)
RecyclerView flRecyle;
private IPersenter iPersenter;
private ListAdapter adapter;
private List<FlBean.DataBean> data;
private FlAdapter adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
//P层
iPersenter = new IPersenter();
iPersenter.attachView(this);
iPersenter.flrequest();
}
//分类的列表
@Override
public void flshowData(String fl) {
//gson解析
Gson gson = new Gson();
FlBean flBean = gson.fromJson(fl, FlBean.class);
data = flBean.getData();
//布局管理器
LinearLayoutManager manager = new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.VERTICAL, false);
flRecyle.setLayoutManager(manager);
//显示子条目
adapter1 = new FlAdapter(R.layout.fllist_item,data);
flRecyle.setAdapter(adapter1);
//点击触发而获取id
adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
int cid = data.get(position).getCid();
iPersenter.xqrequest(cid);
}
});
}
//详情的回传数据
@Override
public void xqshowData(String xq) {
//Gson解析
Gson gson = new Gson();
XqBean xqBean = gson.fromJson(xq, XqBean.class);
List<XqBean.DataBean> data = xqBean.getData();
//布局管理器
LinearLayoutManager manager = new LinearLayoutManager(HomeActivity.this);
xqRecyle.setLayoutManager(manager);
//recyleview的适配器
RecyAdapter adapter = new RecyAdapter(R.layout.business_item, data);
xqRecyle.setAdapter(adapter);
}
//避免内存泄漏
@Override
protected void onDestroy() {
super.onDestroy();
iPersenter.detachView(this);
}
}
左侧条目的适配器
public class FlAdapter extends BaseQuickAdapter<FlBean.DataBean,BaseViewHolder> {
public FlAdapter(int layoutResId, @Nullable List<FlBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, FlBean.DataBean item) {
//利用helper获取id
helper.setText(R.id.recy_text,item.getName());
}
}
商家的条目
public class RecyAdapter extends BaseQuickAdapter<XqBean.DataBean,BaseViewHolder> {
public RecyAdapter(int layoutResId, @Nullable List<XqBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, XqBean.DataBean item) {
//获取id
helper.setText(R.id.busimnss_text, item.getName());
//获取商家的recyleView
RecyclerView goods_text = helper.getView(R.id.busimnss_recyle);
List<XqBean.DataBean.ListBean> list = item.getList();
//布局管理器
GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false);
goods_text.setLayoutManager(gridLayoutManager);
//条目详情的adapter
GoodsAdapter adapter = new GoodsAdapter(R.layout.goods_item,list);
goods_text.setAdapter(adapter);
}
}
商家里面的详情
public class GoodsAdapter extends BaseQuickAdapter<XqBean.DataBean.ListBean,BaseViewHolder> {
public GoodsAdapter(int layoutResId, @Nullable List<XqBean.DataBean.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, XqBean.DataBean.ListBean item) {
helper.setText(R.id.goods_text,item.getName());
ImageView imageView = helper.getView(R.id.goods_img);
Glide.with(mContext).load(item.getIcon()).into(imageView);
}
}