仿淘宝购物车 recyclerview嵌套

三个接口

public interface IPort {
    /**
     * 购物车数据 加入购物车
     *
     * @return
     */
    @FormUrlEncoded
    @POST("product/getCarts")
    Observable<Result<List<MyCar>>> addmycart(@Field("uid") int uid);
}
package com.why.test.core;

import com.why.test.bean.Result;

public interface DataCall<T> {
    void success(T data);
    void fail(Result result);
}
package com.why.test.core;

public interface GoodsCheck {
    void addGood();
    void reduceGood();
    void itemcheck();//商品复选框
}

两个adapter

店铺

public class MyFAdapter extends RecyclerView.Adapter<MyFAdapter.MyHolder> implements GoodsCheck{
    GoodsCheck goodsCheck;

    Context context;
    List<MyCar> list = new ArrayList<>();


    public MyFAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        MyCar myCar = list.get(position);
        MyCAdapter myCAdapter = new MyCAdapter(context);
        holder.child_re.setLayoutManager(new LinearLayoutManager(context));
        myCAdapter.setData(myCar.getList());
        /*

         */
        myCAdapter.setGoodsCheck(this);
        /*

         */
        holder.child_re.setAdapter(myCAdapter);
        holder.name.setText(myCar.getSellerName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

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

    public void addAll(Result<List<MyCar>> data) {
        if (data!=null){
            list.addAll(data.getData());
        }
    }

    @Override
    public void addGood() {
        goodsCheck.checked();
    }

    @Override
    public void reduceGood() {
        goodsCheck.checked();

    }

    @Override
    public void itemcheck() {
        goodsCheck.checked();

    }

   /* public void addAll(List<MyCar> data1) {
        if (data1!=null){
            list.addAll(data1);
        }
    }*/

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView name;
        RecyclerView child_re;

        public MyHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.shop_name);
            child_re = itemView.findViewById(R.id.child_recycler);
        }
    }

    //    public interface

    public interface GoodsCheck {
        void checked();//监听接口里的三个方法 自定义接口回调 可以直接返回主界面

    }

    public void setGoodsCheck(GoodsCheck goodsCheck) {
        this.goodsCheck = goodsCheck;
    }
}

商品

public class MyCAdapter extends RecyclerView.Adapter<MyCAdapter.MyHolder> {
    GoodsCheck goodsCheck;
    Context context;
    List<MyCar.ListBean> lists=new ArrayList<>();
    public MyCAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, int position) {
        final MyCar.ListBean listBean = lists.get(position);
        String images = listBean.getImages();
        String[] split = images.split("!");
        holder.c_img.setImageURI(split[0]);
        holder.price.setText("¥"+listBean.getPrice()+"");
        holder.title.setText(listBean.getTitle());
        holder.checkBox.setChecked(listBean.isChecked());

        holder.num.setText(String.valueOf(listBean.getNum()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBean.setNum(listBean.getNum()+1);
                if (holder.checkBox.isChecked()){
                    goodsCheck.addGood();
                }
                notifyDataSetChanged();
            }
        });
        holder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (0==listBean.getNum()){
                    return;
                }
                listBean.setNum(listBean.getNum()-1);
                if (holder.checkBox.isChecked()){
                    goodsCheck.reduceGood();
                }
                notifyDataSetChanged();
            }
        });
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBean.setChecked(holder.checkBox.isChecked());
                goodsCheck.itemcheck();
            }
        });

    }

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

    public void setData(List<MyCar.ListBean> datalist) {
        if (datalist!=null){
            lists.clear();
            lists=datalist;
            notifyDataSetChanged();
        }
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView c_img;
        TextView title,price,num;
        CheckBox checkBox;
        Button add,reduce;
        public MyHolder(View itemView) {
            super(itemView);
            c_img=itemView.findViewById(R.id.c_img);
            title=itemView.findViewById(R.id.c_title);
            price=itemView.findViewById(R.id.c_price);
            checkBox=itemView.findViewById(R.id.c_check);
            add=itemView.findViewById(R.id.add);
            reduce=itemView.findViewById(R.id.reduce);
            num=itemView.findViewById(R.id.num);


        }
    }

    public void setGoodsCheck(GoodsCheck goodsCheck) {
        this.goodsCheck = goodsCheck;
    }

}

主函数

public class MainActivity extends AppCompatActivity implements MyFAdapter.GoodsCheck{
    @BindView(R.id.sum)
    TextView mysum;
    private List<MyCar> data1;
    @BindView(R.id.f_recycler)
    RecyclerView fRecycler;
    @BindView(R.id.f_check)
    CheckBox fCheck;
    @BindView(R.id.f_price)
    Button fPrice;
    private MyFAdapter myFAdapter;
    private MyCarPresenter myCarPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fRecycler.setLayoutManager(new LinearLayoutManager(this));
        myFAdapter = new MyFAdapter(this);
        /*

         */
        myFAdapter.setGoodsCheck(this);
        /*

         */
        fRecycler.setAdapter(myFAdapter);
        myCarPresenter = new MyCarPresenter(new MyCarCall());
        myCarPresenter.request(71);
        fCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = fCheck.isChecked();
                for (int i = 0; i < data1.size(); i++) {
                    for (int j = 0; j < data1.get(i).getList().size(); j++) {
                        data1.get(i).getList().get(j).setChecked(checked);
                    }
                }
                if (checked) {
                    totalPrice();
                } else {
                    mysum.setText("¥0.0");
                    fPrice.setText("去结算(0)");
                }
                myFAdapter.notifyDataSetChanged();
            }
        });

    }

    private void totalPrice() {
        double sum=0;
        int num=0;
        for (int i = 0; i < data1.size(); i++) {
            for (int j = 0; j < data1.get(i).getList().size(); j++) {
                if ( data1.get(i).getList().get(j).isChecked()){
                    Log.i("++++++++++++", "getSum: "+data1.get(i).getList().get(j).getNum()*data1.get(i).getList().get(j).getPrice());
                    Log.i("++++++++++++", "getSum: "+data1.get(i).getList().get(j).getNum());
                    sum+= data1.get(i).getList().get(j).getNum()*data1.get(i).getList().get(j).getPrice();
                    num+= data1.get(i).getList().get(j).getNum();
                }
            }
        }
        mysum.setText("¥"+sum+"");
        fPrice.setText("去结算("+num+")");

    }

    @Override
    public void checked() {
        totalPrice();
        int chnum=0;
        for (int i = 0; i < data1.size(); i++) {
            for (int j = 0; j < data1.get(i).getList().size(); j++) {
                if (data1.get(i).getList().get(j).isChecked()) {
                    chnum++;
                }
            }
        }
        if (chnum==data1.size()){
            fCheck.setChecked(true);
        }else {
            fCheck.setChecked(false);

        }
    }



    private class MyCarCall implements DataCall<Result<List<MyCar>>> {


        @Override
        public void success(Result<List<MyCar>> data) {
            data1 = data.getData();
            myFAdapter.addAll(data);
            myFAdapter.notifyDataSetChanged();

        }

        @Override
        public void fail(Result result) {
            Toast.makeText(getBaseContext(), "失败了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myCarPresenter.unbind();
    }
}

Appliction

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

三个布局

主函数

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    android:orientation="vertical"
    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=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_weight="1"
        android:id="@+id/f_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选"
        android:id="@+id/f_check"
        android:textSize="16sp" />
    <TextView
        android:layout_marginLeft="10dp"
        android:text="222.30"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sum"/>

    <Button
        android:id="@+id/f_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="结算"
        android:textColor="@android:color/holo_red_light"
        android:textSize="16sp" />
</LinearLayout>
</LinearLayout>

父recyclerview

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

    <TextView
        android:text="55"
        android:layout_marginLeft="50dp"
        android:id="@+id/shop_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/child_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/shop_name"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

子recyclerview

<?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/c_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/c_img"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_toRightOf="@id/c_check"
        android:src="@drawable/ic_launcher_background" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/c_img"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:id="@+id/c_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="jhglhgjakg" />

        <TextView
            android:layout_below="@id/c_title"
            android:id="@+id/c_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="5555" />

        <LinearLayout
            android:layout_marginTop="10dp"
            android:layout_below="@id/c_title"
            android:layout_alignParentRight="true"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/reduce"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="-" />

            <TextView
                android:id="@+id/num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="55" />

            <Button
               android:gravity="center"
                android:id="@+id/add"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="+" />

        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_43603372/article/details/86560225