fragment
package com.example.administrator.monthmn.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.Toast; import com.example.administrator.monthmn.R; import com.example.administrator.monthmn.adapter.EMyAdapter; import com.example.administrator.monthmn.adapter.MyAdapter; import com.example.administrator.monthmn.bean.EBean; import com.example.administrator.monthmn.bean.FBean; import com.example.administrator.monthmn.model.FModel; import com.example.administrator.monthmn.presenter.FPresenter; import com.example.administrator.monthmn.view.FView; import java.util.List; /** * Created by Administrator on 2018/6/29. */ public class Fragment2 extends Fragment implements FView, MyAdapter.OnItemClickListener { private static final String TAG = "Fragment2"; private RecyclerView recyclerView; private ExpandableListView eListView; private MyAdapter.OnItemClickListener onItemClickListener; private MyAdapter myAdapter; private FPresenter fPresenter; private EMyAdapter eMyAdapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, container, false); recyclerView = view.findViewById(R.id.recyclerView); eListView = view.findViewById(R.id.EListView); //setOnItemClickListener(onItemClickListener); fPresenter = new FPresenter(this); fPresenter.Home(1); return view; } @Override public void getSuccess(FBean fBean) { Listlist = fBean.getData(); myAdapter = new MyAdapter(list,getActivity()); myAdapter.setOnItemClickListener(this); LinearLayoutManager linearLayoutManager =new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(myAdapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); // onItemClickListener.onClick(); } @Override public void getSuccess2(EBean eBena) { Log.d(TAG, "getSuccess2: "+eBena.getData()); List list2 = eBena.getData(); eMyAdapter = new EMyAdapter(list2,getActivity()); // Toast.makeText(getContext(),eMyAdapter.getGroupCount()+"=====",Toast.LENGTH_SHORT).show(); eListView.setAdapter(eMyAdapter); int count = eListView.getCount(); for (int i = 0; i < count; i++) { eListView.expandGroup(i); } eMyAdapter.notifyDataSetChanged(); } // //外部访问接口的方法 // public void setOnItemClickListener(MyAdapter.OnItemClickListener onItemClickListener){ // this.onItemClickListener = onItemClickListener; // } @Override public void onClick(int cid) { Toast.makeText(getContext(),""+cid,Toast.LENGTH_LONG).show(); fPresenter.Home(cid); } }
model层
package com.example.administrator.monthmn.model; import android.content.Context; import android.util.Log; import com.example.administrator.monthmn.bean.EBean; import com.example.administrator.monthmn.bean.FBean; import com.example.administrator.monthmn.util.OkHttpUtil; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; /** * Created by Administrator on 2018/6/29. */ public class FModel { private static final String TAG = "FModel"; private String url ="https://www.zhaoapi.cn/product/getCatagory"; private String url2="https://www.zhaoapi.cn/product/getProductCatagory"; private Context context; public void getUrl(final IModel iModel){ Mapmap =new HashMap<>(); OkHttpUtil okHttpUtil = OkHttpUtil.getInstance(context); okHttpUtil.getData(url, new OkHttpUtil.ICallBack() { @Override public void Data(String result) { Gson gson = new Gson(); FBean fBean = gson.fromJson(result, FBean.class); iModel.getSuccess(fBean); } }); } public void getUrl2(int cid, final IModel2 iModel2){ Map map =new HashMap<>(); OkHttpUtil okHttpUtil = OkHttpUtil.getInstance(context); okHttpUtil.getData(url2+"?cid="+cid, new OkHttpUtil.ICallBack() { @Override public void Data(String result) { Gson gson = new Gson(); EBean eBean = gson.fromJson(result, EBean.class); Log.d(TAG, "Data: "+eBean.getCode()); Log.d(TAG, "Data: "+eBean.getMsg()); Log.d("111",eBean+""); iModel2.getSuccess2(eBean); } }); } public interface IModel{ void getSuccess(FBean fBean); } public interface IModel2{ void getSuccess2(EBean eBean); } }
p层
package com.example.administrator.monthmn.presenter; import com.example.administrator.monthmn.bean.EBean; import com.example.administrator.monthmn.bean.FBean; import com.example.administrator.monthmn.model.FModel; import com.example.administrator.monthmn.view.FView; /** * Created by Administrator on 2018/6/29. */ public class FPresenter { private FView mfView; private final FModel fModel; public FPresenter(FView fView){ this.mfView=fView; fModel = new FModel(); } public void Home(int cid){ fModel.getUrl(new FModel.IModel() { @Override public void getSuccess(FBean fBean) { mfView.getSuccess(fBean); } }); fModel.getUrl2(cid,new FModel.IModel2() { @Override public void getSuccess2(EBean eBean) { mfView.getSuccess2(eBean); } }); } }
v层
package com.example.administrator.monthmn.view; import com.example.administrator.monthmn.bean.EBean; import com.example.administrator.monthmn.bean.FBean; /** * Created by Administrator on 2018/6/29. */ public interface FView { void getSuccess(FBean fBean); void getSuccess2(EBean eBena); }
adapter适配器左
package com.example.administrator.monthmn.adapter; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; 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.administrator.monthmn.R; import com.example.administrator.monthmn.bean.FBean; import java.util.List; /** * Created by Administrator on 2018/6/29. */ public class MyAdapter extends RecyclerView.Adapter{ private List list; private FragmentActivity activity; private OnItemClickListener onItemClickListener; public MyAdapter(List list, FragmentActivity activity) { this.list = list; this.activity = activity; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(activity).inflate(R.layout.r_layout, parent, false); MyViewHolder myViewHolder = new MyViewHolder(itemView); return myViewHolder; } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) { holder.r_text.setText(list.get(position).getName()); holder.r_text.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //接口对象 onItemClickListener.onClick(list.get(position).getCid()); } }); } public void setOnItemClickListener(OnItemClickListener callBcak){ this.onItemClickListener = callBcak; } @Override public int getItemCount() { return list==null?0:list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder{ private final TextView r_text; public MyViewHolder(View itemView) { super(itemView); r_text = itemView.findViewById(R.id.r_text); } } public interface OnItemClickListener{ //定义点击方法 void onClick(int cid); } }
adapter适配器右
package com.example.administrator.monthmn.adapter; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.example.administrator.monthmn.R; import com.example.administrator.monthmn.bean.EBean; import com.example.administrator.monthmn.bean.FBean; import org.w3c.dom.Text; import java.util.List; /** * Created by Administrator on 2018/6/29. */ public class EMyAdapter extends BaseExpandableListAdapter { private Listlist2; private FragmentActivity activity; private List recyclerView_list; public EMyAdapter(List list2, FragmentActivity activity) { this.list2=list2; this.activity=activity; } @Override public int getGroupCount() { return list2==null?0:list2.size(); } @Override public int getChildrenCount(int groupPosition) { return list2.get(groupPosition)==null?0:list2.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return list2.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return list2.get(groupPosition).getList().get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { MyViewHolder1 myViewHolder1; if(convertView==null){ convertView = View.inflate(activity, R.layout.e_r_1_layout, null); myViewHolder1 = new MyViewHolder1(); myViewHolder1.textView=convertView.findViewById(R.id.text_view1); convertView.setTag(myViewHolder1); }else{ myViewHolder1 = (MyViewHolder1) convertView.getTag(); } myViewHolder1.textView.setText(list2.get(groupPosition).getName()); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { MyViewHolder2 myViewHolder2; if(convertView==null){ convertView = View.inflate(activity,R.layout.e_layout,null); myViewHolder2 = new MyViewHolder2(); myViewHolder2.recyclerView = convertView.findViewById(R.id.recyclerView2); convertView.setTag(myViewHolder2); }else{ myViewHolder2 = (MyViewHolder2) convertView.getTag(); } recyclerView_list = list2.get(groupPosition).getList(); RAdapter rAdapter =new RAdapter(groupPosition,childPosition,activity); GridLayoutManager gridLayoutManager =new GridLayoutManager(activity,3); myViewHolder2.recyclerView.setLayoutManager(gridLayoutManager); myViewHolder2.recyclerView.setAdapter(rAdapter); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class MyViewHolder1{ TextView textView; } class MyViewHolder2{ RecyclerView recyclerView; } class RAdapter extends RecyclerView.Adapter { private static final String TAG = "RAdapter"; private int childPosition; private int groupPosition; private FragmentActivity activity; public RAdapter(int groupPosition, int childPosition, FragmentActivity activity) { this.childPosition=childPosition; this.activity=activity; this.groupPosition=groupPosition; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(activity).inflate(R.layout.e_r_layout, parent, false); MyViewHolder myViewHolder =new MyViewHolder(itemView); return myViewHolder; } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.textView.setText(recyclerView_list.get(position).getName()); Glide.with(activity).load(recyclerView_list.get(position).getIcon()).into(holder.imageView); } @Override public int getItemCount() { return recyclerView_list==null?0:recyclerView_list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder{ private final ImageView imageView; private final TextView textView; public MyViewHolder(View itemView) { super(itemView); imageView = itemView.findViewById(R.id.image_view2); textView = itemView.findViewById(R.id.text_view2); } } } }
OKHTTP
package com.example.administrator.monthmn.util; import android.content.Context; import android.os.Handler; import android.os.Looper; import java.io.IOException; 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.Response; /** * Created by Administrator on 2018/6/29. */ public class OkHttpUtil { private Context context; private final Handler handler; private static OkHttpUtil okHttpUtil; private OkHttpClient okHttpClient; private OkHttpUtil(Context context){ this.context=context; handler = new Handler(Looper.getMainLooper()); initOk(); } private void initOk() { okHttpClient = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(1000,TimeUnit.MILLISECONDS) .connectTimeout(5,TimeUnit.SECONDS) .build(); } public static OkHttpUtil getInstance(Context context){ if(okHttpUtil==null){ synchronized (OkHttpUtil.class){ if(okHttpUtil==null){ okHttpUtil = new OkHttpUtil(context); } } } return okHttpUtil; } public void getData(String url, final ICallBack iCallBack){ Request request = new Request.Builder() .url(url) .get() .build(); okHttpClient.newCall(request).enqueue(new Callback() { private String result; @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ result = response.body().string(); handler.post(new Runnable() { @Override public void run() { iCallBack.Data(result); } }); } } }); } private void postData(String url, Mapmap, final ICallBack iCallBack){ FormBody.Builder formBody = new FormBody.Builder(); for(Map.Entry stringStringEntry:map.entrySet()){ formBody.add(stringStringEntry.getKey(),stringStringEntry.getValue()); } Request request =new Request.Builder() .url(url) .post(formBody.build()) .build(); okHttpClient.newCall(request).enqueue(new Callback() { private String result; @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ result = response.body().string(); handler.post(new Runnable() { @Override public void run() { iCallBack.Data(result); } }); } } }); } public interface ICallBack{ void Data(String result); } }