Retrofit拼接接口
public static final String LEFT_SHOP="https://www.zhaoapi.cn/";
接口
//左侧的拼接地址 @GET("product/getCatagory") Call<LeftDatas> getLeftDatas(); //右侧的拼接地址 @GET("product/getProductCatagory?") Call<RightDatas> getRightdatas(@Query ("cid")int cid); //查询购物车的 @GET("product/getCarts?uid=13249") Call<ShowDatas> getShowDatas();
1.分类页面布局
使用recycleView 和framelayout
显示左边布局
xml布局
?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/leftviews" android:layout_width="100dp" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> <FrameLayout android:id="@+id/rightfragment" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </LinearLayout>
<2>fragment中使用
public class FenLeiFragment extends Fragment { @BindView(R.id.leftviews) RecyclerView leftviews; @BindView(R.id.rightfragment) FrameLayout rightfragment; Unbinder unbinder; private LeftShopAdaper adaper; private List<LeftDatas.DataBean> data; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate (R.layout.fenlei, container, false); unbinder = ButterKnife.bind (this, view); ininview(); return view; } private void ininview() { //得到数据 Retrofit retrofit=new Retrofit.Builder () .addConverterFactory (GsonConverterFactory.create ()) .baseUrl (Connstand.LEFT_SHOP) .build (); IConnstand iConnstand = retrofit.create (IConnstand.class); iConnstand.getLeftDatas ().enqueue (new Callback<LeftDatas> () { @Override public void onResponse(Call<LeftDatas> call, Response<LeftDatas> response) { data = response.body ().getData (); adaper = new LeftShopAdaper (getActivity (), data); leftviews.setAdapter (adaper); leftviews.setLayoutManager (new LinearLayoutManager (getActivity (),LinearLayoutManager.VERTICAL,false)); //点击左边的列表进行传值 leftclick(); } @Override public void onFailure(Call<LeftDatas> call, Throwable t) { } }); } private void leftclick() { adaper.setMsetItemOnclickListener (new LeftShopAdaper.SetItemOnclickListener () { @Override public void setCid(int layoutPosition) { //得到每个下标里面的cid int cid = data.get (layoutPosition).getCid (); //动态的加载右边的fragment FenLeiRightFragment fenLeiRightFragment=new FenLeiRightFragment (); //bundle进行传值 Bundle bundle=new Bundle (); bundle.putString ("cid",cid+""); fenLeiRightFragment.setArguments (bundle); //加载布局 getActivity ().getSupportFragmentManager ().beginTransaction ().replace (R.id.rightfragment,fenLeiRightFragment).commit (); } }); } @Override public void onDestroyView() { super.onDestroyView (); unbinder.unbind (); } }
《3》适配器中使用
public class LeftShopAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<LeftDatas.DataBean> data; public LeftShopAdaper(Context context, List<LeftDatas.DataBean> data) { this.context = context; this.data = data; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate (context,R.layout.fenlei_style,null); ViewHolder holder=new ViewHolder (view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder holder1= (ViewHolder) holder; holder1.textView.setText (data.get (position).getName ()); holder1.simpleDraweeView.setImageURI (data.get (position).getIcon ()); //得到下标 final int layoutPosition = holder1.getLayoutPosition (); //在item的点击事件里进行操作利用接口回调传下标 holder1.itemView.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { if(msetItemOnclickListener!=null){ msetItemOnclickListener.setCid (layoutPosition); } } }); } @Override public int getItemCount() { return data.size (); } public class ViewHolder extends RecyclerView.ViewHolder{ SimpleDraweeView simpleDraweeView; TextView textView; public ViewHolder(View itemView) { super (itemView); simpleDraweeView=itemView.findViewById (R.id.leftimages); textView=itemView.findViewById (R.id.lefttext); } } //创建接口 public interface SetItemOnclickListener{ void setCid(int layoutPosition); } //接口实现类 private SetItemOnclickListener msetItemOnclickListener; public void setMsetItemOnclickListener(SetItemOnclickListener msetItemOnclickListener) { this.msetItemOnclickListener = msetItemOnclickListener; } }
2.右边framelayout布局
recycleview 的布局文件中再次嵌套了一个recycleview
xml
<android.support.v7.widget.RecyclerView android:id="@+id/biaoti" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
fragment代码
/** * Created by 11561 on 2018/6/29. * * 右边的fragment里面是一个recycleview,这个recycleview里面的布局文件里又嵌套了一个recycleview * * */ public class FenLeiRightFragment extends Fragment { @BindView(R.id.biaoti) RecyclerView biaoti; Unbinder unbinder; private int cid; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate (R.layout.fenleiright, container, false); //接收传来的值 Bundle arguments = getArguments (); cid = Integer.parseInt (arguments.getString ("cid")); initdatas (); unbinder = ButterKnife.bind (this, view); return view; } private void initdatas() { Retrofit retrofit = new Retrofit.Builder () .addConverterFactory (GsonConverterFactory.create ()) .baseUrl (Connstand.LEFT_SHOP) .build (); IConnstand iConnstand = retrofit.create (IConnstand.class); //拼接cid并进行解析数据 iConnstand.getRightdatas (cid).enqueue (new Callback<RightDatas> () { @Override public void onResponse(Call<RightDatas> call, Response<RightDatas> response) { List<RightDatas.DataBean> data = response.body ().getData (); //得到适配器 RightShopAdaper adaper = new RightShopAdaper (getActivity (), data); //加载数据 biaoti.setAdapter (adaper); biaoti.setLayoutManager (new LinearLayoutManager (getActivity (), LinearLayoutManager.VERTICAL, false)); } @Override public void onFailure(Call<RightDatas> call, Throwable t) { } }); } @Override public void onDestroyView() { super.onDestroyView (); unbinder.unbind (); } }
第一个适配器中
public class RightShopAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<RightDatas.DataBean> data; public RightShopAdaper(Context context, List<RightDatas.DataBean> data) { this.context = context; this.data = data; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate (context,R.layout.fenleiright_style,null); //第一个分类中的R.layout.fenleiright_style布局中又嵌套了一个recycleview,所以在这个里面还要加载另一个适配器 ViewHolder holder=new ViewHolder (view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder holder1= (ViewHolder) holder; holder1.textView.setText (data.get (position).getName ()); //返回的数据里面还有一层集合,先拿到这个集合 List<RightDatas.DataBean.ListBean> list = data.get (position).getList (); //重新写一个适配器,把拿到的集合放到适配器当中 RightShopingAdaper shopingAdaper=new RightShopingAdaper (context,list); //加载布局 holder1.recyclerView.setAdapter (shopingAdaper); holder1.recyclerView.setLayoutManager (new GridLayoutManager (context,4)); } @Override public int getItemCount() { return data.size (); } public class ViewHolder extends RecyclerView.ViewHolder{ TextView textView; RecyclerView recyclerView; public ViewHolder(View itemView) { super (itemView); textView=itemView.findViewById (R.id.rightshoptitle); //得到嵌套的recycleview的id, recyclerView=itemView.findViewById (R.id.rightshoping); } } }
第一个适配器中加载的布局
<TextView android:id="@+id/rightshoptitle" android:text="标题" android:textSize="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:layout_below="@+id/rightshoptitle" android:id="@+id/rightshoping" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
第二个适配器
xml布局
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/rightshopneirongimages" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:id="@+id/rightshop_text" android:text="商品内容" android:layout_width="wrap_content" android:layout_height="wrap_content" />
适配器中
public class RightShopingAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ private Context context; private List<RightDatas.DataBean.ListBean> data;; public RightShopingAdaper(Context context, List<RightDatas.DataBean.ListBean> data) { this.context = context; this.data = data; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=View.inflate (context,R.layout.fenleirightshop_style,null); ViewHolder holder=new ViewHolder (view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder holder1= (ViewHolder) holder; holder1.simpleDraweeView.setImageURI (data.get (position).getIcon ()); holder1.textView.setText (data.get (position).getName ()); } @Override public int getItemCount() { return data.size (); } public class ViewHolder extends RecyclerView.ViewHolder{ SimpleDraweeView simpleDraweeView; TextView textView; public ViewHolder(View itemView) { super (itemView); simpleDraweeView=itemView.findViewById (R.id.rightshopneirongimages); textView=itemView.findViewById (R.id.rightshop_text); } } }
最后把这个适配器在第一个适配器中加载
二 购物车页面展示
使用二级列表进行展示数据
xml 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <ExpandableListView android:layout_weight="9" android:id="@+id/busshop" android:layout_width="match_parent" android:layout_height="0dp"></ExpandableListView> <RelativeLayout android:id="@+id/busshop_bottom" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/sum_chbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/sum_chbox" android:layout_alignBottom="@+id/sum_chbox" android:layout_toEndOf="@+id/sum_chbox" android:layout_toRightOf="@+id/sum_chbox" android:text="全选" /> <TextView android:id="@+id/sums" android:text="合计:" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/sum_prcie" android:text="总价" android:layout_toRightOf="@+id/sums" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/jiesuan" android:text="结算" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp" android:background="#ff00" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout>
2.fragment中操作
public class BusFragment extends Fragment { @BindView(R.id.busshop) ExpandableListView busshop; Unbinder unbinder; @BindView(R.id.sum_chbox) CheckBox sumChbox; @BindView(R.id.sums) TextView sums; @BindView(R.id.sum_prcie) TextView sumPrcie; @BindView(R.id.jiesuan) TextView jiesuan; @BindView(R.id.busshop_bottom) RelativeLayout busshopBottom; private List<ShowDatas.DataBean> data; private BusAdaper busAdaper; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate (R.layout.bus, container, false); unbinder = ButterKnife.bind (this, view); inindata (); return view; } private void inindata() { //使用retrofit Retrofit retrofit = new Retrofit.Builder () .addConverterFactory (GsonConverterFactory.create ()) .baseUrl (Connstand.LEFT_SHOP) .build (); IConnstand iConnstand = retrofit.create (IConnstand.class); iConnstand.getShowDatas ().enqueue (new Callback<ShowDatas> () { @Override public void onResponse(Call<ShowDatas> call, Response<ShowDatas> response) { data = response.body ().getData (); final int price = data.get (0).getList ().get (0).getPrice (); //得到适配器 busAdaper = new BusAdaper (getActivity (), data); //加载适配器 busshop.setAdapter (busAdaper); //展示子条目下的所有数据 List<ShowDatas.DataBean.ListBean> list = data.get (0).getList (); for (int i = 0; i < data.size (); i++) { //二级列表里面的属性 busshop.expandGroup (i); } } @Override public void onFailure(Call<ShowDatas> call, Throwable t) { } }); //点击全选框 sumChbox.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener () { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //第一层循环遍历第一层数据 for (int i = 0; i <data.size () ; i++) { //List<ShowDatas.DataBean.ListBean> list = data.get (i).getList (); //第二层循环拿到里面的一层数据 for (int j = 0; j <data.get (i).getList ().size () ; j++) { //如果是选中状态赋值为1 if(isChecked){ data.get (i).getList ().get (j).setSelected (1); }else{ data.get (i).getList ().get (j).setSelected (0); } } } //刷新适配器 busAdaper.notifyDataSetChanged (); } }); } @Override public void onDestroyView() { super.onDestroyView (); unbinder.unbind (); } }
3.需要两个布局 一个是组布局文件,一个是组布局文件下的子条目布局
组布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/group_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="48dp" android:layout_marginStart="48dp" android:layout_marginTop="33dp" /> <TextView android:id="@+id/group_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/group_box" android:layout_alignBottom="@+id/group_box" android:layout_marginLeft="40dp" android:layout_marginStart="40dp" android:layout_toEndOf="@+id/group_box" android:layout_toRightOf="@+id/group_box" android:text="组" /> </RelativeLayout>
子布局
绘制一个背景图,在适配器中给设置的加和减两个text view设置背景
在draw able建一个xml文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 内部颜色 --> <solid android:color="#ffffff" /> <!-- 边缘线条颜色 --> <stroke android:width="3dp" android:color="#9e9e9e" /> <!-- 圆角的幅度 --> <corners android:radius="20dp" /> </shape>子布局xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <CheckBox android:id="@+id/child_box" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/child_image" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:id="@+id/child_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/child_image" android:text="名称" /> <TextView android:id="@+id/child_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:layout_toRightOf="@+id/child_image" android:text="价钱" /> </RelativeLayout> <LinearLayout android:layout_marginLeft="300dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/addimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" + " android:textSize="20dp" /> <TextView android:id="@+id/numliang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:padding="5dp" android:text="1" android:textSize="20dp" /> <TextView android:id="@+id/subimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" - " android:textSize="20dp" /> </LinearLayout> </LinearLayout>
4.二级列表的适配器
public class BusAdaper extends BaseExpandableListAdapter{ private Context context; private List<ShowDatas.DataBean> data; private int childIndex; //定义变量,点击加减的时候变化 private int i=1; public BusAdaper(Context context, List<ShowDatas.DataBean> data) { this.context = context; this.data = data; } //得到组的数量 @Override public int getGroupCount() { return data.size (); } //组的位置(groupPosition)得到 @Override public int getChildrenCount(int groupPosition) { return data.get (groupPosition).getList ().size (); } //根据组的位置得到内容 @Override public Object getGroup(int groupPosition) { return data.get (groupPosition); } //根据组的位置得到指定的子条目内容 @Override public Object getChild(int groupPosition, int childPosition) { return data.get (groupPosition).getList ().get (childPosition); } //得到组的id @Override public long getGroupId(int groupPosition) { return groupPosition; } //得到字条的ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //Stable 固定的 是否持有固定的ID @Override public boolean hasStableIds() { return true; } //得到组的视图 //isExpanded 是否处于展开的状态 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupViewHolder groupViewHolder; //优化 if(convertView==null){ convertView=View.inflate (context, R.layout.busgroup_style,null); groupViewHolder=new GroupViewHolder (); groupViewHolder.gcheckBox = convertView.findViewById (R.id.group_box); groupViewHolder.gtextview= convertView.findViewById (R.id.group_text); convertView.setTag (groupViewHolder); }else{ groupViewHolder= (GroupViewHolder) convertView.getTag (); } groupViewHolder.gtextview.setText (data.get (groupPosition).getSellerName ()); for (int j = 0; j <data.size () ; j++) { } return convertView; } //得到子条目的布局 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) { final ChildViewHolder childViewHolder; //优化 if(convertView==null){ convertView=View.inflate (context,R.layout.buschild_style,null); childViewHolder=new ChildViewHolder (); childViewHolder.ccheckBox=convertView.findViewById (R.id.child_box); childViewHolder.ctextview=convertView.findViewById (R.id.child_text); childViewHolder.cprice=convertView.findViewById (R.id.child_price); childViewHolder.add=convertView.findViewById (R.id.addimage); childViewHolder.sub=convertView.findViewById (R.id.subimage); childViewHolder.numliang=convertView.findViewById (R.id.numliang); childViewHolder.csimpleDraweeView=convertView.findViewById (R.id.child_image); convertView.setTag (childViewHolder); }else{ childViewHolder= (ChildViewHolder) convertView.getTag (); } childViewHolder.ctextview.setText (data.get (groupPosition).getList ().get (childPosition).getTitle ()); String images = data.get (groupPosition).getList ().get (childPosition).getImages (); String[] split = images.split ("\\|"); //设置背景 childViewHolder.add.setBackground (context.getDrawable (R.drawable.ed)); childViewHolder.sub.setBackground (context.getDrawable (R.drawable.ed)); childViewHolder.csimpleDraweeView.setImageURI (split[0]); childViewHolder.cprice.setText (data.get (groupPosition).getList ().get (childPosition).getPrice ()+""); final int price = data.get (groupPosition).getList ().get (childPosition).getPrice (); //在子条目里面得到接口中那个状态值getSelected int selected = data.get (groupPosition).getList ().get (childPosition).getSelected (); //进行判断 if(selected==1){ //等于1的时候选为true childViewHolder.ccheckBox.setChecked (true); }else{ //否则选为false childViewHolder.ccheckBox.setChecked (false); } //点击加号数量进行加加 childViewHolder.add.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { if(i>=1){ i++; //i定义的是1,当i++的时候让i*价钱的单价 int numi = BusAdaper.this.i * price; childViewHolder.cprice.setText (numi+""); } /*if(mgetNumListener!=null){ mgetNumListener.getnum (i); }*/ //让加减号中间的数变化 childViewHolder.numliang.setText (i+""); } }); //点击减号数量进行加加 childViewHolder.sub.setOnClickListener (new View.OnClickListener () { @Override public void onClick(View v) { if(i>1){ i--; int numi = BusAdaper.this.i * price; childViewHolder.cprice.setText (numi+""); } //让加减号中间的数变化 childViewHolder.numliang.setText (i+""); } }); return convertView; } //子条目是否可以点击-----??? true 可以进行点击 @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //组的寄存器 class GroupViewHolder{ CheckBox gcheckBox; TextView gtextview; } //子条目的寄存器 class ChildViewHolder{ TextView add,sub,numliang; CheckBox ccheckBox; TextView ctextview,cprice; SimpleDraweeView csimpleDraweeView; } /* public interface GetNumListener{ void getnum(int i); } private GetNumListener mgetNumListener; public void setMgetNumListener(GetNumListener mgetNumListener) { this.mgetNumListener = mgetNumListener; }*/ }