activity_main.xml
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/la" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <TextView android:id="@+id/sy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:textSize="20dp" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/fl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分类" android:textSize="20dp" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/gwc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="购物车" android:layout_weight="1" android:gravity="center" android:textSize="20dp" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vp_s" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/la" ></android.support.v4.view.ViewPager> </RelativeLayout> </LinearLayout>
item_fl.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/rec_left" 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/right_lnl" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"></LinearLayout> </ScrollView> </LinearLayout> </android.support.constraint.ConstraintLayout>
item_list.xml
<?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:orientation="vertical" > <TextView android:id="@+id/txt_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="haha" android:textSize="20dp" android:padding="10dp" /> </LinearLayout>
item_right.xml
<?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:orientation="vertical" > <ImageView android:id="@+id/img_right" android:layout_width="75dp" android:layout_height="75dp" android:padding="10dp" /> <TextView android:id="@+id/txt_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" /> </LinearLayout>
创建FenLeiLeft(Inter<IView>,LeftAdapter<LeftAdapter>,LeftModel<LeftModel>,LeftPresenter<LeftPresenter>)right
Inter<IView>,
public interface IView { void getleft(List<LeftBean.DataBean> list); void failed(Exception e); }
LeftAdapter<LeftAdapter>
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.ViewHolder>{ private Context context; private List<LeftBean.DataBean> mList; public LeftAdapter(Context context, List<LeftBean.DataBean> list) { this.context = context; mList = list; } public interface OnItemClickListener{ void onItemClick(View itemview,int position); } private OnItemClickListener clickListener; public void setOnItemClickListener(OnItemClickListener clickListener){ this.clickListener = clickListener; } @NonNull @Override public LeftAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = View.inflate(context,R.layout.item_list,null); LeftAdapter.ViewHolder holder = new LeftAdapter.ViewHolder(v); return holder; } @Override public void onBindViewHolder(@NonNull LeftAdapter.ViewHolder holder, final int position) { holder.txtleft.setText(mList.get(position).getName()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (clickListener !=null){ clickListener.onItemClick(view,position); } } }); } @Override public int getItemCount() { return mList.size(); } class ViewHolder extends RecyclerView.ViewHolder{ private TextView txtleft; public ViewHolder(View itemview){ super(itemview); txtleft = itemview.findViewById(R.id.txt_left); } } }
LeftModel<LeftModel>
public class LeftModel { public void getlefts(String url, IcallBack icallBack, Type type){ HttpUtils.getInstance().get(url, icallBack,type); } }
LeftPresenter<LeftPresenter>
public class LeftPresenter { private IView iv; private LeftModel mLeftModel; public void attch(final IView iv){ this.iv = iv; mLeftModel = new LeftModel(); } public void getleft(){ Type type = new TypeToken<LeftBean>(){}.getType(); mLeftModel.getlefts("http://www.zhaoapi.cn/product/getCatagory", new IcallBack() { @Override public void onSunness(Object obj) { LeftBean leftBean = (LeftBean) obj; if (leftBean!=null){ iv.getleft(leftBean.getData()); } } @Override public void onfailed(Exception e) {iv.failed(e); } },type); } public void detach(){ if (iv != null){ iv = null; } } }
Right///Inter<IVew>
public interface IVew { void getrights(List<RightBean.DataBean> list); void failed(Exception e); }
RightAdapter<RightAdapter>
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder>{ private Context context; private List<RightBean.DataBean.ListBean> mList; public RightAdapter(Context context, List<RightBean.DataBean.ListBean> list) { this.context = context; mList = list; } @NonNull @Override public RightAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item_right,null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull RightAdapter.ViewHolder holder, int position) { Glide.with(context).load(mList.get(position).getIcon()).into(holder.imageright); holder.textright.setText(mList.get(position).getName()); } @Override public int getItemCount() { return mList.size(); } class ViewHolder extends RecyclerView.ViewHolder { private ImageView imageright; private TextView textright; public ViewHolder(View itemView) { super(itemView); imageright = itemView.findViewById(R.id.img_right); textright = itemView.findViewById(R.id.txt_right); } } }
RightModel<RightModel>
public class RightModel { public void getrights(String url, IcallBack icallBack,Type Type){ HttpUtils.getInstance().get(url, icallBack,Type); } }
RightPreaenter<RightPreaenter>
public class RightPreaenter { private IVew iv; private RightModel mRightModel; public void attch(IVew iv){ this.iv = iv; mRightModel = new RightModel(); } public void getright(String url){ Type type = new TypeToken<RightBean>(){}.getType(); mRightModel.getrights(url, new IcallBack() { @Override public void onSunness(Object obj) { RightBean rightBean = (RightBean) obj; if (rightBean != null){ iv.getrights(rightBean.getData()); } } @Override public void onfailed(Exception e) { iv.failed(e); } },type); } public void detach(){ if (iv != null){ iv = null; } } }
Fragment<FlFragment>
public class FlFragment extends Fragment implements IView, com.example.yuekaolianxi2018.FenLei.Right.Inter.IVew{ private RecyclerView recleft; private LeftAdapter mLeftAdapter; private List<LeftBean.DataBean> mBeanList; private LeftPresenter mLeftPresenter; private LinearLayout layoutringht; private RightAdapter rightAdapter; private RightPreaenter rightPreaenter; private List<RightBean.DataBean> beanright; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.item_fl,container,false); recleft = view.findViewById(R.id.rec_left); layoutringht = view.findViewById(R.id.right_lnl); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),1, LinearLayoutManager.VERTICAL,false); recleft.setLayoutManager(layoutManager); mBeanList = new ArrayList<>(); beanright = new ArrayList<>(); mLeftAdapter = new LeftAdapter(getActivity(),mBeanList); mLeftAdapter.setOnItemClickListener(new LeftAdapter.OnItemClickListener() { @Override public void onItemClick(View itemview, int position) { LeftBean.DataBean dataBean = mBeanList.get(position); rightPreaenter.getright("http://www.zhaoapi.cn/product/getProductCatagory?cid="+dataBean.getCid()); } }); recleft.setAdapter(mLeftAdapter); mLeftPresenter = new LeftPresenter(); mLeftPresenter.attch(this); mLeftPresenter.getleft(); rightPreaenter = new RightPreaenter(); rightPreaenter.attch(this); } @Override public void getleft(List<LeftBean.DataBean> list) { if (list != null){ mBeanList.clear();; mBeanList.addAll(list); mLeftAdapter.notifyDataSetChanged(); } } @Override public void getrights(List<RightBean.DataBean> list) { if (list != null){ layoutringht.removeAllViews(); for (int i = 0; i<list.size();i++){ TextView textView = new TextView(getActivity()); textView.setText(list.get(i).getName()); RecyclerView rvNextb = new RecyclerView(getActivity()); RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),3); rvNextb.setLayoutManager(layoutManager); rightAdapter = new RightAdapter(getActivity(),list.get(i).getList()); rvNextb.setAdapter(rightAdapter); beanright.clear(); beanright.addAll(list); rightAdapter.notifyDataSetChanged(); layoutringht.addView(textView); layoutringht.addView(rvNextb); } } } @Override public void failed(Exception e) { } @Override public void onDestroy() { super.onDestroy(); if (mLeftPresenter != null){ mLeftPresenter.detach(); } } @Override public void onResume() { super.onResume(); rightPreaenter.getright("http://www.zhaoapi.cn/product/getProductCatagory?cid=1"); } }
Utils<HttpUtils>
public class HttpUtils { private static volatile HttpUtils instance; private OkHttpClient mClient; private Handler mHandler = new Handler(); public HttpUtils(){ mClient = 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 icallBack, final Type type){ Request request = new Request.Builder().get().url(url).build(); Call call = mClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, final IOException e) { mHandler.post(new Runnable() { @Override public void run() { icallBack.onfailed(e); } }); } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); Gson gson = new Gson(); final Object o = gson.fromJson(result,type); mHandler.post(new Runnable() { @Override public void run() { icallBack.onSunness(o); } }); } }); } }
Utils<IcallBack>
public interface IcallBack { void onSunness(Object obj); void onfailed(Exception e); }
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private TextView textView1; private TextView textView2; private TextView textView3; private FlFragment flFragment; private GwcFragment gwcFragment; private ShouYeFragment shouYeFragment; private List<Fragment> list; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView3 = findViewById(R.id.sy); textView1 = findViewById(R.id.fl); textView2 = findViewById(R.id.gwc); textView3.setOnClickListener(this); textView1.setOnClickListener(this); textView2.setOnClickListener(this); flFragment = new FlFragment(); gwcFragment = new GwcFragment(); shouYeFragment = new ShouYeFragment(); list = new ArrayList<>(); list.add(flFragment); list.add(gwcFragment); list.add(shouYeFragment); viewPager = findViewById(R.id.vp_s); viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.fl: viewPager.setCurrentItem(0); break; case R.id.gwc: viewPager.setCurrentItem(1); break; case R.id.sy: viewPager.setCurrentItem(2); break; } } }