购物车

流失布局布局

<?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="com.example.liushibuju.MainActivity">
    <EditText
        android:id="@+id/ed_text"
        android:hint="请输入"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/add_btn"
        android:text="添加"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.fyales.tagcloud.library.TagCloudLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:tagSpacing="15dp"
        app:lineSpacing="10dp"/>

</LinearLayout>

购物车主页面

<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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.gouwuche.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/re_view"
    android:layout_width="match_parent"
    android:layout_weight="9"
    android:layout_height="0dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_height="0dp">
        <CheckBox
            android:id="@+id/che1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="全选"/>
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="350dp"
            android:textSize="20dp"
            android:text="总价"/>
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:textSize="20dp"
            android:text="结算"/>
    </LinearLayout>
</LinearLayout>

内部条目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <CheckBox
        android:id="@+id/che2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/img1"
        android:src="@mipmap/ic_launcher"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/text3"
        android:text="xxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.example.liushibuju.kaiguan
        android:id="@+id/aaa"
        android:layout_width="100dp"
        android:layout_height="100dp"></com.example.liushibuju.kaiguan>
</LinearLayout>

外部条目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xxxxx"
        android:textSize="15dp"
        android:id="@+id/shangjia_text"
        />

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

自定义加减布局

<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:id="@+id/btn_add"
        android:text="+"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_nub"
        android:text="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_jian"
        android:text="-"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />
</LinearLayout>

自定义加减

public class kaiguan extends LinearLayout implements View.OnClickListener {

    private Button btn_add;
    private Button btn_jian;
    private TextView text_nub;

    public kaiguan(Context context) {
        super(context);
    }

    public kaiguan(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.lllayout,this);
        btn_add = findViewById(R.id.btn_add);
        btn_jian = findViewById(R.id.btn_jian);
        text_nub = findViewById(R.id.text_nub);
        btn_add.setOnClickListener(this);
        btn_jian.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String s = text_nub.getText().toString();
        int number = Integer.parseInt(s);
        switch (v.getId()){
            case R.id.btn_add:
                number+=1;
                text_nub.setText(String.valueOf(number));
                onshowcallback.onshowcallback(number);
                break;
            case R.id.btn_jian:
                if (number<1){
                    Toast.makeText(getContext(), "最小不能小于1", Toast.LENGTH_SHORT).show();
                    return;
                }
                number-=1;
                text_nub.setText(String.valueOf(number));
                onshowcallback.onshowcallback(number);
                break;
        }
    }
    public interface Onshowcallback{
        void onshowcallback(int number);
    }
    public Onshowcallback onshowcallback;
    public void setOnshowcallback(Onshowcallback onshowcallback){
        this.onshowcallback=onshowcallback;
    }
}

流失布局

public class MainActivity extends AppCompatActivity {

    private ArrayList<String> mList;
    private TagCloudLayout mContainer;
    private TagBaseAdapter mAdapter;
    private EditText ed_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed_text = findViewById(R.id.ed_text);
        mContainer = (TagCloudLayout) findViewById(R.id.container);
        mList = new ArrayList<>();
        mList.add("中华人民共和国");
        mList.add("大韩民国");
        mList.add("日本");
        mList.add("朝鲜");
        mList.add("台湾");
        mList.add("香港特别行政区");
        mList.add("澳门特别行政区");
        mList.add("越南");
        mList.add("老挝");
        mList.add("柬埔寨");
        mList.add("泰国");
        mList.add("缅甸");
        mList.add("马来西亚");
        mList.add("新加坡");
        mList.add("印度尼西亚");
        mList.add("文莱");
        mList.add("菲律宾");
        mAdapter = new TagBaseAdapter(MainActivity.this, mList);
        findViewById(R.id.add_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = ed_text.getText().toString();
                mList.add(s);
                mAdapter.notifyDataSetChanged();
            }
        });
        mContainer.setAdapter(mAdapter);
        mContainer.setItemClickListener(new TagCloudLayout.TagItemClickListener() {
            @Override
            public void itemClick(int position) {
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }
        });
    }
}

Main2

public class Main2Activity extends AppCompatActivity implements ICont.IView{

    private ICont.IPresenter pp;
    private static final String url_string="https://www.zhaoapi.cn/product/getCarts?uid=71";
    private Context context;
    private RecyclerView re_view;
    private CheckBox shangjia_box;
    private List<news.DataBean> data;
    private Myadapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        pp = new Pp();
        pp.attachview(this);
        pp.respinfo(url_string);
        context = this;
        re_view = findViewById(R.id.re_view);
        shangjia_box = findViewById(R.id.che1);
        shangjia_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shangjia_box.isChecked()){
                    for (int i = 0; i <data.size(); i++) {
                        data.get(i).setChecked(true);
                        for (int j = 0; j < data.get(i).getList().size(); j++) {
                            data.get(i).getList().get(j).setChecked(true);
                        }
                    }
                }else {
                    for (int i = 0; i <data.size(); i++) {
                        data.get(i).setChecked(false);
                        for (int j = 0; j < data.get(i).getList().size(); j++) {
                            data.get(i).getList().get(j).setChecked(false);
                        }
                    }
                }
                myadapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void shoudata(final String string) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Gson gson = new Gson();
                news news = gson.fromJson(string, news.class);
                data = news.getData();
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                re_view.setLayoutManager(linearLayoutManager);
                myadapter = new Myadapter(context, (ArrayList<news.DataBean>) data);
                re_view.setAdapter(myadapter);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pp.datachview(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        Toast.makeText(context, "数量是:" + event.getNum() + "价格是" + event.getPrice(), Toast.LENGTH_SHORT).show();
    };
}

event


public class MessageEvent {
    private int num;
    private double price;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

外层adapter

public class Myadapter extends RecyclerView.Adapter<Myadapter.SubViewHoder>{
    Context context;
    ArrayList<news.DataBean> data;
    public Myadapter(Context context, ArrayList<news.DataBean> data) {
        this.context=context;
        this.data=data;
    }

    @Override
    public SubViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.kuailayout, parent, false);
        SubViewHoder subViewHoder = new SubViewHoder(inflate);
        return subViewHoder;
    }

    @Override
    public void onBindViewHolder(final SubViewHoder holder, final int position) {
        holder.shangjia_text.setText(data.get(position).getSellerName());
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.re_view1.setLayoutManager(linearLayoutManager);
        final Myyadapter myyadapter = new Myyadapter(context, (ArrayList<news.DataBean.ListBean>) data.get(position).getList());
        myyadapter.setOnItemClickLisentener(new Myyadapter.onItemClickLisentener() {
            @Override
            public void onItemClick(int layoutPosition) {
                Toast.makeText(context, "该条目被点了", Toast.LENGTH_SHORT).show();
            }
        });
        holder.re_view1.setAdapter(myyadapter);
        final news.DataBean dataBean = data.get(position);
        holder.shangjia_box.setOnCheckedChangeListener(null);
        holder.shangjia_box.setChecked(data.get(position).getChecked());
        holder.shangjia_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                data.get(position).setChecked(isChecked);
                for (news.DataBean.ListBean listBean:dataBean.getList()){
                    listBean.setChecked(isChecked);
                }
                notifyDataSetChanged();
            }
        });

    }

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

    class SubViewHoder extends RecyclerView.ViewHolder {

        private RecyclerView re_view1;
        private TextView shangjia_text;
        private CheckBox shangjia_box;

        public SubViewHoder(View itemView) {
            super(itemView);
            shangjia_box = itemView.findViewById(R.id.shangjia_box);
            shangjia_text = itemView.findViewById(R.id.shangjia_text);
            re_view1 = itemView.findViewById(R.id.re_view1);
        }
    }
}

内部adapter

public class Myyadapter extends RecyclerView.Adapter<Myyadapter.ShangViewHoder>{
    Context context;
    ArrayList<news.DataBean.ListBean> data;
    onItemClickLisentener onItemClickLisentener;
    news.DataBean bean;
    Myadapter myadapter;
    private int number1;

    public interface onItemClickLisentener{
        void onItemClick(int layoutPosition);
    }
    public void setOnItemClickLisentener(onItemClickLisentener onItemClickLisentener){
        this.onItemClickLisentener=onItemClickLisentener;
    }
    public Myyadapter(Context context, ArrayList<news.DataBean.ListBean> data) {
            this.context=context;
            this.data=data;
            this.bean=bean;
            this.myadapter= myadapter;
    }

    @Override
    public ShangViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.itemlayout, parent, false);
        ShangViewHoder shangViewHoder = new ShangViewHoder(inflate);
        return shangViewHoder;
    }

    @Override
    public void onBindViewHolder(final ShangViewHoder holder, int position) {

        String images = data.get(position).getImages();
        if (images.contains("|")){
            images=images.substring(0,images.indexOf("|"));
        }else {
            images = data.get(position).getImages();
        }
        Picasso.with(context).load(images).into(holder.img1);
        holder.text3.setText("单价是"+ data.get(position).getPrice());
        holder.aaa.setOnshowcallback(new kaiguan.Onshowcallback() {
            @Override
            public void onshowcallback(int number) {
                number1 = number;
                Toast.makeText(context, "单价是"+number, Toast.LENGTH_SHORT).show();
            }
        });
        holder.che2.setChecked(data.get(position).getChecked());
        holder.che2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    MessageEvent messageEvent = new MessageEvent();
                    messageEvent.setNum(number1);
                    messageEvent.setPrice(data.get(holder.getLayoutPosition()).getPrice());
                    EventBus.getDefault().post(messageEvent);
                }
            }
        });
    }

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

    class ShangViewHoder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private kaiguan aaa;
        private TextView text3;
        private CheckBox che2;
        private ImageView img1;
        private Button btn_add;
        private Button btn_jian;

        public ShangViewHoder(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            btn_add = itemView.findViewById(R.id.btn_add);
            btn_jian = itemView.findViewById(R.id.btn_jian);
            text3 = itemView.findViewById(R.id.text3);
            che2 = itemView.findViewById(R.id.che2);
            aaa = itemView.findViewById(R.id.aaa);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int layoutPosition = getLayoutPosition();
            onItemClickLisentener.onItemClick(layoutPosition);
        }
    }
    private OncheckedLisenner oncheckedLisenner;;
    interface OncheckedLisenner{
        void onchecked(int layoutPosition, boolean checked);
    }
    public void setOncheckedLisenner(OncheckedLisenner oncheckedLisenner){
        this.oncheckedLisenner=oncheckedLisenner;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值