购物车+流失布局实现

布局

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

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="wrap_content"
    tools:context=".activity.MainActivity">

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xrecylers"
        android:layout_above="@+id/cart_bottom_layout"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <RelativeLayout
        android:id="@+id/cart_bottom_layout"
        android:padding="5dp"
        android:background="#987f"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_centerVertical="true"
            android:id="@+id/allCheckbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/totalpriceTv"
            android:textColor="#ffffff"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/allCheckbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="总价:"/>
           <Button
               android:id="@+id/but"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginLeft="200dp"
               android:text="流失布局"

               />

        <Button
            android:id="@+id/buy"
            android:layout_alignParentRight="true"
            android:text="去结算"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

</RelativeLayout>

 

 

 

<?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:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">

        <CheckBox
            android:id="@+id/checkboxs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/senames"
            android:text="商家"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclers"
        android:layout_width="match_parent"
        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:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">

        <LinearLayout

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:orientation="horizontal">
            <CheckBox
                android:id="@+id/checkse"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <ImageView
                android:src="@mipmap/ic_launcher"
                android:id="@+id/product_icon"
                android:layout_width="80dp"
                android:layout_height="80dp"/>
        </LinearLayout>

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

            <TextView
                android:id="@+id/titles"
                android:text="商品标题"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <RelativeLayout
                android:layout_marginTop="15dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/prices"
                    android:layout_alignParentLeft="true"
                    android:text="优惠价:¥99.99"
                    android:textColor="@android:color/holo_red_light"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>



            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>

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



</LinearLayout>

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/yuanxing"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/study"
        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"
    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=".activity.ShowActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/ednames"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="请输入要搜获的商品"
            />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="搜索"
            />

    </LinearLayout>
    <com.example.zhaoday11_18.MyView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></com.example.zhaoday11_18.MyView>
</LinearLayout>

 

 

代码

package com.example.zhaoday11_18.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.zhaoday11_18.R;
import com.example.zhaoday11_18.adapter.MyshopAdapter;
import com.example.zhaoday11_18.adapter.OnShopListener;
import com.example.zhaoday11_18.bean.ShopCart;
import com.example.zhaoday11_18.mvp.presenter.ShopPresenter;
import com.example.zhaoday11_18.mvp.view.ShopView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements ShopView {

    @BindView(R.id.xrecylers)
    XRecyclerView xrecylers;
    @BindView(R.id.allCheckbox)
    CheckBox allCheckbox;
    @BindView(R.id.totalpriceTv)
    TextView totalpriceTv;
    @BindView(R.id.but)
    Button but;

    private MyshopAdapter adapter;
    private ShopPresenter presenter;
    private String uid = "71";
    private List<ShopCart.DataBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();
        presenter = new ShopPresenter(this);
        presenter.getshopcarts(uid);
    }

    private boolean isselected = true;

    private double price = 0;

    private void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        xrecylers.setLayoutManager(manager);
        xrecylers.setLoadingMoreEnabled(true);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ShowActivity.class);
                startActivity(intent);
            }
        });


        allCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                price = 0;
                if (isselected) {
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(true);
                            for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                                list.get(i).getList().get(i1).setSelecteds(true);
                                price = price + (list.get(i).getList().get(i1).getPrice() * list.get(i).getList().get(i1).getNum());
                            }
                        }
                    }
                    totalpriceTv.setText("总价:" + price);
                    isselected = false;
                } else {
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(false);
                            for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                                list.get(i).getList().get(i1).setSelecteds(false);
                                price = price + (list.get(i).getList().get(i1).getPrice() * list.get(i).getList().get(i1).getNum());
                            }
                        }
                    }
                    totalpriceTv.setText("总价:0");
                    isselected = true;
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void getsgopcarts(ShopCart shopCart) {
        list = shopCart.getData();
        adapter = new MyshopAdapter(this, shopCart);
        xrecylers.setAdapter(adapter);

        adapter.setOnShopListener(new OnShopListener() {
            @Override
            public void onshoplistener(List<ShopCart.DataBean> list, int type) {
                price = 0;
                if (type == 0) {
                    for (int i = 0; i < list.size(); i++) {
                        for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                            if (list.get(i).getList().get(i1).isSelecteds()) {
                                price = price + (list.get(i).getList().get(i1).getPrice() * list.get(i).getList().get(i1).getNum());
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < list.size(); i++) {
                        for (int i1 = 0; i1 < list.get(i).getList().size(); i1++) {
                            if (list.get(i).getList().get(i1).isSelecteds()) {
                                price = price + (list.get(i).getList().get(i1).getPrice() * list.get(i).getList().get(i1).getNum());
                            }
                        }
                    }
                }
                totalpriceTv.setText("总价:" + price);
            }


        });

    }
}

第一个适配器代码

package com.example.zhaoday11_18.adapter;

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


import com.example.zhaoday11_18.R;
import com.example.zhaoday11_18.bean.ShopCart;

import java.util.List;

import butterknife.BindView;

public class MyshopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private ShopCart shopCart;
    private List<ShopCart.DataBean> list;

    public MyshopAdapter(Context context, ShopCart shopCart) {
        this.context = context;
        this.shopCart = shopCart;
        list = shopCart.getData();
    }
    private OnShopListener onShopListener;

    public void setOnShopListener(OnShopListener onShopListener) {
        this.onShopListener = onShopListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            MyViewHolder holder1= (MyViewHolder) holder;
            holder1.senames.setText(shopCart.getData().get(position).getSellerName());
            final MyshopAdapters myshopAdapters=new MyshopAdapters(context,list.get(position).getList());
        LinearLayoutManager manager=new LinearLayoutManager(context);
        holder1.recyclers.setLayoutManager(manager);
            holder1.recyclers.setAdapter(myshopAdapters);
            holder1.checkboxs.setChecked(list.get(position).isSelected());


            myshopAdapters.setOnShopsListeners(new OnShopsListeners() {
                @Override
                public void onshopslisteners() {
                    onShopListener.onshoplistener(list,1);
                }
            });




            holder1.checkboxs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                      if (!list.get(position).isSelected()){
                          for (int i = 0; i <list.get(position).getList().size() ; i++) {
                              list.get(position).getList().get(i).setSelecteds(true);
                          }
                          list.get(position).setSelected(true);

                      }else {
                          for (int i = 0; i <list.get(position).getList().size() ; i++) {
                               list.get(position).getList().get(i).setSelecteds(false);
                          }
                          list.get(position).setSelected(false);

                      }
                      notifyItemChanged(position);
                      myshopAdapters.notifyDataSetChanged();
                }
            });





    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkboxs)
        CheckBox checkboxs;
        @BindView(R.id.senames)
        TextView senames;
        @BindView(R.id.recyclers)
        RecyclerView recyclers;

        public MyViewHolder(View itemView) {
            super(itemView);
            checkboxs=itemView.findViewById(R.id.checkboxs);
            senames=itemView.findViewById(R.id.senames);
            recyclers=itemView.findViewById(R.id.recyclers);
        }
    }
}

 

第二个适配器代码

package com.example.zhaoday11_18.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhaoday11_18.R;
import com.example.zhaoday11_18.bean.ShopCart;

import java.util.List;

import butterknife.BindView;

public class MyshopAdapters extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ShopCart.DataBean.ListBean> list;

    public MyshopAdapters(Context context, List<ShopCart.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    private OnShopsListeners onShopsListeners;

    public void setOnShopsListeners(OnShopsListeners onShopsListeners) {
        this.onShopsListeners = onShopsListeners;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
              MyViewHolder holder1= (MyViewHolder) holder;
              holder1.titles.setText(list.get(position).getTitle());
              holder1.prices.setText(list.get(position).getPrice()+"");
              String[] images=list.get(position).getImages().split("\\|");
              Glide.with(context).load(images[0]).into(holder1.productIcon);
              holder1.checkse.setChecked(list.get(position).isSelecteds());


              holder1.checkse.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if (list.get(position).isSelecteds()){
                             list.get(position).setSelecteds(false);
                      }else {
                          list.get(position).setSelecteds(true);
                      }
                      notifyItemChanged(position);
                      onShopsListeners.onshopslisteners();
                  }
              });



    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkse)
        CheckBox checkse;
        @BindView(R.id.product_icon)
        ImageView productIcon;
        @BindView(R.id.titles)
        TextView titles;
        @BindView(R.id.prices)
        TextView prices;

        public MyViewHolder(View itemView) {
            super(itemView);
            checkse=itemView.findViewById(R.id.checkse);
            productIcon=itemView.findViewById(R.id.product_icon);
            titles=itemView.findViewById(R.id.titles);
            prices=itemView.findViewById(R.id.prices);
        }
    }
}

 

流失布局代码

package com.example.zhaoday11_18;

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;
        }
    }
}

 

activity代码

package com.example.zhaoday11_18.activity;

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

import com.example.zhaoday11_18.MyView;
import com.example.zhaoday11_18.R;

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

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShowActivity extends AppCompatActivity {

    @BindView(R.id.ednames)
    EditText ednames;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.myview)
    MyView myview;

    private LayoutInflater layoutInflater;
    private Context context;
    private MyView myView;
    private List<String> list=new ArrayList<>();
    private TextView study;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        ButterKnife.bind(this);
        initData();
        context=this;

    }

    private void initData() {


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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值