需要的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.picasso:picasso:2.3.2'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
implementation 'com.squareup.okhttp3:okhttp:3.4.2'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
.activity_main
<?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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:background="#d1c7c7"
android:id="@+id/rv_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"></android.support.v7.widget.RecyclerView>
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8">
<LinearLayout
android:id="@+id/ll_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
.mainclassify_item
<?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"
android:gravity="center">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="10dp"
android:textSize="20dp" />
</LinearLayout>
.nextclassify_item
<?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"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/img_next"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/txt_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
此处用的网络工具类 .HttpUtils
public class HttpUtils {
private static volatile HttpUtils instance;
private OkHttpClient client;
private Handler handler = new Handler();
private HttpUtils(){
client = new OkHttpClient();
}
public static HttpUtils getInstance(){
if (instance == null){
synchronized (HttpUtils.class){
if (null == instance){
instance = new HttpUtils();
}
}
}
return instance;
}
public void get(String url, final ICallBack callBack, final Type type){
Request request = new Request.Builder()
.get()
.url(url)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.Failed(e);
}
});
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(string, type);
handler.post(new Runnable() {
@Override
public void run() {
callBack.Success(o);
}
});
}
});
}
}
两个Bean类
.MainBean
public class MainBean {
private String msg;
private String code;
private List<DataBean> data;
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<DataBean> getData() {
return data;
}
public void setData(List<DataBean> 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;
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;
}
}
}
.NextBean
public class NextBean {
private String msg;
private String code;
private List<DataBean> data;
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<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* cid : 1
* list : [{"icon":"http://120.27.23.105/images/icon.png","name":"月饼","pcid":1,"pscid":1},{"icon":"http://120.27.23.105/images/icon.png","name":"坚果炒货","pcid":1,"pscid":2},{"icon":"http://120.27.23.105/images/icon.png","name":"糖巧","pcid":1,"pscid":3},{"icon":"http://120.27.23.105/images/icon.png","name":"休闲零食","pcid":1,"pscid":4},{"icon":"http://120.27.23.105/images/icon.png","name":"肉干肉脯","pcid":1,"pscid":5},{"icon":"http://120.27.23.105/images/icon.png","name":"饼干蛋糕","pcid":1,"pscid":6},{"icon":"http://120.27.23.105/images/icon.png","name":"蜜饯果干","pcid":1,"pscid":7},{"icon":"http://120.27.23.105/images/icon.png","name":"无糖食品","pcid":1,"pscid":8}]
* name : 休闲零食
* pcid : 1
*/
private String cid;
private String name;
private String pcid;
private List<ListBean> 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<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
/**
* icon : http://120.27.23.105/images/icon.png
* name : 月饼
* pcid : 1
* pscid : 1
*/
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;
}
}
}
}
创建一个接口
.ICallBack
public interface ICallBack {
void Success(Object o);
void Failed(Exception e);
}
创建一个Model层
.Model
public class Model {
public void getSuccess(String url, ICallBack callBack, Type type){
HttpUtils.getInstance().get(url,callBack,type);
}
}
创建一个V层 注意是一个接口
.FenView
public interface FenView {
void getMain(List<MainBean.DataBean> list);
void getNext(List<NextBean.DataBean> list);
void getFailed(Exception e);
}
创建一个P层
.FenPresenter
public class FenPresenter {
private FenView cv;
private Model model;
public void attach(FenView cv) {
this.cv = cv;
model = new Model();
}
public void detch() {
if (cv != null) {
cv = null;
}
}
public void getMain(){
Type type = new TypeToken<MainBean>(){}.getType();
model.getSuccess("http://www.zhaoapi.cn/product/getCatagory", new ICallBack() {
@Override
public void Success(Object o) {
MainBean mainBean = (MainBean) o;
if (mainBean != null){
cv.getMain(mainBean.getData());
}
}
@Override
public void Failed(Exception e) {
cv.getFailed(e);
}
},type);
}
public void getNext(String url){
Type type = new TypeToken<NextBean>(){}.getType();
model.getSuccess(url, new ICallBack() {
@Override
public void Success(Object o) {
NextBean nextBean = (NextBean) o;
if (nextBean != null){
cv.getNext(nextBean.getData());
}
}
@Override
public void Failed(Exception e) {
cv.getFailed(e);
}
},type);
}
}
写那两个Adapter
.MainAdapter
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>{
private Context context;
private List<MainBean.DataBean> list;
public MainAdapter(Context context, List<MainBean.DataBean> list) {
this.context = context;
this.list = list;
}
public interface OnItemClicklistener{
void onItemClick(View itemView, int position);
}
private OnItemClicklistener clicklistener;
public void getOnItemClicklistener(OnItemClicklistener clicklistener){
this.clicklistener = clicklistener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= View.inflate(context, R.layout.mainclassify_item,null);
ViewHolder holder=new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtTitle.setText(list.get(position).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clicklistener != null){
clicklistener.onItemClick(v,position);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
txtTitle=itemView.findViewById(R.id.txt_title);
}
}
}
.NextAdapter
public class NextAdapter extends RecyclerView.Adapter<NextAdapter.ViewHolder>{
private Context context;
private List<NextBean.DataBean.ListBean> list;
public NextAdapter(Context context, List<NextBean.DataBean.ListBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = View.inflate(context, R.layout.nextclassify_item, null);
ViewHolder holder=new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Glide.with(context).load(list.get(position).getIcon()).into(holder.imgNext);
holder.txtNext.setText(list.get(position).getName());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imgNext;
private TextView txtNext;
public ViewHolder(View itemView) {
super(itemView);
imgNext = itemView.findViewById(R.id.img_next);
txtNext = itemView.findViewById(R.id.txt_next);
}
}
}
最后是这个.MainActivity
public class MainActivity extends AppCompatActivity implements FenView {
private RecyclerView rvMain;
private LinearLayout llNext;
private MainAdapter mMainAdapter;
private NextAdapter mNextAdapter;
private List<MainBean.DataBean> mainList;
private FenPresenter presenter;
private List<NextBean.DataBean> nextList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
rvMain = (RecyclerView) findViewById(R.id.rv_main);
llNext = (LinearLayout) findViewById(R.id.ll_next);
mainList = new ArrayList<>();
nextList = new ArrayList<>();
mMainAdapter = new MainAdapter(this,mainList);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
rvMain.addItemDecoration(new DividerItemDecoration(MainActivity.this,DividerItemDecoration.VERTICAL));
rvMain.setLayoutManager(manager);
rvMain.setAdapter(mMainAdapter);
presenter = new FenPresenter();
presenter.attach(MainActivity.this);
presenter.getMain();
}
private void initData() {
mMainAdapter.getOnItemClicklistener(new MainAdapter.OnItemClicklistener() {
@Override
public void onItemClick(View itemView, int position) {
MainBean.DataBean dataBean = mainList.get(position);
presenter.getNext("http://www.zhaoapi.cn/product/getProductCatagory?cid="+dataBean.getCid());
}
});
}
@Override
public void getMain(List<MainBean.DataBean> list) {
if (list != null){
mainList.clear();
mainList.addAll(list);
mMainAdapter.notifyDataSetChanged();
}
}
@Override
public void getNext(List<NextBean.DataBean> list) {
if (list != null){
llNext.removeAllViews();
for (int i = 0; i <list.size() ; i++) {
TextView textView = new TextView(this);
textView.setText(list.get(i).getName());
RecyclerView recyclerView = new RecyclerView(this);
RecyclerView.LayoutManager manager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(manager);
mNextAdapter = new NextAdapter(this,list.get(i).getList());
recyclerView.setAdapter(mNextAdapter);
nextList.clear();
nextList.addAll(list);
mNextAdapter.notifyDataSetChanged();
llNext.addView(textView);
llNext.addView(recyclerView);
}
}
}
@Override
public void getFailed(Exception e) {
Toast.makeText(this,"网络连接错误",Toast.LENGTH_SHORT).show();
}
@Override
public void onResume() {
super.onResume();
presenter.getNext("http://www.zhaoapi.cn/product/getProductCatagory?cid=1");
}
@Override
public void onDestroy() {
super.onDestroy();
if (presenter != null){
presenter.detch();
}
}
}