--------------------------------------分类主页面布局文件-------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#ff0" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:layout_weight="2" android:id="@+id/recycler_left" android:layout_width="0dp" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> <FrameLayout android:layout_weight="8" android:id="@+id/frame_right" android:layout_width="0dp" android:layout_height="match_parent"></FrameLayout> </LinearLayout>
--------------------------------------分类Fragment----------------------------------------------
package com.example.day0629_moni_yk.view.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; 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 android.widget.FrameLayout; import android.widget.Toast; import com.example.day0629_moni_yk.R; import com.example.day0629_moni_yk.adapter.FenLeiAdapterLeft; import com.example.day0629_moni_yk.bean.Constant; import com.example.day0629_moni_yk.bean.GsonFenLeiLeftData; import com.example.day0629_moni_yk.util.RequestDataUtil; import com.google.gson.Gson; import org.greenrobot.eventbus.EventBus; import java.util.List; /** * Created by John on 2018/6/29 0029. */ public class FragmentFenClass extends Fragment { private View view; private RecyclerView mRecyclerLeft; private RequestDataUtil dataUtil; private FragmentShoppingCartRigth fragmentRigth; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_fenlei, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(view); //请求左侧数据 requestLeft(); } //请求左边的数据 private void requestLeft() { dataUtil.getRequestJsonData(Constant.FenLeiUrl, new RequestDataUtil.IcallBack() { @Override public void onRequest(String jsonData) { //请求成功 Gson gson = new Gson(); GsonFenLeiLeftData leftData = gson.fromJson(jsonData, GsonFenLeiLeftData.class); final List<GsonFenLeiLeftData.DataBean> leftList = leftData.getData(); //设置布局管理器 mRecyclerLeft.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false)); //设置适配器 FenLeiAdapterLeft adapterLeft = new FenLeiAdapterLeft(getContext(),leftList); mRecyclerLeft.setAdapter(adapterLeft); //接口回调 adapterLeft.setOnItemClickListener(new FenLeiAdapterLeft.LeftIcallBack() { @Override public void onItemClick(int position) { //动态加载右边的Fragment fragmentRigth = new FragmentShoppingCartRigth(); Bundle bundle = new Bundle(); bundle.putString("cid",leftList.get(position).getCid()+""); fragmentRigth.setArguments(bundle); getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_right, fragmentRigth).commit(); Toast.makeText(getContext(), ""+position, Toast.LENGTH_SHORT).show(); } }); } }); } private void initView(View view) { mRecyclerLeft = (RecyclerView) view.findViewById(R.id.recycler_left); //得到网络请求工具类 dataUtil = RequestDataUtil.getInstance(); //动态加载右边的Fragment 这个是设置默认加载一个页面 fragmentRigth = new FragmentShoppingCartRigth(); getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_right, fragmentRigth).commit(); } }
------------------------------------右边显示数据的Fragment的布局------------------------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/fenlei_reight_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="18sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/fenlei_reight_content" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout>
------------------------------------右边显示数据的Fragment------------------------------------------------
package com.example.day0629_moni_yk.view.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; 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.TextView; import com.example.day0629_moni_yk.R; import com.example.day0629_moni_yk.adapter.FenLeiAdapterRight; import com.example.day0629_moni_yk.bean.Constant; import com.example.day0629_moni_yk.bean.GsonFenLeiRightData; import com.example.day0629_moni_yk.util.RequestDataUtil; import com.google.gson.Gson; import org.greenrobot.eventbus.EventBus; import java.util.List; /** * Created by John on 2018/6/29 0029. */ public class FragmentShoppingCartRigth extends Fragment { private View view; private EventBus eventBus; private String cid = "1";//默认为1 /** * 热门分类 */ private TextView mFenleiReightName; private RecyclerView mFenleiReightContent; private RequestDataUtil dataUtil; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_shopping_rigth, container, false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(view); Bundle arguments = getArguments(); if (arguments != null) { cid = arguments.getString("cid"); //Log.d("zzz", "onActivityCreated: "+ cid); } //网络请求数据 requestDataFunction(); } private void requestDataFunction() { //拼接地址 String urlRight = Constant.FenLeiUrlRight+"?cid="+cid; Log.d("oooo", "getItemCount: "+urlRight); dataUtil.getRequestJsonData(urlRight, new RequestDataUtil.IcallBack() { @Override public void onRequest(String jsonData) { //请求数据成功 Gson gson = new Gson(); GsonFenLeiRightData rightData = gson.fromJson(jsonData, GsonFenLeiRightData.class); List<GsonFenLeiRightData.DataBean> beanList = rightData.getData(); //设置适配器 mFenleiReightContent.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false)); FenLeiAdapterRight adapterRight = new FenLeiAdapterRight(getContext(),beanList); mFenleiReightContent.setAdapter(adapterRight); } }); } private void initView(View view) { mFenleiReightName = (TextView) view.findViewById(R.id.fenlei_reight_name); mFenleiReightContent = (RecyclerView) view.findViewById(R.id.fenlei_reight_content); //得到网络请求工具类 dataUtil = RequestDataUtil.getInstance(); } }
---------------------------------------------第一层适配器---------------------------------------------
package com.example.day0629_moni_yk.adapter; import android.content.Context; import android.support.v7.widget.GridLayoutManager; 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.TextView; import com.example.day0629_moni_yk.R; import com.example.day0629_moni_yk.bean.GsonFenLeiRightData; import java.util.List; /** * Created by John on 2018/6/29 0029. */ public class FenLeiAdapterRight extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<GsonFenLeiRightData.DataBean> list; public FenLeiAdapterRight(Context context, List<GsonFenLeiRightData.DataBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.fragment_shopping_rigth, parent, false); ViewHolderRight holderRight = new ViewHolderRight(view); return holderRight; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolderRight holderRight = (ViewHolderRight) holder; //绑定数据 holderRight.mFenleiReightName.setText(list.get(position).getName()); //得到集合 设置适配器 List<GsonFenLeiRightData.DataBean.ListBean> listb = list.get(position).getList(); //设置布局管理器 holderRight.mFenleiReightContent.setLayoutManager(new GridLayoutManager(context,3)); //设置适配器 FenLeiChildrenRightAdapter rightAdapter = new FenLeiChildrenRightAdapter(context,listb); holderRight.mFenleiReightContent.setAdapter(rightAdapter); /**//接口回调 baseAdapter.setOnItemClickRight(new RightNeiAdapter.IcallBackRight() { @Override public void ItemOnClick(int childrenPosition) {//接口回调得到子适配器的Position if(rightIcallBack!=null){ rightIcallBack.getPcid(childrenPosition,position); } //获取获取pscid 这个position使用于得到子集合的 这个childrenposition使用于得到子集合中的具体哪一个商品的 int pscid = beanList.get(position).getList().get(childrenPosition).getPscid(); //Log.d("dddddd", "getPcid: "+pscid+"position:"+position+"childrenPosition:"+childrenPosition); //跳转页面 Intent intent = new Intent(context, CommodityListActivity.class); intent.putExtra("pscid",pscid+""); context.startActivity(intent); } });**/ } @Override public int getItemCount() { return list.size(); } static class ViewHolderRight extends RecyclerView.ViewHolder { View view; TextView mFenleiReightName; RecyclerView mFenleiReightContent; ViewHolderRight(View view) { super(view); this.view = view; this.mFenleiReightName = (TextView) view.findViewById(R.id.fenlei_reight_name); this.mFenleiReightContent = (RecyclerView) view.findViewById(R.id.fenlei_reight_content); } } }
------------------------------------第二层适配器------------------------------------
package com.example.day0629_moni_yk.adapter; import android.content.Context; import android.net.Uri; 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.day0629_moni_yk.R; import com.example.day0629_moni_yk.bean.GsonFenLeiRightData; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; /** * Created by John on 2018/6/29 0029. */ public class FenLeiChildrenRightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<GsonFenLeiRightData.DataBean.ListBean> listb; public FenLeiChildrenRightAdapter(Context context, List<GsonFenLeiRightData.DataBean.ListBean> listb) { this.context = context; this.listb = listb; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.fenleirightchildren, parent, false); ViewHolderChildren holderChildren = new ViewHolderChildren(view); return holderChildren; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolderChildren holderChildren = (ViewHolderChildren) holder; //绑定数据 holderChildren.mChildrenName.setText(listb.get(position).getName()); //下载图片 Uri uri = Uri.parse(listb.get(position).getIcon()); holderChildren.mMyChildrenImage.setImageURI(uri); } @Override public int getItemCount() { return listb.size(); } static class ViewHolderChildren extends RecyclerView.ViewHolder { View view; SimpleDraweeView mMyChildrenImage; TextView mChildrenName; ViewHolderChildren(View view) { super(view); this.view = view; this.mMyChildrenImage = (SimpleDraweeView) view.findViewById(R.id.my_children_image); this.mChildrenName = (TextView) view.findViewById(R.id.children_name); } } }