依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.squareup.picasso:picasso:2.3.2'
implementation 'com.google.guava:guava:16.0.1'
NekWord接口
public interface NekWord {
void onSuccessUser(List<User.DataBean> list);
void onSuccessUserBean(List<UserBean.DataBean> listBeans);
void fail(String fail);
OkHttp工具类
public class Okhttp {
private static final String METH_GET="GET";
private static OkHttpClient client;
private Okhttp() {
}
public static void init(){
client = new OkHttpClient.Builder()
.readTimeout(3000, TimeUnit.MILLISECONDS)
.writeTimeout(3000, TimeUnit.MILLISECONDS)
.connectTimeout(3000, TimeUnit.MILLISECONDS)
.build();
}
public static void onGet(String url, Callback callback){
Request request=createRequest(url,METH_GET);
Call call = client.newCall(request);
call.enqueue(callback);
}
private static Request createRequest(String url, String methGet) {
Request.Builder builder = new Request.Builder()
.url(url);
Request request=null;
switch (methGet){
case METH_GET:
request=builder.get().build();
break;
}
return request;
}
}
view层
public interface FenView {
void viewSuccessUser(List<User.DataBean> list);
void viewSuccessUserBean(List<UserBean.DataBean> beanList);
void viewfail(String fail);
}
model层
public class FenModel {
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
public void login(final String url, final NekWord nekWord){
Okhttp.onGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
User user = gson.fromJson(string, User.class);
List<User.DataBean> data = user.getData();
nekWord.onSuccessUser(data);
}
});
}
});
}
public void login2(String url2,final NekWord nekWord){
Okhttp.onGet(url2, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
UserBean userBean = gson.fromJson(string, UserBean.class);
List<UserBean.DataBean> data = userBean.getData();
nekWord.onSuccessUserBean(data);
}
});
}
});
}
}
Presenter层
public class FenPresenter {
private FenView fenView;
private final FenModel fenModel;
public FenPresenter(FenView fenView) {
this.fenView = fenView;
fenModel = new FenModel();
}
public void login(String url){
fenModel.login(url, new NekWord() {
@Override
public void onSuccessUser(List<User.DataBean> list) {
fenView.viewSuccessUser(list);
}
@Override
public void onSuccessUserBean(List<UserBean.DataBean> beanList) {
}
@Override
public void fail(String fail) {
}
});
}
public void login2(String url2){
fenModel.login2(url2, new NekWord() {
@Override
public void onSuccessUser(List<User.DataBean> list) {
}
@Override
public void onSuccessUserBean(List<UserBean.DataBean> beanList) {
fenView.viewSuccessUserBean(beanList);
}
@Override
public void fail(String fail) {
}
});
}
}
User类
public class User {
/**
* msg : 获取子分类成功
* code : 0
* data : [{"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"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜水果","pcid":2,"pscid":9},{"icon":"http://120.27.23.105/images/icon.png","name":"海鲜水产","pcid":2,"pscid":10},{"icon":"http://120.27.23.105/images/icon.png","name":"精选肉类","pcid":2,"pscid":11},{"icon":"http://120.27.23.105/images/icon.png","name":"蛋类","pcid":2,"pscid":12},{"icon":"http://120.27.23.105/images/icon.png","name":"新鲜蔬菜","pcid":2,"pscid":13},{"icon":"http://120.27.23.105/images/icon.png","name":"冷冻食品","pcid":2,"pscid":14},{"icon":"http://120.27.23.105/images/icon.png","name":"饮品甜品","pcid":2,"pscid":15},{"icon":"http://120.27.23.105/images/icon.png","name":"大闸蟹","pcid":2,"pscid":16}],"name":"京东生鲜","pcid":"2"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"大米","pcid":3,"pscid":21},{"icon":"http://120.27.23.105/images/icon.png","name":"面粉","pcid":3,"pscid":22},{"icon":"http://120.27.23.105/images/icon.png","name":"杂粮","pcid":3,"pscid":23},{"icon":"http://120.27.23.105/images/icon.png","name":"食用油","pcid":3,"pscid":24},{"icon":"http://120.27.23.105/images/icon.png","name":"调味品","pcid":3,"pscid":25},{"icon":"http://120.27.23.105/images/icon.png","name":"方便速食","pcid":3,"pscid":26},{"icon":"http://120.27.23.105/images/icon.png","name":"有机食品","pcid":3,"pscid":27}],"name":"粮油调味","pcid":"3"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"饮用水","pcid":4,"pscid":28},{"icon":"http://120.27.23.105/images/icon.png","name":"饮料","pcid":4,"pscid":29},{"icon":"http://120.27.23.105/images/icon.png","name":"牛奶乳品","pcid":4,"pscid":30},{"icon":"http://120.27.23.105/images/icon.png","name":"名茶","pcid":4,"pscid":31},{"icon":"http://120.27.23.105/images/icon.png","name":"蜂蜜","pcid":4,"pscid":32}],"name":"水饮茗茶","pcid":"4"},{"cid":"1","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"白酒","pcid":5,"pscid":33},{"icon":"http://120.27.23.105/images/icon.png","name":"葡萄酒","pcid":5,"pscid":34},{"icon":"http://120.27.23.105/images/icon.png","name":"洋酒","pcid":5,"pscid":35},{"icon":"http://120.27.23.105/images/icon.png","name":"啤酒","pcid":5,"pscid":36},{"icon":"http://120.27.23.105/images/icon.png","name":"黄酒","pcid":5,"pscid":37},{"icon":"http://120.27.23.105/images/icon.png","name":"陈年老酒","pcid":5,"pscid":38}],"name":"中外名酒","pcid":"5"}]
*/
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;
}
}
}
}
UserBean类
public class UserBean {
/**
* msg : 获取子分类成功
* code : 0
* data : [{"cid":"3","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"耳机/耳麦","pcid":10,"pscid":61},{"icon":"http://120.27.23.105/images/icon.png","name":"音箱/音响","pcid":10,"pscid":62},{"icon":"http://120.27.23.105/images/icon.png","name":"MP3/MP4","pcid":10,"pscid":63},{"icon":"http://120.27.23.105/images/icon.png","name":"便携/无线音箱","pcid":10,"pscid":64},{"icon":"http://120.27.23.105/images/icon.png","name":"麦克风","pcid":10,"pscid":65}],"name":"影音娱乐","pcid":"10"},{"cid":"3","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"游戏手机","pcid":11,"pscid":66},{"icon":"http://120.27.23.105/images/icon.png","name":"拍照手机","pcid":11,"pscid":67},{"icon":"http://120.27.23.105/images/icon.png","name":"大屏手机","pcid":11,"pscid":68},{"icon":"http://120.27.23.105/images/icon.png","name":"老人机","pcid":11,"pscid":69},{"icon":"http://120.27.23.105/images/icon.png","name":"对讲机","pcid":11,"pscid":70}],"name":"手机通讯","pcid":"11"},{"cid":"3","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"蓝牙耳机","pcid":12,"pscid":71},{"icon":"http://120.27.23.105/images/icon.png","name":"移动电源","pcid":12,"pscid":72},{"icon":"http://120.27.23.105/images/icon.png","name":"苹果周边","pcid":12,"pscid":73},{"icon":"http://120.27.23.105/images/icon.png","name":"手机耳机","pcid":12,"pscid":74},{"icon":"http://120.27.23.105/images/icon.png","name":"充电器","pcid":12,"pscid":75},{"icon":"http://120.27.23.105/images/icon.png","name":"数据线","pcid":12,"pscid":76},{"icon":"http://120.27.23.105/images/icon.png","name":"手机电池","pcid":12,"pscid":77},{"icon":"http://120.27.23.105/images/icon.png","name":"保护套","pcid":12,"pscid":78}],"name":"手机配件","pcid":"12"},{"cid":"3","list":[{"icon":"http://120.27.23.105/images/icon.png","name":"单反相机","pcid":13,"pscid":79},{"icon":"http://120.27.23.105/images/icon.png","name":"单电/微单相机","pcid":13,"pscid":80},{"icon":"http://120.27.23.105/images/icon.png","name":"拍立得","pcid":13,"pscid":81},{"icon":"http://120.27.23.105/images/icon.png","name":"数码相机","pcid":13,"pscid":82},{"icon":"http://120.27.23.105/images/icon.png","name":"摄像机","pcid":13,"pscid":83},{"icon":"http://120.27.23.105/images/icon.png","name":"镜头","pcid":13,"pscid":84}],"name":"摄影摄像","pcid":"13"}]
*/
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 : 3
* list : [{"icon":"http://120.27.23.105/images/icon.png","name":"耳机/耳麦","pcid":10,"pscid":61},{"icon":"http://120.27.23.105/images/icon.png","name":"音箱/音响","pcid":10,"pscid":62},{"icon":"http://120.27.23.105/images/icon.png","name":"MP3/MP4","pcid":10,"pscid":63},{"icon":"http://120.27.23.105/images/icon.png","name":"便携/无线音箱","pcid":10,"pscid":64},{"icon":"http://120.27.23.105/images/icon.png","name":"麦克风","pcid":10,"pscid":65}]
* name : 影音娱乐
* pcid : 10
*/
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 : 10
* pscid : 61
*/
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;
}
}
}
}
item1布局
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好"
android:id="@+id/onttext"
android:padding="20dp"/>
</LinearLayout>
item2
<?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">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/twotext"
android:text="商品分类"
android:padding="10dp"
android:layout_weight="1"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recythree"
android:layout_weight="1"
/>
</LinearLayout>
item3
<?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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagethree"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textthree"
android:padding="10dp"
android:text="你好"/>
</LinearLayout>
MyAdpter适配器
public class MyAdapter extends RecyclerView.Adapter{
private Context context;
private List<User.DataBean> list;
public MyAdapter(Context context, List<User.DataBean> list) {
this.context=context;
this.list=list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item1, viewGroup, false);
FirstViewHolder firstViewHolder = new FirstViewHolder(view);
return firstViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof FirstViewHolder){
((FirstViewHolder) viewHolder).textView.setText(list.get(position).getName());
}
}
@Override
public int getItemCount() {
return list.size()==0?0:list.size();
}
public void setData(List<User.DataBean> data) {
this.list = data;
}
class FirstViewHolder extends RecyclerView.ViewHolder{
private final TextView textView;
public FirstViewHolder(final View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.onttext);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemClick.onItem(getAdapterPosition());
}
});
}
}
private ItemClick itemClick;
public interface ItemClick{
void onItem(int data);
}
public void setItemClick(ItemClick itemClick){
this.itemClick=itemClick;
}
}
MyTwoAdpter适配器
public class MyTwoAdpter extends RecyclerView.Adapter {
private Context context;
private List<UserBean.DataBean> list;
public MyTwoAdpter(Context context, List<UserBean.DataBean> beanList) {
this.context = context;
this.list = beanList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item2, viewGroup, false);
TwoViewHolder twoViewHolder = new TwoViewHolder(view);
return twoViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof TwoViewHolder) {
((TwoViewHolder) viewHolder).textView2.setText(list.get(position).getName());
((TwoViewHolder) viewHolder).recyclerView.setLayoutManager(new GridLayoutManager(context, 3, RecyclerView.VERTICAL, false));
MyThreeAdpter myThreeAdpter = new MyThreeAdpter(context, list.get(position).getList());
((TwoViewHolder) viewHolder).recyclerView.setAdapter(myThreeAdpter);
}
}
@Override
public int getItemCount() {
return list.size();
}
public void setData(List<UserBean.DataBean> data) {
this.list = data;
}
class TwoViewHolder extends RecyclerView.ViewHolder {
private final TextView textView2;
private final RecyclerView recyclerView;
public TwoViewHolder(View itemView) {
super(itemView);
textView2 = itemView.findViewById(R.id.twotext);
recyclerView = itemView.findViewById(R.id.recythree);
}
}
}
MyThreeAdpter适配器
public class MyThreeAdpter extends RecyclerView.Adapter{
private Context context;
private List<UserBean.DataBean.ListBean> listBeans;
public MyThreeAdpter(Context context, List<UserBean.DataBean.ListBean> list) {
this.context=context;
this.listBeans=list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item3, viewGroup, false);
ThreeViewHolder threeViewHolder = new ThreeViewHolder(view);
return threeViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof ThreeViewHolder){
((ThreeViewHolder) viewHolder).textView3.setText(listBeans.get(position).getName());
Picasso.with(context).load(listBeans.get(position).getIcon()).into(((ThreeViewHolder) viewHolder).imageView3);
}
}
@Override
public int getItemCount() {
return listBeans.size()==0?0:listBeans.size();
}
class ThreeViewHolder extends RecyclerView.ViewHolder{
private final ImageView imageView3;
private final TextView textView3;
public ThreeViewHolder(View itemView) {
super(itemView);
textView3 = itemView.findViewById(R.id.textthree);
imageView3 = itemView.findViewById(R.id.imagethree);
}
}
}
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"
android:orientation="horizontal"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/recyone"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/recytwo"
/>
</LinearLayout>
主页面mainactivity
public class MainActivity extends AppCompatActivity implements FenView {
private String url = "http://www.zhaoapi.cn/product/getCatagory";
int page = 3;
private String url2 = "http://www.zhaoapi.cn/product/getProductCatagory?cid=";
String url3 = "";
private FenPresenter fenPresenter;
private MyAdapter myAdapter;
private MyTwoAdpter myTwoAdpter;
private RecyclerView recyone;
private RecyclerView recytwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
fenPresenter = new FenPresenter(this);
fenPresenter.login(url);
url3 = url2 + page;
fenPresenter.login2(url3);
myAdapter = new MyAdapter(MainActivity.this, null);
myTwoAdpter = new MyTwoAdpter(this, null);
myAdapter.setItemClick(new MyAdapter.ItemClick() {
@Override
public void onItem(int data) {
url3 = url2 + data;
fenPresenter.login2(url3);
}
});
}
private void initView() {
recyone = findViewById(R.id.recyone);
recytwo = findViewById(R.id.recytwo);
}
@Override
public void viewSuccessUser(List<User.DataBean> list) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyone.setLayoutManager(linearLayoutManager);
myAdapter.setData(list);
recyone.setAdapter(myAdapter);
}
@Override
public void viewSuccessUserBean(List<UserBean.DataBean> beanList) {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
recytwo.setLayoutManager(linearLayoutManager);
myTwoAdpter.setData(beanList);
recytwo.setAdapter(myTwoAdpter);
}
@Override
public void viewfail(String fail) {
}
}
App类
注意在清单文件注册 加权限
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
Okhttp.init();
}
}