购物车一级列表的简单实现(访问接口数据)
来源:互联网 发布:好孩子淘宝店是正品吗 编辑:程序博客网 时间:2024/04/29 09:25
导入依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.android.support:recyclerview-v7:25.0.0' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
bean实体类里面加入一个字段,,用来控制 每个商品前面checkbox的选中状态
private boolean check;public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; }
values下面的colors.xml,方便布局里面的统一调用
<?xml version="1.0" encoding="utf-8"?><resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!--背景颜色--> <color name="toolbar_color">#f8f8f8</color> <color name="background_color">#f6f6f6</color> <color name="edittext_noenter_color">#f5f5f5</color> <!--辅助色--> <color name="support_yellow">#f9a340</color> <color name="btn_unselect_color">#f4736e</color> <!--分割线--> <color name="splitline_color">#dddddd</color> <!--文字--> <color name="notice_text_color">#999999</color> <color name="default_text_color">#666666</color> <color name="main_black_text">#333333</color> <color name="main_red_text">#e53e42</color> <color name="main_white_text">#ffffff</color> <color name="protocol_text">#3C68FE</color> <!--图标--> <color name="abate_icon_color">#bbbbbb</color> <color name="default_icon_color">#999999</color> <color name="pressed_icon_color">#e53e42</color> <!--CycleViewPager--> <color name="cycle_image_bg">#44222222</color> <!-- 透明 --> <color name="transparent">#00000000</color>; </resources>去结算 这个文字的背景 自己画的小圆点
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="200dp"></corners> <solid android:color="@color/pressed_icon_color"></solid></shape>
activity_main.xml里面的布局 ,上面是一个recyclerview ,下面是一个linearlayout,装着全选 ,总价,总数量,去结算等
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/second_recyview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_alignBottom="@+id/linear" android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/main_white_text" android:gravity="center_vertical" android:id="@+id/ll_pay" android:layout_alignParentBottom="true" > <CheckBox android:button="@null" android:id="@+id/quanxuan" android:background="@drawable/shopcart_unselected" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textSize="23sp" android:id="@+id/all_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="全选" android:drawablePadding="5dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/totalprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingTop="10dp" android:text="总价:¥0" android:textColor="@color/main_red_text" android:textSize="20sp" /> <TextView android:id="@+id/totalnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:text="共0件商品" android:textSize="18sp" android:paddingBottom="10dp" /> </LinearLayout> <TextView android:id="@+id/qujiesuan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/login_btn" android:text="去结算" android:textSize="23sp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:textColor="@color/main_white_text" android:layout_marginRight="10dp" /> </LinearLayout></LinearLayout>recyclerview的适配器的布局 ,,里面引入了自定义的customview的布局
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/background_color" ></View> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/shopcart_cloth_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/shopcart_selected" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:visibility="invisible" /> <TextView android:id="@+id/tv_item_shopcart_clothname" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="穿秋装情侣字母徽章风衣" android:paddingLeft="10dp" android:paddingTop="10dp" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <CheckBox android:id="@+id/cb_item_shopcart_clothselect" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="10dp" /> <ImageView android:src="@mipmap/ic_launcher" android:id="@+id/iv_item_shopcart_cloth_pic" android:layout_width="90dp" android:layout_height="90dp" android:layout_margin="10dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/tv_item_shopcart_cloth_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥185" android:textColor="@color/main_red_text" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" > <TextView android:id="@+id/tv_item_shopcart_cloth_color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="颜色:黑色" android:textSize="18sp" /> <TextView android:id="@+id/tv_item_shopcart_cloth_size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="尺寸:XL" android:textSize="18sp" android:layout_marginLeft="10dp" /> </LinearLayout> <com.example.a171118_zhouliu_disanzhou.custom.CustomView android:layout_width="100dp" android:layout_height="50dp" android:id="@+id/custom_View" android:layout_marginLeft="20dp"/> </LinearLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="@color/splitline_color" ></View> <ImageView android:id="@+id/iv_item_shopcart_cloth_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:src="@drawable/shopcart_delete" /> </LinearLayout> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/background_color" ></View></LinearLayout>
自定义组合控件的customview的布局
<?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"> <Button android:background="#00FFFFFF" android:gravity="center" android:textStyle="bold" android:textSize="20sp" android:text="一" android:id="@+id/reverse" android:layout_width="50dp" android:layout_height="30dp" /> <EditText android:id="@+id/count" android:textSize="23sp" android:textStyle="bold" android:text="1" android:layout_width="wrap_content" android:layout_height="50dp" /> <Button android:textStyle="bold" android:textSize="22sp" android:id="@+id/add" android:gravity="center" android:background="#00FFFFFF" android:layout_width="50dp" android:layout_height="30dp" android:text="+"/></LinearLayout>写CustomView类继承LinearLayout,, 填充customView.xml的布局
public class CustomView extends LinearLayout{ private Button reverse; private Button add; private EditText editText; private int mCount; Context context; public CustomView(Context context) { super(context); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); this.context =context; View view = View.inflate(context, R.layout.custom_view,this); reverse = (Button) view.findViewById(R.id.reverse); add = (Button) view.findViewById(R.id.add); editText = (EditText) view.findViewById(R.id.count); //点击减号,,数量减1 reverse.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String content = editText.getText().toString().trim(); //转成int int count = Integer.valueOf(content); if(count>1){ mCount = count-1; editText.setText(mCount+"");//设置数量 //接口回调 把mCount暴露出去,,adapter里面调用 if(clickListener!=null){ clickListener.click(mCount); } } } }); //点击加号 数量加1 add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String content = editText.getText().toString().trim(); int count = Integer.valueOf(content); mCount = count+1;//赋值给mcount editText.setText(count+""); if(clickListener!=null){ clickListener.click(mCount); } } }); } ClickListener clickListener; public void setClickListener(ClickListener clickListener){ this.clickListener = clickListener; } //设置加减号的监听 public interface ClickListener{ public void click(int count);//把当前的数量传过去 } //设置初始的数量值 public void getCount(int count){ editText.setText(count+"");//设置初始数量 } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}RecyclerAdapter 适配器里面的,, 实现customview里面的接口
public class SecondRecyAdapter extends RecyclerView.Adapter<SecondRecyAdapter.MyViewholder1>{ Context context; List<SecondBean.DataBean.ListBean> listShop; public SecondRecyAdapter(Context context, List<SecondBean.DataBean.ListBean> listShop) { this.listShop = listShop; this.context = context; } @Override public MyViewholder1 onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context,R.layout.second_recy_adapter,null); MyViewholder1 myviewholder1 = new MyViewholder1(view); return myviewholder1; } @Override public void onBindViewHolder(final MyViewholder1 holder, final int position) { String images = listShop.get(position).getImages(); String[] split = images.split("\\|"); //设置商品图片 ImageLoader.getInstance().displayImage(split[0],holder.ivPic); //调用customiew类里面的方法,,设置商品名称 holder.customView.getCount(listShop.get(position).getNum()); // holder.tvShopName.setText(listShop.get(position).getSellerid()); holder.tvName.setText(listShop.get(position).getTitle()); holder.tvPrice.setText(listShop.get(position).getPrice()+""); //给 每个条目的选中按钮 设置状态,根据数据源里面的check holder.product_select.setChecked(listShop.get(position).isCheck()); //主动点击每个条目前面的多选框,点击事件 holder.product_select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //拿到当前多选框的选中状态,,赋值给数据源里面,,holder.product_select.isChecked() listShop.get(position).setCheck(holder.product_select.isChecked()); notifyDataSetChanged(); if(checkBoxListener != null){ //secondActivity调用方法 checkBoxListener.checkBox(listShop); } } }); //customview中的加减号的点击监听 holder.customView.setClickListener(new CustomView.ClickListener() { @Override public void click(int count) { //count是当前的数量 //根据传来的count 改变数据源里的num listShop.get(position).setNum(count); notifyDataSetChanged(); //接口回调出去.. if(customViewListener!=null){ //把当前集合传过去 customViewListener.customClick(listShop); } } }); //删除当前条目的点击事件 holder.ivDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //改变数据源中的 删除当前条目 listShop.remove(position); notifyDataSetChanged(); //接口回调出去 if(delItemListener!=null){ delItemListener.delItem(listShop); } } }); } @Override public int getItemCount() { return listShop==null?0:listShop.size(); } public static class MyViewholder1 extends RecyclerView.ViewHolder { private final ImageView ivDelete; private final TextView tvPrice; private final TextView tvName; private final ImageView ivSelect; CustomView customView; private final ImageView ivPic; private final CheckBox product_select;//每个条目前面的多选框, // private final TextView tvShopName;//商铺名称 public MyViewholder1(View itemView) { super(itemView); //找到所有的控件 ivDelete = (ImageView) itemView.findViewById(R.id.iv_item_shopcart_cloth_delete); tvPrice = (TextView) itemView.findViewById(R.id.tv_item_shopcart_cloth_price); tvName = (TextView) itemView.findViewById(R.id.tv_item_shopcart_clothname); ivSelect = (ImageView) itemView.findViewById(R.id.shopcart_cloth_select); ivPic = (ImageView) itemView.findViewById(R.id.iv_item_shopcart_cloth_pic); product_select = (CheckBox) itemView.findViewById(R.id.cb_item_shopcart_clothselect); customView = (CustomView) itemView.findViewById(R.id.custom_View); // tvShopName = (TextView) itemView.findViewById(R.id.tv_item_shopcart_shopname); } } //返回当前集合的方法 public List<SecondBean.DataBean.ListBean> getList(){ return listShop; } //实现多选框的监听 CheckBoxListener checkBoxListener; public void setCheckBoxListener(CheckBoxListener checkBoxListener){ this.checkBoxListener = checkBoxListener; } //每个条目前面的多选框点击的监听 public interface CheckBoxListener{ public void checkBox(List<SecondBean.DataBean.ListBean> list); } //实现加减号 改变数量和总价 public interface CustomViewListener{ public void customClick(List<SecondBean.DataBean.ListBean> list); } //实现加减号 的监听 CustomViewListener customViewListener; public void setCustomViewListener(CustomViewListener customViewListener){ this.customViewListener = customViewListener; } //实现删除按钮的点击事件 public interface DelItemListener{ public void delItem(List<SecondBean.DataBean.ListBean> list); } DelItemListener delItemListener; public void setDelItemListener(DelItemListener delItemListener){ this.delItemListener = delItemListener; }}
Mainactivity里面的,,求总价 求总数量 实现recyAdapter里面的接口
public class SecondActivity extends AppCompatActivity { private TextView all_select;//全选按钮 private TextView totalPrice;//总价 private TextView totalNum;//总件数 private List<SecondBean.DataBean.ListBean> listShop = new ArrayList<>(); private SecondRecyAdapter secondRecyAdapter; boolean mSelect = false; private RecyclerView second_recyview; private boolean shifoucheck; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //获取控件 second_recyview = (RecyclerView) findViewById(R.id.second_recyview); all_select = (TextView) findViewById(R.id.quanxuan);//全选按钮 totalPrice = (TextView) findViewById(R.id.totalprice); totalNum = (TextView) findViewById(R.id.totalnum); // secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop);; LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SecondActivity.this,LinearLayoutManager.VERTICAL,false); second_recyview.setLayoutManager(linearLayoutManager); //请求网络数据 getData(); //请求完网络数据 设置适配器 if(secondRecyAdapter==null){ secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop); second_recyview.setAdapter(secondRecyAdapter); }else{ secondRecyAdapter.notifyDataSetChanged(); } //进入页面先给总的全选按钮设置一个标记tag tag为1 不选中, tag为2选中 all_select.setTag(1);//默认不选中 //为了通过all_select改变 每条数据的选中状态 shifoucheck = false; //全选按钮的点击事件 all_select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置图片和字在一个控件里面,,图片的大小不会打乱 // Drawable left = getResources().getDrawable(R.drawable.shopcart_selected); // all_select.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null); //获取all_select 的tag int tag = (int) all_select.getTag(); if(tag==1){ //如果是没选中,点击以后 改变成选中 all_select.setTag(2); all_select.setBackgroundResource(R.drawable.shopcart_selected); //控制所有的条目都改成 选中 shifoucheck = true; }else{ //如果当前是选中的 就设置成未选中 all_select.setTag(1); all_select.setBackgroundResource(R.drawable.shopcart_unselected); shifoucheck = false; } //然后遍历集合里面的所有条目都设置成选中 for (SecondBean.DataBean.ListBean bean : listShop){ //给数据源里面的check 标记为 上面改变的 bean.setCheck(shifoucheck); } secondRecyAdapter.notifyDataSetChanged();//刷新适配器 sum(secondRecyAdapter.getList());//把适配器里面的集合传进去,求和 } }); //调用适配器里面的选中多选框的监听 secondRecyAdapter.setCheckBoxListener(new SecondRecyAdapter.CheckBoxListener() { @Override public void checkBox(List<SecondBean.DataBean.ListBean> list) { //调用求和的方法 sum(list); } }); //调用适配器里面的 加减号的监听 secondRecyAdapter.setCustomViewListener(new SecondRecyAdapter.CustomViewListener() { @Override public void customClick(List<SecondBean.DataBean.ListBean> list) { sum(list); } }); //调用适配器里面的 删除当前条目的方法 secondRecyAdapter.setDelItemListener(new SecondRecyAdapter.DelItemListener() { @Override public void delItem(List<SecondBean.DataBean.ListBean> list) { sum(list); } }); } //求总价的总数量的方法 public void sum(List<SecondBean.DataBean.ListBean> listShop){ float price = 0; int count = 0; //反着全选的控制,,,控制下面的总的 全选按钮 boolean allCheck = true; for (SecondBean.DataBean.ListBean bean : listShop){ if(bean.isCheck()){ //如果当前的check为true.计算总价 price += bean.getPrice()*bean.getNum(); count += bean.getNum(); }else{ //只要有一个为没选中,就标记,改变下面的全选按钮 allCheck = false; } //设置下面的总价和数量 totalNum.setText("共"+count+"件商品"); totalPrice.setText("总价: "+price); } //根据allcheck 改变下面的全选按钮的值 if(allCheck){ //如果遍历出来的所有check都是true,就全选也选中 all_select.setTag(2); all_select.setBackgroundResource(R.drawable.shopcart_selected); }else{ all_select.setTag(1); all_select.setBackgroundResource(R.drawable.shopcart_unselected); } } //请求网络数据解析 private void getData() { String path = "http://120.27.23.105/product/getCarts?uid=100"; OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<SecondBean>() { @Override public void success(SecondBean secondBean) { // System.out.println(secondBean.getData().get(0).getList().get(0).getTitle()); //添加到集合里 data里面的list for(int i=0;i<secondBean.getData().size();i++){ //将集合里面的list集合添加到大集合里去 listShop.addAll(secondBean.getData().get(i).getList()); } // System.out.println(listShop.get(0).getTitle()); //设置适配器 /*if(secondRecyAdapter==null){ secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop); second_recyview.setAdapter(secondRecyAdapter); }else{ secondRecyAdapter.notifyDataSetChanged(); }*/ } @Override public void failure(Exception e) { } }); }}新建类继承application
public class App extends Application{ @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); }}清单文件中声明
<application android:name=".aplica.App" </application>
阅读全文
0 0
- 购物车一级列表的简单实现(访问接口数据)
- 一级列表购物车的简单实现(网络接口)
- 简单实现 一级列表购物车(固定数据)
- 000001简单一级购物车的实现
- android 实现简单的一级购物车
- 一级列表的购物车
- 一级列表的购物车
- 购物车二级列表的实现,在一级列表基础上,bean类里再添加两个字段(请求本地json数据)
- 购物车二级列表的实现,在一级列表基础上,bean类里再添加两个字段(请求本地json数据)
- 一级列表购物车
- 一级列表购物车
- 购物车一级列表
- Android一级购物车简单实现
- 一级购物车实现添加数据
- 一级列表购物车完整版
- 仿购物车一级列表
- android一级列表购物车
- 购物车一级列表布局
- 【漫漫科研路】序言
- 深度学习网络大杀器之Dropout——深入解析Dropout
- 设置darktable为中文
- 数据恢复软件介绍
- 蓝桥杯 算法提高 扶老奶奶过街 (Java解题)
- 购物车一级列表的简单实现(访问接口数据)
- 增删改查
- axios发送两次请求,options与get/post
- 数据的序列化(all Type to Byte[])
- iOS ScrollView嵌套ScrolloView解决方案
- linux下python第三库(setuptools)的安装
- 几种形式队列的实现
- JVM内存相关
- java中数组与集合<泛型>的相互转换