创建contrant包,在包内创建Clacontrant 接口
package com.example.dtm2.contrant;
import com.example.dtm2.model.ClaModel;
import java.util.HashMap;
public interface Clacontrant {
abstract class Iclapresenter{
public abstract void clalist(HashMap<String,String>params);
public abstract void claright(HashMap<String,String>params);
}
interface IclaModel{
void clalist(HashMap<String,String>params,ClaModel.IClaModelCallback callback);
void claright(HashMap<String,String>params,ClaModel.IClaModelCallback callback);
}
interface IclaView{
void success(String result);
void Failure(String msg);
void rightsuccess(String result);
void rightFailure(String msg);
}
}
创建Model包,在包内创建ClaModel类
package com.example.dtm2.model;
import com.example.dtm2.api.Api;
import com.example.dtm2.contrant.Clacontrant;
import com.example.dtm2.net.OkHttpCallBack;
import com.example.dtm2.net.OkHttpUtils;
import java.util.HashMap;
import java.util.Map;
public class ClaModel implements Clacontrant.IclaModel {
private String value;
@Override
public void clalist(HashMap<String, String> params, final IClaModelCallback callback) {
OkHttpUtils.getInstance().doGet(Api.HOME_API, new OkHttpCallBack() {
@Override
public void Success(String result) {
callback.success(result);
}
@Override
public void failure(String msg) {
callback.Failure(msg);
}
});
}
//右
@Override
public void claright(HashMap<String, String> params, final IClaModelCallback callback) {
for (Map.Entry<String,String> map:params.entrySet()) {
value = map.getValue();
}
String ww=Api.RIGHT_API+value;
OkHttpUtils.getInstance().doGet(ww, new OkHttpCallBack() {
@Override
public void Success(String result) {
callback.success(result);
}
@Override
public void failure(String msg) {
callback.Failure(msg);
}
});
}
public interface IClaModelCallback {
void success(String result);
void Failure(String msg);
}
}
创建net包,在包内创建OkHttpCallBack接口
public interface OkHttpCallBack {
void Success(String result);
void failure(String msg);
}
接着在net包内创建OkHttpUtils类
package com.example.dtm2.net;
import android.os.Handler;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkHttpUtils {
private static OkHttpUtils mInstance;
private final OkHttpClient okHttpClient;
private Handler handler = new Handler();
public static OkHttpUtils getInstance() {
if (mInstance==null){
synchronized (OkHttpUtils.class){
if (mInstance==null){
mInstance = new OkHttpUtils();
}
}
}
return mInstance;
}
private OkHttpUtils(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.writeTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.build();
}
public void doGet(String url, final OkHttpCallBack okHttpCallback){
Request build = new Request.Builder().url(url).get().build();
okHttpClient.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
okHttpCallback.failure("网络异常");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code()==200){
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
okHttpCallback.Success(string);
}
});
}
}
});
}
public void dopost(String url, HashMap<String,String> params, final OkHttpCallBack callBack){
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String,String> map:params.entrySet()) {
builder.add(map.getKey(),map.getValue());
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder().url(url).post(requestBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (callBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
callBack.failure("网络请求失败");
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (callBack!=null){
if (response.code()==200){
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callBack.Success(result);
}
});
}
}
}
});
}
}
创建persenter包,在包内创建Clapresenter类
package com.example.dtm2.persenter;
import com.example.dtm2.contrant.Clacontrant;
import com.example.dtm2.model.ClaModel;
import java.util.HashMap;
public class Clapresenter extends Clacontrant.Iclapresenter {
private Clacontrant.IclaView iclaView;
private ClaModel claModel;
public Clapresenter(Clacontrant.IclaView iclaView){
this.iclaView=iclaView;
claModel = new ClaModel();
}
@Override
public void clalist(HashMap<String, String> params) {
claModel.clalist(params, new ClaModel.IClaModelCallback() {
@Override
public void success(String result) {
iclaView.success(result);
}
@Override
public void Failure(String msg) {
iclaView.Failure(msg);
}
});
}
//右
@Override
public void claright(HashMap<String, String> params) {
claModel.claright(params, new ClaModel.IClaModelCallback() {
@Override
public void success(String result) {
iclaView.rightsuccess(result);
}
@Override
public void Failure(String msg) {
iclaView.rightFailure(msg);
}
});
}
}
Api
package com.example.dtm2.api;
public class Api {
public static final String HOME_API = "http://172.17.8.100/small/commodity/v1/findFirstCategory";
public static final String RIGHT_API = "http://172.17.8.100/small/commodity/v1/findSecondCategory?firstCategoryId=";
}
Bean
package com.example.dtm2.bean;
import java.util.List;
public class UserBean {
public List<ResultEntity> result;
public String message;
public String status;
public class ResultEntity {
public String name;
public String id;
}
}
右
package com.example.dtm2.bean;
import java.util.List;
public class RightBean {
public List<ResultBean> result;
public static class ResultBean {
public String id;
public String name;
}
}
Adapter左适配器
package com.example.dtm2.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.dtm2.R;
import com.example.dtm2.bean.UserBean;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.Myvh>{
private Context context;
private List<UserBean.ResultEntity> list;
public Adapter (Context context){
this.context=context;
list=new ArrayList<>();
}
public void setList(List<UserBean.ResultEntity> list){
this.list=list;
notifyDataSetChanged();
}
@NonNull
@Override
public Adapter.Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.Myvh holder, final int position) {
holder.title.setText(list.get(position).name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onclicklisener!=null){
onclicklisener.click(list.get(position).id);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder{
private final TextView title;
public Myvh(View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
}
}
private onclicklisener onclicklisener;
public void setOnclicklisener(Adapter.onclicklisener onclicklisener) {
this.onclicklisener = onclicklisener;
}
public interface onclicklisener{
void click(String cid);
}
}
右适配器
package com.example.dtm2.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.dtm2.R;
import com.example.dtm2.bean.RightBean;
import java.util.ArrayList;
import java.util.List;
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.Myvh> {
private Context context;
private List<RightBean.ResultBean> list;
public RightAdapter(Context context) {
this.context = context;
list = new ArrayList<>();
}
public void setList(List<RightBean.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
@NonNull
@Override
public RightAdapter.Myvh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(context).inflate(R.layout.right_item,parent,false);
return new Myvh(view);
}
@Override
public void onBindViewHolder(@NonNull RightAdapter.Myvh holder, int position) {
holder.title.setText(list.get(position).name);
Glide.with(context).load("http://365jia.cn/uploads/appletrecommend/201804/5ad6ba6c6bb8b.jpg").into(holder.imageView);
}
@Override
public int getItemCount() {
return list.size();
}
public class Myvh extends RecyclerView.ViewHolder {
private final ImageView imageView;
private final TextView title;
public Myvh(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
}
}
}
主页面
package com.example.dtm2.fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.dtm2.MainActivity;
import com.example.dtm2.R;
import com.example.dtm2.Ss;
import com.example.dtm2.adapter.Adapter;
import com.example.dtm2.adapter.RightAdapter;
import com.example.dtm2.bean.RightBean;
import com.example.dtm2.bean.UserBean;
import com.example.dtm2.contrant.Clacontrant;
import com.example.dtm2.persenter.Clapresenter;
import com.google.gson.Gson;
import java.util.HashMap;
public class FragmentA extends Fragment implements Clacontrant.IclaView,Adapter.onclicklisener{
private RecyclerView recyclerView;
private Adapter adapter;
private Clapresenter clapresenter;
private RecyclerView recyclerView1;
private RightAdapter rightAdapter;
private Ss ss;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView=view.findViewById(R.id.rexc);
ss = view.findViewById(R.id.ss);
recyclerView1 = view.findViewById(R.id.rightrexc);
recyclerView1.setLayoutManager(new GridLayoutManager(getActivity(),3));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter=new Adapter(getActivity());
rightAdapter = new RightAdapter(getActivity());
clapresenter=new Clapresenter(this);
HashMap<String,String>map=new HashMap<>();
clapresenter.clalist(map);
}
@Override
public void click(String cid) {
getLeft(cid);
}
public void getLeft(String cid){
HashMap<String,String> params=new HashMap<>();
params.put("firstCategoryId",cid);
clapresenter.claright(params);
}
@Override
public void success(String result) {
UserBean userBean = new Gson().fromJson(result, UserBean.class);
adapter.setList(userBean.result);
adapter.setOnclicklisener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void Failure(String msg) {
}
@Override
public void rightsuccess(String result) {
RightBean rightBean=new Gson().fromJson(result,RightBean.class);
rightAdapter.setList(rightBean.result);
recyclerView1.setAdapter(rightAdapter);
}
@Override
public void rightFailure(String msg) {
}
}