flFragment
public class flFragment extends Fragment implements ClsContract.IclsView ,LeftAdapter.ItemClickListener{
private ClsPresennter clsPresennter;
private LeftAdapter leftAdapter;
@BindView(R.id.left_rv)
RecyclerView leftRv;
@BindView(R.id.right_rv)
RecyclerView rightRv;
private String cid = "1";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_fl, container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
private void initData() {
leftRv.setLayoutManager(new LinearLayoutManager(getActivity()));
rightRv.setLayoutManager(new LinearLayoutManager(getActivity()));
clsPresennter = new ClsPresennter(this);
getLeft();
clsPresennter.getLeftList(new HashMap());
}
public void getLeft(){
HashMap p2 = new HashMap<>();
p2.put("cid", cid);
clsPresennter.getRightList(p2);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void leftSucess(List leftClsBeanList) {
if (leftAdapter==null){
leftAdapter = new LeftAdapter(leftClsBeanList,getActivity());
leftRv.setAdapter(leftAdapter);
leftAdapter.setItemClickListener(this);
}else{
leftAdapter.notifyDataSetChanged();
}
}
@Override
public void rightSucess(List rightClsBeanList) {
rightRv.setAdapter(new RightAdapter(rightClsBeanList,getActivity()));
}
@Override
public void leftFailure(String error) {
}
@Override
public void rightFailure(String error) {
}
@Override
public void click(String cid) {
getLeft();
}
}
Api
public class Api {
public static final String BASE_URL = “http://www.zhaoapi.cn/”;
}
ClsApi
public class ClsApi {
public static final String LEFT_URL = Api.BASE_URL+“product/getCatagory”;
public static final String RIGHT_URL = Api.BASE_URL+“product/getProductCatagory”;
}
bean类
LeftClsBean
public class LeftClsBean {
public String code;
public String msg;
public List data;
public class Cls{
public String name;
public String cid;
}
}
RightClsBean
public class RightClsBean {
public String code;
public String msg;
public List data;
public class Cls{
public String pcid;
public String name;
public List list;
public class Pcls{
public String name;
public String icon;
}
}
}
ClsPresenter
public interface ClsContract {
abstract class ClsPresenter{
public abstract void getLeftList(HashMap
public abstract void getRightList(HashMap
}
public interface IClsModel{
void getLeftList(HashMap params, ClsModel.ClsModelCallback clsModelCallback);
void getRightList(HashMap params, ClsModel.ClsModelCallback clsModelCallback);
}
public interface IclsView{
void leftSucess(List leftClsBeanList);
void rightSucess(List rightClsBeanList);
void leftFailure(String error);
void rightFailure(String error);
}
}
OkhttpUtils
public class OkhttpUtils {
private Handler handler = new Handler() ;
private OkHttpClient okHttpClient;
private static OkhttpUtils 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 static OkhttpUtils getInstance() {
if (mInstance == null) {
synchronized (OkhttpUtils.class) {
if (mInstance == null) {
mInstance = new OkhttpUtils();
}
}
}
return mInstance;
}
//get请求方式
public void doGet( String url, HashMap parmas, final OkhttpCallback requestCallback) {
StringBuilder p = new StringBuilder();
if (parmas!=null&&parmas.size()>0){
for (Map.Entry map : parmas.entrySet()) {
p.append(map.getKey()).append("=").append(map.getValue()).append("&");
}
System.out.println("ppppppp===="+p.toString());
}
Request request = new Request.Builder().url(url+"?"+p.toString())
.get().build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (requestCallback != null) {
handler.post(new Runnable() {
@Override
public void run() {
requestCallback.failure("网络异常");
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String result = response.body().string();
if (requestCallback != null) {
handler.post(new Runnable() {
@Override
public void run() {
requestCallback.success(result);
}
});
}
}
});
}
//Post请求方式
public void doPost(String url, HashMap parmas, final OkhttpCallback callback) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry map : parmas.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, final Response response) throws IOException {
if (callback != null) {
if (200 == response.code()) {
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callback.success(result);
}
});
}
}
}
});
}
//上传头像,文件
public void uploadFile(String url, HashMap params, final OkhttpCallback okhttpCallback) {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
for (Map.Entry map : params.entrySet()) {
String key = map.getKey();
Object value = map.getValue();
if (value instanceof File) {
File file = (File) value;
builder.addFormDataPart(key, file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
} else {
builder.addFormDataPart(key, value.toString());
}
}
Request request = new Request.Builder().url(url).post(builder.build()).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (okhttpCallback != null) {
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallback.failure("网络异常");
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (okhttpCallback != null) {
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
okhttpCallback.success(result);
}
});
}
}
});
}
//取消所有请求
public void cancelAllTask() {
if (okHttpClient != null) {
okHttpClient.dispatcher().cancelAll();
}
}
}
OkhttpCallback
public interface OkhttpCallback {
void failure(String msg);
void success(String result);
}
ClsModel
public class ClsModel implements ClsContract.IClsModel{
@Override
public void getLeftList( HashMap
OkhttpUtils.getInstance().doPost(ClsApi.LEFT_URL, params, new OkhttpCallback() {
@Override
public void failure(String msg) {
if (clsModelCallback!=null)
clsModelCallback.leftFailure(msg);
}
@Override
public void success(String result) {
if (clsModelCallback!=null){
clsModelCallback.leftSuccess(result);
}
}
});
}
@Override
public void getRightList( HashMap params, final ClsModelCallback clsModelCallback) {
OkhttpUtils.getInstance().doPost(ClsApi.RIGHT_URL, params, new OkhttpCallback() {
@Override
public void failure(String msg) {
if (clsModelCallback!=null)
clsModelCallback.rightSUccess(msg);
}
@Override
public void success(String result) {
if (clsModelCallback!=null){
clsModelCallback.rightSUccess(result);
}
}
});
}
public interface ClsModelCallback{
void leftSuccess(String result);
void rightSUccess(String result);
void leftFailure(String error);
void rightFailure(String error);
}
}
ClsPresennter
public class ClsPresennter extends ClsContract.ClsPresenter {
private ClsModel clsModel;
private ClsContract.IclsView view;
public ClsPresennter( ClsContract.IclsView view) {
this.clsModel = new ClsModel();
this.view = view;
}
@Override
public void getLeftList(HashMap params) {
clsModel.getLeftList(params, new ClsModel.ClsModelCallback() {
@Override
public void leftSuccess(String result) {
LeftClsBean leftClsBean = new Gson().fromJson(result,LeftClsBean.class);
List data = leftClsBean.data;
view.leftSucess(data);
}
@Override
public void rightSUccess(String result) {
}
@Override
public void leftFailure(String error) {
}
@Override
public void rightFailure(String error) {
}
});
}
@Override
public void getRightList(HashMap params) {
clsModel.getRightList(params, new ClsModel.ClsModelCallback() {
@Override
public void leftSuccess(String result) {
}
@Override
public void rightSUccess(String result) {
RightClsBean rightClsBean = new Gson().fromJson(result,RightClsBean.class);
List data = rightClsBean.data;
view.rightSucess(data);
}
@Override
public void leftFailure(String error) {
}
@Override
public void rightFailure(String error) {
}
});
}
}
LeftAdapter
public class LeftAdapter extends RecyclerView.Adapter
private List
private Context context;
public LeftAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public LeftViewHolder onCreateViewHolder( @NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.left_item,viewGroup,false);
LeftViewHolder leftViewHolder = new LeftViewHolder(view);
return leftViewHolder;
}
@Override
public void onBindViewHolder(@NonNull LeftViewHolder leftViewHolder, final int pos) {
final LeftClsBean.Cls cls = list.get(pos);
leftViewHolder.fname.setText(cls.name);
leftViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (itemClickListener!=null){
itemClickListener.click(cls.cid);
}
}
});
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class LeftViewHolder extends RecyclerView.ViewHolder{
private TextView fname;
public LeftViewHolder(@NonNull View itemView) {
super(itemView);
fname = itemView.findViewById(R.id.ftitle);
}
}
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public interface ItemClickListener{
void click(String cid);
}
}
RightAdapter
public class RightAdapter extends RecyclerView.Adapter{
private List list;
private Context context;
public RightAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RightAdapter.RightViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.right_item,viewGroup,false);
RightAdapter.RightViewHolder leftViewHolder = new RightAdapter.RightViewHolder(view);
return leftViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RightAdapter.RightViewHolder rightViewHolder, int pos) {
RightClsBean.Cls bean = list.get(pos);
List list = bean.list;
rightViewHolder.nameTv.setText(bean.name);
//设置子分类列表适配器
rightViewHolder.rv.setLayoutManager(new GridLayoutManager(context,3));
rightViewHolder.rv.setAdapter(new RightItemAdapter(list,context));
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class RightViewHolder extends RecyclerView.ViewHolder{
private TextView nameTv;
private RecyclerView rv;
public RightViewHolder(@NonNull View itemView) {
super(itemView);
nameTv = itemView.findViewById(R.id.name);
rv = itemView.findViewById(R.id.re);
}
}
}
RightItemAdapter
public class RightItemAdapter extends RecyclerView.Adapter{
private List list;
private Context context;
public RightItemAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RightItemAdapter.RightViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.right_subitem,viewGroup,false);
RightItemAdapter.RightViewHolder leftViewHolder = new RightItemAdapter.RightViewHolder(view);
return leftViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RightItemAdapter.RightViewHolder rightViewHolder, int pos) {
RightClsBean.Cls.Pcls bean = list.get(pos);
rightViewHolder.nameTv.setText(bean.name);
Glide.with(context).load(bean.icon).into(rightViewHolder.iv);
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
class RightViewHolder extends RecyclerView.ViewHolder{
private TextView nameTv;
private ImageView iv;
public RightViewHolder(@NonNull View itemView) {
super(itemView);
nameTv = itemView.findViewById(R.id.name);
iv = itemView.findViewById(R.id.img);
}
}
}