购物车Recyclerview实现

Main_xml

<?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="match_parent"
    tools:context="com.bwie.goodscar.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/r1_review"
        android:layout_above="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
    <RelativeLayout
    android:id="@+id/b"
    android:padding="10dp"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/selectall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/j"
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:text="价钱:¥"
            android:textColor="#000"
            android:layout_toRightOf="@+id/selectall"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/sumnoney"
            android:layout_width="wrap_content"
            android:padding="6dp"
            android:text="money"
            android:textSize="17dp"
            android:textColor="#c00"
            android:layout_toRightOf="@+id/j"
            android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>

 child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/boss_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:background="#ccc"
        android:layout_toRightOf="@+id/boss_all"
        android:id="@+id/boss"
        android:padding="10dp"
        android:text="boss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <android.support.v7.widget.RecyclerView
        android:layout_below="@+id/boss"
        android:id="@+id/re2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</RelativeLayout>
childitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
    android:id="@+id/good_cb"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/good_iv"
        android:layout_toRightOf="@+id/good_cb"
        android:src="@mipmap/ic_launcher"
        android:layout_width="80dp"
        android:layout_height="80dp" />
    <TextView
        android:id="@+id/good_shop"
        android:padding="6dp"
        android:layout_toRightOf="@+id/good_iv"
        android:text="店名"
        android:textSize="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/buy_content"
        android:text="context"
        android:layout_below="@+id/good_shop"
        android:layout_toRightOf="@+id/good_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:textColor="#c00"
        android:id="@+id/good_money"
        android:text="context"
        android:layout_below="@+id/buy_content"
        android:layout_toRightOf="@+id/good_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/good_j"
        android:text="X "
        android:layout_marginLeft="80dp"
        android:layout_below="@+id/buy_content"
        android:layout_toRightOf="@+id/good_money"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/good_num"
        android:text="mone "
        android:layout_below="@+id/buy_content"
        android:layout_toRightOf="@+id/good_j"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
在主Activity里面
package com.bwie.goodscar;

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

import com.bwie.goodscar.adapter.MyAdapter;
import com.bwie.goodscar.bean.Bean;
import com.bwie.goodscar.utils.NetUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recycle;
    private CheckBox cball;
    private TextView money;
    private List<Bean.DataBean> data;
    private MyAdapter ada;
    private int kb;
    private double money1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initdata();
        setlistener();
    }

    private void setlistener() {
        cball.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(cball.isChecked()){
                    for (Bean.DataBean bean : data) {
                        bean.setFlag(true);
                        List<Bean.DataBean.ListBean> list = bean.getList();
                        for (Bean.DataBean.ListBean listBean : list) {
                            listBean.setSelected(1);
                        }
                    }
                }else{
                    for (Bean.DataBean bean : data) {
                        bean.setFlag(false);
                        List<Bean.DataBean.ListBean> list = bean.getList();
                        for (Bean.DataBean.ListBean listBean : list) {
                            listBean.setSelected(0);
                        }
                    }
                }
                ada.notifyDataSetChanged();

            }
        });
    }

    private void initdata() {
        NetUtils.getResult(Api.LOOK_URL, new NetUtils.CallResult() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) {

                try {
                    String str=response.body().string();
                    Gson gson=new Gson();
                    Bean bean = gson.fromJson(str, Bean.class);

                    data = bean.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ada= new MyAdapter((ArrayList<Bean.DataBean>) data,MainActivity.this);
                            recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
                            recycle.setAdapter(ada);
                            //回调的钱的接口
                            ada.setMoneyResult(new MyAdapter.MoneyResult() {
                                @Override
                                public void money() {
                                     kb= 0;
                                     money1= 0;
                                    for (Bean.DataBean dataBean : data) {
                                        List<Bean.DataBean.ListBean> been = dataBean.getList();
                                        for (Bean.DataBean.ListBean listBean : been) {

                                            if(listBean.getSelected()==1){
                                                kb++;
                                                money1+=listBean.getBargainPrice()*listBean.getNum();
                                            }
                                        }
                                    }
                                    //设置总价
                                    money.setText(money1+"");
                                }
                            });

                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    private void initview() {
        recycle = (RecyclerView) findViewById(R.id.r1_review);
        cball = (CheckBox) findViewById(R.id.selectall);
        money = (TextView) findViewById(R.id.sumnoney);

    }
}
第一个适配器

 
package com.bwie.goodscar.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import com.bwie.goodscar.R;
import com.bwie.goodscar.bean.Bean;

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



public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private ArrayList<Bean.DataBean> list;
    private Context context;
    private ChildAdapter ada;

    public MyAdapter(ArrayList<Bean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.item,null);
        return new MyHolder(view);
    }

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

        holder.title.setText(list.get(position).getSellerName());
        final List<Bean.DataBean.ListBean> l = this.list.get(position).getList();

        ada = new ChildAdapter((ArrayList<Bean.DataBean.ListBean>) l,context);
        holder.lv.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        holder.lv.setAdapter(ada);

        //根据数据,状态改变
        holder.cb.setChecked(list.get(position).isFlag());

        //商家改变子商品
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).setFlag(!list.get(position).isFlag());
                for (int i = 0; i < l.size(); i++) {
                    if (list.get(position).isFlag()){
                        l.get(i).setSelected(1);
                    }else {
                        l.get(i).setSelected(0);
                    }
                }
                holder.lv.setAdapter(ada);
                notifyDataSetChanged();
              moneyResult.money();
            }
        });

        //计算钱
        ada.setCallMoney(new ChildAdapter.CallMoney() {
            @Override
            public void changeMoney() {
                moneyResult.money();
            }
        });

        //子改变商家cb
        ada.setOnItemAllClick(new ChildAdapter.OnItemAllClick() {
            @Override
            public void all() {
                list.get(position).setFlag(true);
                holder.cb.setChecked(true);
                notifyDataSetChanged();
            }
            @Override
            public void noall() {
                list.get(position).setFlag(false);
                holder.cb.setChecked(false);
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyHolder extends RecyclerView.ViewHolder{
        private RecyclerView lv;
        private TextView title;
        private CheckBox cb;
        public MyHolder(View itemView) {
            super(itemView);
            lv=itemView.findViewById(R.id.re2);
            title=itemView.findViewById(R.id.boss);
            cb=itemView.findViewById(R.id.boss_all);
        }
    }
    private MoneyResult moneyResult;

    public void setMoneyResult(MoneyResult moneyResult) {
        this.moneyResult = moneyResult;
    }

    public interface MoneyResult{
        void money();
    }
}
第二个适配器
package com.bwie.goodscar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.goodscar.R;
import com.bwie.goodscar.bean.Bean;

import java.util.ArrayList;



public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.MHolder> {
    private ArrayList<Bean.DataBean.ListBean> list;
    private Context context;

    public ChildAdapter(ArrayList<Bean.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.childitem,null);
        return new MHolder(view);
    }

    @Override
    public void onBindViewHolder(final MHolder holder, final int position) {
        holder.title.setText(list.get(position).getSubhead());
        holder.title.setMaxLines(1);//限制行
        holder.money.setText(list.get(position).getBargainPrice()+"");
        holder.c.setText(list.get(position).getTitle());
        holder.c.setMaxLines(1);
        holder.num.setText(list.get(position).getNum()+"");
        String[] split = list.get(position).getImages().split("\\|");
        Glide.with(context).load(split[0]).into(holder.iv);

        //根据数据改变状态
        if(list.get(position).getSelected()==1){
            holder.cb.setChecked(true);
        }else{
            holder.cb.setChecked(false);
        }

        //子商品的点击事件
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.cb.isChecked()){
                    holder.cb.setChecked(true);
                    list.get(position).setSelected(1);
                }else {
                    holder.cb.setChecked(false);
                    list.get(position).setSelected(0);
                }
                all();
                notifyDataSetChanged();
                callMoney.changeMoney();
            }
        });

    }

    //判断是否是全选的回调
    private int sum=0;
    private void all() {
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getSelected()==1){
                sum++;
            }else{
                sum--;
            }
        }
        if(sum==list.size()){
            onItemAllClick.all();
        }else{
            onItemAllClick.noall();
        }
        sum=0;
    }
    private OnItemAllClick onItemAllClick;

    public void setOnItemAllClick(OnItemAllClick onItemAllClick) {
        this.onItemAllClick = onItemAllClick;
    }

    public interface OnItemAllClick{
        void all();
        void noall();
    }

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

    class MHolder extends RecyclerView.ViewHolder{
        ImageView iv;
        TextView money;
        TextView title;
        TextView c;
        CheckBox cb;
        TextView num;
        public MHolder(View itemView) {
            super(itemView);
            iv=itemView.findViewById(R.id.good_iv);
            money=itemView.findViewById(R.id.good_money);
            title=itemView.findViewById(R.id.good_shop);
            c=itemView.findViewById(R.id.buy_content);
            cb=itemView.findViewById(R.id.good_cb);
            num=itemView.findViewById(R.id.good_num);
        }
    }

    private CallMoney callMoney;

    public void setCallMoney(CallMoney callMoney) {
        this.callMoney = callMoney;
    }

    public interface CallMoney{
        void changeMoney();
    }
}
Bean类,供参考,数据可以改变
接口数据( http://120.27.23.105/product/getCarts?uid=144):个人数据,有时候会不好使

数据请求的类
package com.bwie.goodscar.utils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;



public class NetUtils {

    public static void getResult(String url, final CallResult callResult){
//        OkHttpClient client = SingleOk.getInstence()
//                .connectTimeout(10, TimeUnit.SECONDS)
//                .readTimeout(10, TimeUnit.SECONDS)
//                .writeTimeout(10, TimeUnit.SECONDS)
//                .retryOnConnectionFailure(false)
//                .build();
        OkHttpClient client =new OkHttpClient.Builder().build();
        FormBody.Builder build=new FormBody.Builder();
        RequestBody body=build.build();
        Request request=new Request.Builder().post(body).url(url).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if(callResult!=null){
                    callResult.onFailure(call,e);
                }

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(callResult!=null) {
                    callResult.onResponse(call, response);
                }
            }
        });


    }

    public interface CallResult{
        void onFailure(Call call, IOException e);
      void onResponse(Call call, Response response);
    }

}



猜你喜欢

转载自blog.csdn.net/zqj861791241/article/details/78339366