购物车的使用

布局:<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"

        >
        <EditText
            android:id="@+id/edittext"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginLeft="65dp"
            android:hint="请输入你要搜索的商品"
            />
            <Button
                android:id="@+id/but1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="搜索"
                />


    </LinearLayout>
    <TextView

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:background="#990000ff"

        android:gravity="center"

        android:text="购物车"

        android:textColor="#ff3660"

        android:textSize="25sp" />
    <com.example.yuekaomoni.MyView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

    </com.example.yuekaomoni.MyView>


    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >

    </com.jcodecraeer.xrecyclerview.XRecyclerView>
    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:layout_alignParentBottom="true"

        android:background="@android:color/white"

        android:gravity="center_vertical">

        <CheckBox

            android:id="@+id/checkbox2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerVertical="true"

            android:layout_marginLeft="10dp"

            android:focusable="false" />

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="50dp"

            android:layout_centerVertical="true"

            android:layout_marginLeft="10dp"

            android:layout_toRightOf="@+id/checkbox2"

            android:gravity="center_vertical"

            android:text="全选"

            android:textSize="20sp" />

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentRight="true"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginLeft="10dp"

                android:text="合计 :" />

            <TextView

                android:id="@+id/price"

                android:layout_width="wrap_content"

                android:layout_height="50dp"

                android:layout_marginLeft="10dp"

                android:paddingRight="10dp"

                android:text="0"

                android:textColor="@android:color/holo_red_light" />

            <TextView

                android:id="@+id/tv_num"

                android:layout_width="wrap_content"

                android:layout_height="50dp"

                android:background="@android:color/holo_red_dark"

                android:gravity="center"

                android:padding="10dp"

                android:text="结算(0)"

                android:textColor="@android:color/white" />

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>


 加减
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#ffffff"/>
    <size android:width="100dp" android:height="30dp"/>
    <stroke android:width="1px" android:color="#999999"/>

</shape>

加减布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal"
    android:background="@drawable/jiajian">

    <TextView
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="25sp"
        android:padding="5dp"/>

    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="#999999"/>

    <EditText
        android:id="@+id/num"
        android:layout_weight="1"
        android:text="10"
        android:gravity="center"
        android:background="#00000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="#999999"/>
    <TextView
        android:id="@+id/jia"
        android:textSize="25sp"
        android:padding="5dp"
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


 

 

 

 

 

 

 

 

 

子布局

<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <CheckBox
                android:id="@+id/checkboxx"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/imagee"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titlee"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="商品标题" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp">

                <TextView
                    android:id="@+id/price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="优惠价:¥99.99"
                    android:textColor="@android:color/holo_red_light" />

                 <com.example.yuekaomoni.MyjiajianView
                     android:id="@+id/myjiajian"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_marginLeft="150dp"
                     ></com.example.yuekaomoni.MyjiajianView>

            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>





</LinearLayout>





 

 

 

 

 

 

 

 

 

 

矩形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#c0c0c0"></solid>
    <stroke android:width="1dip" android:color="#c0c0c0"></stroke>
    <padding android:bottom="5dp" android:left="12dp" android:top="5dp" android:right="12dp"></padding>
    <corners android:radius="80dp"></corners>
</shape>

package com.example.yuekaomoni;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyjiajianView extends LinearLayout {
    private TextView jiaTv,jiantv;
    private EditText numEt;
    private int num = 1;
    public MyjiajianView(Context context) {
        this(context,null);
    }

    public MyjiajianView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public  MyjiajianView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {

        View view = LayoutInflater.from(context).inflate(R.layout.jiajian,this,true);
//        addView(view);
        jiantv = view.findViewById(R.id.jian);
        jiaTv = view.findViewById(R.id.jia);
        numEt = view.findViewById(R.id.num);



        numEt.setText(num+"");



        jiaTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // num = Integer.parseInt(numEt.getText().toString());
                num++;
               // numEt.setText(num+"");
                if (jiaJianListener!=null){
                    jiaJianListener.getNum(num);
                }
            }
        });
        jiantv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // num = Integer.parseInt(numEt.getText().toString());
                num--;
                if (num<=0){
                    Toast.makeText(getContext(), "数量不能小于1", Toast.LENGTH_SHORT).show();
                    num = 1;
                }

               // numEt.setText(num+"");

                if (jiaJianListener!=null){
                    jiaJianListener.getNum(num);
                }



            }
        });

    }

    public void setNumEt(int n) {
        numEt.setText(n+"");
        num = Integer.parseInt(numEt.getText().toString());
    }

    private JiaJianListener jiaJianListener;

    public void setJiaJianListener(JiaJianListener jiaJianListener) {
        this.jiaJianListener = jiaJianListener;
    }

    public interface JiaJianListener{
        void getNum(int num);
    }

}

 

自定义view

package com.example.yuekaomoni;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class MyView extends ViewGroup {
    private int mleftMargin = 20;
    private int mtopMargin = 20;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
     //绘制
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        int leftMargin = mleftMargin;
        int topMargin = mtopMargin;
         //宽度
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        //高度
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
         //判断
        switch (modeHeight){
            case MeasureSpec.AT_MOST:
                int measuredHeight = 0;
                //循环遍历得到子的元素
                for (int j=0;j<getChildCount();j++){
                    int measuredWidth = getChildAt(j).getMeasuredWidth();
                    measuredHeight = getChildAt(j).getMeasuredHeight();
                    if (leftMargin+measuredWidth+mleftMargin>getWidth()){
                        leftMargin = mleftMargin;
                        topMargin+=measuredHeight+mtopMargin;
                    }
                    leftMargin+=measuredWidth+mleftMargin;
                }
                topMargin+=measuredHeight+mtopMargin;
                break;
        }
        setMeasuredDimension(sizeWidth,topMargin);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int leftMargin = mleftMargin;
        int topMargin = mtopMargin;

        for (int j=0;j<getChildCount();j++){
            int measuredWidth = getChildAt(j).getMeasuredWidth();
            int measuredHeight = getChildAt(j).getMeasuredHeight();

            if (leftMargin+measuredWidth+mleftMargin>getWidth()){
                leftMargin=mleftMargin;
                topMargin+=measuredHeight+mtopMargin;
                getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
            }else {
                getChildAt(j).layout(leftMargin,topMargin,leftMargin+measuredWidth,topMargin+measuredHeight);
            }
            leftMargin+=measuredWidth+mleftMargin;
        }
    }


}

 

 

package com.example.yuekaomoni.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.yuekaomoni.MyjiajianView;
import com.example.yuekaomoni.R;
import com.example.yuekaomoni.bean.Shop;
import com.example.yuekaomoni.event.OnPriceEvent;


import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final String TAG = MyAdapter.class.getSimpleName();
    private Context context;
    private Shop shop;
    private List<Shop.DataBean> list;

    public MyAdapter(Context context, Shop shop) {
        this.context = context;
        this.shop = shop;
        list = shop.getData();
    }

    public void addData(List<Shop.DataBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.myadapter, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final MyViewHolder holder1 = (MyViewHolder) holder;
        final Shop.DataBean bean = list.get(position);

        String[] imgs = shop.getData().get(position).getImages().split("\\|");
        Glide.with(context).load(imgs[0]).into(holder1.imageView);
        holder1.title.setText(shop.getData().get(position).getTitle());
        holder1.myjiajian.setNumEt(bean.getSalenum());
        holder1.price.setText(shop.getData().get(position).getPrice());
        holder1.checkBox.setChecked(list.get(position).ischecked());
        holder1.myjiajian.setJiaJianListener(new MyjiajianView.JiaJianListener() {
            @Override
            public void getNum(int num) {
                bean.setSalenum(num);
                bean.setIschecked(true);
               // holder1.checkBox.setChecked(true);
                Log.i(TAG, "postion:" + position + "bean" + bean.ischecked());
                notifyDataSetChanged();
            }
        });
        /*holder1.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //list.get(position).setIschecked(isChecked);
              //  bean.setIschecked(isChecked);
                initprice();
            }
        });*/
        holder1.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setIschecked(!list.get(position).ischecked());
                notifyDataSetChanged();
            }
        });
        initprice();
    }

    private void initprice() {
        double price = 0;
        if (onPriceEvent != null) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).ischecked()) {
                    price += Double.parseDouble(list.get(i).getPrice()) * list.get(i).getSalenum();
                }
            }
        }
        onPriceEvent.price(price);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView price;
        private ImageView imageView;
        private CheckBox checkBox;
        private MyjiajianView myjiajian;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.titlee);
            price = itemView.findViewById(R.id.price);
            imageView = itemView.findViewById(R.id.imagee);
            checkBox = itemView.findViewById(R.id.checkboxx);
            myjiajian = itemView.findViewById(R.id.myjiajian);



        }
    }

    //全选全不选
    public void selectAll(boolean quanxuan) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIschecked(quanxuan);
        }
        notifyDataSetChanged();
    }

    private OnPriceEvent onPriceEvent;

    public void setOnPriceEvent(OnPriceEvent onPriceEvent) {
        this.onPriceEvent = onPriceEvent;
    }
}

 

 

package com.example.yuekaomoni;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuekaomoni.View.IRecyView;
import com.example.yuekaomoni.adapter.MyAdapter;
import com.example.yuekaomoni.bean.Shop;
import com.example.yuekaomoni.event.OnPriceEvent;
import com.example.yuekaomoni.persenter.RecyPersenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IRecyView {
       private XRecyclerView xRecyclerView;
       private MyAdapter adapter;
       private RecyPersenter persenter;
       private int page=1;
       private CheckBox checkBox;
       private TextView pricee;
       private EditText editText;
       private Button button;
       private Context context;
       private MyView myView;
       private LayoutInflater layoutInflater;
       private List<String>  list=new ArrayList<>();
       private TextView studyy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        persenter=new RecyPersenter(this);
        persenter.Datashop();
        initData();
        initView();
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                persenter.Datashop();
            }

            @Override
            public void onLoadMore() {
                page++;
                persenter.Datashop();
            }
        });

    }

    private void initView() {
          editText=findViewById(R.id.edittext);
          button=findViewById(R.id.but1);
          myView=findViewById(R.id.myview);
         layoutInflater=LayoutInflater.from(context);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 String s=editText.getText().toString();
                 View view=LayoutInflater.from(context).inflate(R.layout.study,null);
                 studyy=view.findViewById(R.id.studyy);
                 list.add(s);
                 for (int i=0;i<list.size();i++){
                     studyy.setText(list.get(i));
                 }
               myView.addView(view);
             }

         });


    }

    private void initData() {
          xRecyclerView=findViewById(R.id.xre);
          checkBox=findViewById(R.id.checkbox2);
          pricee=findViewById(R.id.price);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.setLoadingMoreEnabled(true);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  adapter.selectAll(isChecked);
            }
        });
    }

    @Override
    public void shopData(Shop shop) {
        if (page==1){
             adapter=new MyAdapter(this,shop);
              adapter.setOnPriceEvent(new OnPriceEvent() {
                  @Override
                  public void price(double price) {
                      pricee.setText(String.valueOf(price));
                  }
              });
             xRecyclerView.setAdapter(adapter);
             xRecyclerView.refreshComplete();
        }else {
            if (adapter!=null){
                adapter.addData(shop.getData());
                xRecyclerView.loadMoreComplete();
            }
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值