购物车布局+代码

//布局文件

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">
        <TextView
            android:text="购物车"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="30dp" />
<ImageView
    android:id="@+id/bianji"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:src="@mipmap/ic_launcher"/>

    </LinearLayout>
    <LinearLayout

        android:id="@+id/linn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="40dp"
           android:orientation="horizontal">
           <Button
               android:id="@+id/denglu"
               android:text="登陆"
               android:layout_marginLeft="10dp"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <TextView
               android:text="登录后同步电脑到手机购物车中商品"
               android:gravity="center_vertical"
               android:layout_width="match_parent"
               android:layout_height="40dp" />
       </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="购物车是空的"/>

        </LinearLayout>
        <LinearLayout
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/miaosha"
                android:text="逛逛秒杀"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/guanzhu"
                android:text="看看关注"
                android:textColor="#FF00"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:visibility="gone"
        android:id="@+id/lins"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ListView
            android:id="@+id/glv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>
    </LinearLayout>





    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:background="#FFFF"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
        <CheckBox
            android:id="@+id/quan"
            android:layout_width="70dp"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:text="全选"/>
        <TextView
            android:id="@+id/heji"
            android:layout_weight="2"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="40dp"
            android:text="合计:¥0.00"/>

        <Button
            android:id="@+id/jiesuan"
            android:layout_weight="1"
            android:background="#FF00"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去结算(0)"/>


    </LinearLayout>




//购物车页面

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dell.wanghao2011.Model.MyDean.MyDeteAdd;
import com.example.dell.wanghao2011.Model.MyDean.MyDeteGouWu;
import com.example.dell.wanghao2011.Model.MyDean.MyDingDan;
import com.example.dell.wanghao2011.Model.MyDean.MySelectDingDan;
import com.example.dell.wanghao2011.Presenter.Presenter;
import com.example.dell.wanghao2011.R;
import com.example.dell.wanghao2011.View.Adepert.Adeperts;
import com.example.dell.wanghao2011.View.MainActivity;
import com.example.dell.wanghao2011.View.Port.Main1;

import java.util.List;

/**
 * Created by dell on 2017/12/18.
 */

public class FragmentGouwu extends Fragment implements Main1{

    private View view;
    private ImageView img;
    private Button miaosha;
    private Button guanzhu;
    private Button denglu;
    public CheckBox quan;
    private Button jiesuan;
    private TextView heji;
    private ListView lv;
    private LinearLayout lins;
    private Presenter pr;
    private LinearLayout linn;
    double precto=0;
    private SharedPreferences xuan;
    private MainActivity ma;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragmentgouwuche, container, false);
        img = (ImageView) view.findViewById(R.id.bianji);
        miaosha = (Button) view.findViewById(R.id.miaosha);
        guanzhu = (Button) view.findViewById(R.id.guanzhu);
        denglu = (Button) view.findViewById(R.id.denglu);
        lv = (ListView)view.findViewById(R.id.glv);
        heji = (TextView)view.findViewById(R.id.heji);
        jiesuan = (Button) view.findViewById(R.id.jiesuan);
        lins = (LinearLayout)view.findViewById(R.id.lins);
        linn = (LinearLayout)view.findViewById(R.id.linn);
        quan = (CheckBox)view.findViewById(R.id.quan);
        pr = new Presenter(this);
        ma = (MainActivity) getActivity();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        pr.getgou("https://www.zhaoapi.cn/product/getCarts?uid=3907&token=A5F6240BAE036F370EEC5F7F7520DE1A&source=android");
        linn.setVisibility(View.GONE);
        lins.setVisibility(View.VISIBLE);
        xuan = getActivity().getSharedPreferences("xuan", getActivity().MODE_PRIVATE);

        quan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean ck = quan.isChecked();
                SharedPreferences.Editor edit = xuan.edit();
                if(ck){
                    edit.putString("name","全选");
                    pr.getgou("https://www.zhaoapi.cn/product/getCarts?uid=3907&token=A5F6240BAE036F370EEC5F7F7520DE1A&source=android");

                }else{
                    edit.putString("name","全不选");
                    pr.getgou("https://www.zhaoapi.cn/product/getCarts?uid=3907&token=A5F6240BAE036F370EEC5F7F7520DE1A&source=android");

                }
                edit.commit();

            }
        });



    }

    @Override
    public void gou(final MyDeteGouWu myDeteGouWu) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(myDeteGouWu.getData().size()!=0) {
                            linn.setVisibility(View.GONE);
                            List<MyDeteGouWu.DataBean> data = myDeteGouWu.getData();

                            String xu =xuan.getString("name",null);
			//判断状态全选和反选
                            Log.d("1----------------","---------------"+xu);
                            if(xu!=null){
                                if(xu.equals("全不选")){
                                    for(int i=0;i<data.size();i++){
                                        List<MyDeteGouWu.DataBean.ListBean> list = data.get(i).getList();
                                        for(int j=0;j<list.size();j++){
                                                MyDeteGouWu.DataBean.ListBean ban = list.get(j);
                                                pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+ban.getSellerid()+"&pid="+ban.getPid()+"&selected=0&num="+ban.getNum()+"");
                                        }
                                    }

                                }else if(xu.equals("全选")){
                                                for(int i=0;i<data.size();i++){
                                                    List<MyDeteGouWu.DataBean.ListBean> list = data.get(i).getList();
                                                                for(int j=0;j<list.size();j++){
                                                                                    MyDeteGouWu.DataBean.ListBean ban = list.get(j);
                                                                                    pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+ban.getSellerid()+"&pid="+ban.getPid()+"&selected=1&num="+ban.getNum()+"");

                                                                }
                                                }
                            }
                                SharedPreferences.Editor edit = xuan.edit();
                                edit.putString("name",null);
                                edit.commit();
                                FragmentGouwu fr=new FragmentGouwu();
                                ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,fr).commit();
                            }

                            Adeperts ad = new Adeperts(getActivity(), data);
                            lv.setAdapter(ad);
					//计算价格
                                        for(int i=0;i<data.size();i++){
                                            List<MyDeteGouWu.DataBean.ListBean> list = data.get(i).getList();
                                                    for(int j=0;j<list.size();j++){
                                                                    if(list.get(j).getSelected()==1){
                                                                        MyDeteGouWu.DataBean.ListBean ban = list.get(j);
                                                                        precto = ban.getNum() * ban.getBargainPrice()+precto;
                                                                    }
                                                    }
                                        }
                                        if(precto!=0){
                                            quan.setChecked(true);
                                            heji.setText("合计:¥"+precto);
                                        }else{
                                            quan.setChecked(false);
                                            heji.setText("合计:¥0.00");
                                        }
                                        jiesuan.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
						//生成订单
                                                Toast.makeText(getActivity(),""+precto,Toast.LENGTH_LONG).show();
                                                pr.getdingDan("https://www.zhaoapi.cn/product/createOrder?uid=3907&price="+precto);
                                                
                                            }
                                        });
                        }else{
                            linn.setVisibility(View.VISIBLE);
                            lins.setVisibility(View.GONE);
                        }




                    }
                });
    }

    @Override
    public void add(MyDeteAdd myDeteAdd) {

    }

    @Override
    public void dingDan(final MyDingDan myDeteAdd) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
		//订单是否创建成功
                Toast.makeText(getActivity(),""+myDeteAdd.getMsg(),Toast.LENGTH_LONG).show();

                if("0".equals(myDeteAdd.getCode())){
			//判断订单是否创建成功
				FragmentDingDan din=new FragmentDingDan();
                              ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,din).commit();
} } }); } @Override public void selectDingDan(MySelectDingDan mySelectDingDan) { }}




//布局文件


  android:orientation="vertical"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
       >
        <CheckBox
            android:id="@+id/gxuan"
            android:layout_width="40dp"
            android:layout_height="30dp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gdianpu"/>

    </LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/grecy"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>





//      listView适配器


public class Adeperts extends BaseAdapter implements Main1 {
    private final Presenter pr;
    List<MyDeteGouWu.DataBean> data;
    Context context;

    public Adeperts(Context context,List<MyDeteGouWu.DataBean> data) {
        this.context = context;
        this.data=data;
        pr = new Presenter(this);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
            final ViewHodels hd;
        final MainActivity ma= (MainActivity) context;
        final FragmentGouwu fr=new FragmentGouwu();
        final MyDeteGouWu.DataBean dataBean = data.get(i);
            if(view==null){
                view=View.inflate(context, R.layout.dianpus,null);
                hd=new ViewHodels();
               hd.xuan= (CheckBox) view.findViewById(R.id.gxuan);
                hd.dianpu= (TextView) view.findViewById(R.id.gdianpu);
                hd.recy=(RecyclerView)view.findViewById(R.id.grecy);
                view.setTag(hd);
            }else{
                hd=(ViewHodels) view.getTag();
            }
        hd.dianpu.setText(data.get(i).getSellerName());
        RecyGouWu re=new RecyGouWu(context,data.get(i).getList(),data);
        hd.recy.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        hd.recy.setAdapter(re);
        for(int j=0;j<data.get(i).getList().size();j++){
            MyDeteGouWu.DataBean.ListBean listBean = dataBean.getList().get(j);
           if(listBean.getSelected()!=1){
               hd.xuan.setChecked(false);
               break;
           }else{
               hd.xuan.setChecked(true);
           }
        }

        hd.xuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean ck = hd.xuan.isChecked();

               if(ck){
                   for(int j=0;j<data.get(i).getList().size();j++){
                       MyDeteGouWu.DataBean.ListBean listBean = dataBean.getList().get(j);
                       pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+listBean.getSellerid()+"&pid="+listBean.getPid()+"&selected=1&num="+listBean.getNum()+"");
                   }} else{
                   for(int j=0;j<data.get(i).getList().size();j++){
                       MyDeteGouWu.DataBean.ListBean listBean = dataBean.getList().get(j);
                       pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+listBean.getSellerid()+"&pid="+listBean.getPid()+"&selected=0&num="+listBean.getNum()+"");
                   }
                   }
                ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,fr).commit();
               }

        });
        return view;
    }

    @Override
    public void gou(MyDeteGouWu myDeteGouWu) {

    }

    @Override
    public void add(MyDeteAdd myDeteAdd) {

    }

    @Override
    public void dingDan(MyDingDan myDeteAdd) {

    }

    @Override
    public void selectDingDan(MySelectDingDan mySelectDingDan) {

    }

    class ViewHodels{
        CheckBox xuan;
        TextView dianpu;
        RecyclerView recy;


    }
}


//布局文件

<CheckBox
    android:id="@+id/sxuan"
    android:layout_width="40dp"
    android:layout_height="100dp" />
    <ImageView
        android:id="@+id/simg"
        android:layout_width="70dp"
        android:layout_height="100dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/stele"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
        <TextView
            android:layout_width="60dp"
            android:layout_weight="1"
            android:gravity="fill"
            android:layout_height="wrap_content"

            android:id="@+id/spric"/>

            <LinearLayout
                android:layout_weight="1"
                android:gravity="right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/sjian"
                    android:layout_width="60dp"
                    android:layout_height="40dp"
                    android:text="-"/>
                <TextView
                    android:id="@+id/snum"
                    android:layout_width="60dp"
                    android:layout_height="40dp"
                    android:text="1"/>
                <Button
                    android:id="@+id/sjia"
                    android:layout_width="60dp"
                    android:layout_height="40dp"
                    android:text="+"/>






            </LinearLayout>
        </LinearLayout>


    </LinearLayout>


//    RecyclerView适配器


extends RecyclerView.Adapter<ViewHodelGou> implements Main1 {
    List<MyDeteGouWu.DataBean> data;
    List<MyDeteGouWu.DataBean.ListBean> list;
    Context context;
    private final Presenter pr;
    public RecyGouWu(Context context, List<MyDeteGouWu.DataBean.ListBean> list, List<MyDeteGouWu.DataBean> data) {
        this.context = context;
        this.data=data;
        this.list=list;
       pr = new Presenter(this);
    }

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

    @Override
    public void onBindViewHolder(final ViewHodelGou holder, final int position) {
                holder.tele.setText(list.get(position).getTitle());
                holder.spric.setText("¥"+list.get(position).getBargainPrice());
                String[] sp = list.get(position).getImages().split("\\!");
                Glide.with(context).load(sp[0]).into(holder.simg);
                holder.snum.setText(list.get(position).getNum()+"");
        final MainActivity ma= (MainActivity) context;
        final FragmentGouwu fr=new FragmentGouwu();

        int se = list.get(position).getSelected();
        if(se==1){
            holder.sxuan.setChecked(true);
        }else{
            holder.sxuan.setChecked(false);
        }
        holder.sxuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = holder.sxuan.isChecked();
                if(checked){
                    pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=1&num="+list.get(position).getNum()+"");
                }else{
                    pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=0&num="+list.get(position).getNum()+"");
                }
                notifyDataSetChanged();
                ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,fr).commit();
            }
        });


                holder.sjia.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int in = list.get(position).getNum() + 1;
                        boolean checked = holder.sxuan.isChecked();

                        if(checked){
                            pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=1&num="+in+"");
                            }else{
                            pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=0&num="+in+"");
                             }
                            notifyDataSetChanged();
                        ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,fr).commit();
                    }
                });
                holder.sjian.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int in = 1;
                        if(list.get(position).getNum()>1){
                            in=list.get(position).getNum() -1;
                        }

                        boolean checked = holder.sxuan.isChecked();

                        if(checked){
                            pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=1&num="+in+"");
                        }else{
                            pr.getupdate("https://www.zhaoapi.cn/product/updateCarts?uid=3907&sellerid="+list.get(position).getSellerid()+"&pid="+list.get(position).getPid()+"&selected=0&num="+in+"");

                        }
                        notifyDataSetChanged();

                        ma.getSupportFragmentManager().beginTransaction().replace(R.id.frag,fr).commit();
                    }
                });

    }

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

    @Override
    public void gou(MyDeteGouWu myDeteGouWu) {

    }

    @Override
    public void add(MyDeteAdd myDeteAdd) {

    }

    @Override
    public void dingDan(MyDingDan myDeteAdd) {

    }

    @Override
    public void selectDingDan(MySelectDingDan mySelectDingDan) {

    }
}



//Viewhodel



 extends RecyclerView.ViewHolder{

    public final TextView spric;
    public  final ImageView simg;
    public  final Button sjian;
    public  final Button sjia;
    public  final CheckBox sxuan;
    public  final TextView snum;
    public  final TextView tele;

    public ViewHodelGou(View itemView) {
        super(itemView);
        sjia = (Button) itemView.findViewById(R.id.sjia);
        sjian = (Button)itemView.findViewById(R.id.sjian);
        simg = (ImageView) itemView.findViewById(R.id.simg);
        spric = (TextView)   itemView.findViewById(R.id.spric);
        sxuan = (CheckBox) itemView.findViewById(R.id.sxuan);
        snum= (TextView)  itemView.findViewById(R.id.snum);
        tele = (TextView) itemView.findViewById(R.id.stele);
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值