一个简单的展示页面 加购物车

先是布局

activity_main.xml

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

    <com.hjm.bottomtabbar.BottomTabBar
        android:id="@+id/bottomTabBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></com.hjm.bottomtabbar.BottomTabBar>

</LinearLayout>

fragment01.xml

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

    <TextView
        android:text="新品"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:layout_marginLeft="10dp"
        android:id="@+id/xRecyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>

fragment02.xml

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

    <TextView
        android:text="222"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

activity_cart.xml

<?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=".view.CartActivity">

    <TextView
        android:gravity="center"
        android:text="购物车"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/xrecyc"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></com.jcodecraeer.xrecyclerview.XRecyclerView>
    <LinearLayout
        android:gravity="center_vertical"
        android:padding="10dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/allCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:textStyle="bold"
            android:layout_marginLeft="10dp"
            android:textSize="21sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            />
        <LinearLayout
            android:padding="10dp"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:textSize="20sp"
                    android:text="总价:¥"
                    android:textColor="#e53e42"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"  />
                <TextView
                    android:id="@+id/sumprice"
                    android:textColor="#e53e42"
                    android:layout_width="70dp"
                    android:layout_height="25dp"
                    android:textSize="18sp"
                    android:text="0" />
            </LinearLayout>

        </LinearLayout>

        <TextView
            android:id="@+id/buy"
            android:text="去结算"
            android:gravity="center"
            android:textColor="#fff"
            android:background="#ff00"
            android:layout_width="70dp"
            android:layout_height="40dp" />
    </LinearLayout>
</LinearLayout>

cart_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<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:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

        <TextView
            android:id="@+id/sellerNameTv"
            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/productXRV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

</LinearLayout>

item_product.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="215dp"
    android:layout_margin="15dp"
    android:layout_marginTop="20dp"
    android:layout_centerInParent="true"
    android:layout_height="215dp">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/item_sim"
        android:src="@mipmap/ic_launcher"
        android:layout_width="215dp"
        android:layout_height="215dp" />
    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_width="215dp"
        android:padding="5dp"
        android:background="#a0000000"
        android:layout_height="40dp">
    <TextView
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="名字"
        android:textColor="#ffffff"
        android:id="@+id/item_name"
        android:textSize="18sp"
        android:layout_width="30dp"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="价格"
        android:id="@+id/item_price"
        android:textSize="18sp"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>
</RelativeLayout>

item_product_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_marginLeft="20dp"
    android:layout_height="wrap_content">

    <LinearLayout
        android:padding="10dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <CheckBox
                android:id="@+id/productCheckBox"
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/product_icon"
                android:src="@mipmap/ic_launcher"
                android:layout_width="80dp"
                android:layout_height="80dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            >
            <TextView
                android:id="@+id/title"
                android:text="商品标题"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <RelativeLayout
                android:layout_marginTop="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/price"
                    android:layout_alignParentLeft="true"
                    android:text="优惠价:99.99"
                    android:textColor="#ff0000"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                 <bwie.com.moni915.fragment.My_add_reduce
                    android:id="@+id/jiajianqi"
                    android:layout_alignParentRight="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"></bwie.com.moni915.fragment.My_add_reduce>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
    <View
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#999999"/>
</LinearLayout>

num_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/jia_jian_bg"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/jian"
        android:text="-"
        android:textSize="25dp"
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#999999"/>

    <EditText
        android:id="@+id/num"
        android:layout_weight="1"
        android:text="1"
        android:gravity="center"
        android:background="#00000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#999999"/>

    <TextView
        android:id="@+id/jia"
        android:text="+"
        android:textSize="25dp"
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

下面是Java代码

ICartApi

public interface ICartApi {
    @GET()
    Observable<CartBean> cart(@Url String url, @Query("uid") String uid);
}

IProductApi

public interface IProductApi {
    @GET()
    Observable<ProductBean> product(@Url String url, @Query("keywords")String keywords);
}

MyApp

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

Api

public class Api {
    public final static String BASE_URL = "http://www.zhaoapi.cn/";
    public final static String PRODUCT_URL = BASE_URL+"product/searchProducts";
    public final static String CART_URL = BASE_URL+"product/getCarts";
}

CartAllCheckLinstener

public interface CartAllCheckLinstener {
    void notifyAllCheckboxStatus();
}

CheckListener

public interface CheckListener {
    void notifyParpen();
}

ItemTouchHelperAdapter

public interface ItemTouchHelperAdapter {
    //数据交换
    void onItemMove(int fromPosition,int toPosition);
    //数据删除
    void onItemDissmiss(int position);
}

SimpleItemTouchHelperCallback

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private ItemTouchHelperAdapter mAdapter;

    public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
        this.mAdapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;        //允许上下的拖动
        int swipeFlags = ItemTouchHelper.LEFT;   //只允许从右向左侧滑
        return makeMovementFlags(dragFlags,swipeFlags);
    }
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDissmiss(viewHolder.getAdapterPosition());
    }
}

RetrofitUtil

public class RetrofitUtil {
    private static String BASE_URL = "https://www.zhaoapi.cn/";
    private final Retrofit retrofit;

    public static class SingleHolder{
        private static final RetrofitUtil _INSTACE =  new RetrofitUtil(BASE_URL);
    }
    public static RetrofitUtil getDefault(){

        return SingleHolder._INSTACE;
    }

    public RetrofitUtil(String baseUrl) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(bulidokhttpclient())
                .build();
    }

    private OkHttpClient bulidokhttpclient(){
        OkHttpClient client = new OkHttpClient.Builder()
                .build();
        return client;
    }
    public <T> T create(Class<T> Clazz){
        return retrofit.create(Clazz);
    }
}

ProductModel

package bwie.com.moni915.model;

import bwie.com.moni915.api.IProductApi;
import bwie.com.moni915.bean.ProductBean;
import bwie.com.moni915.utils.RetrofitUtil;
import io.reactivex.Observable;

public class ProductModel {
    public Observable<ProductBean> product(String url,String keywords){
     return RetrofitUtil.getDefault().create(IProductApi.class).product(url,keywords);
    }
}

CartModel

package bwie.com.moni915.model;

import bwie.com.moni915.api.ICartApi;
import bwie.com.moni915.bean.CartBean;
import bwie.com.moni915.utils.RetrofitUtil;
import io.reactivex.Observable;

public class CartModel {
    public Observable<CartBean> cart(String url, String uid) {

        return RetrofitUtil.getDefault().create(ICartApi.class).cart(url, uid);
    }
}

IProductView

package bwie.com.moni915.view;

import bwie.com.moni915.bean.ProductBean;

public interface IProductView {
    void success(ProductBean productBean);
    void failure(String msg);
}

ICartView

package bwie.com.moni915.view;

import bwie.com.moni915.bean.CartBean;

public interface ICartView {
    void Success(CartBean cartBean);
    void Failure(String msg);
}

ProducePresneter

package bwie.com.moni915.presneter;

import bwie.com.moni915.bean.ProductBean;
import bwie.com.moni915.http.Api;
import bwie.com.moni915.model.ProductModel;
import bwie.com.moni915.view.IProductView;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ProducePresneter {
    private ProductModel productModel;
    IProductView miProductView;

    public ProducePresneter(IProductView iProductView) {
        miProductView = iProductView;
        productModel = new ProductModel();
    }
    public void getProduct(String keywords){
        productModel.product(Api.PRODUCT_URL,keywords)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ProductBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ProductBean value) {
                        miProductView.success(value);
                    }

                    @Override
                    public void onError(Throwable e) {
                        miProductView.failure(e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

CartPresenter

package bwie.com.moni915.presneter;

import bwie.com.moni915.bean.CartBean;
import bwie.com.moni915.http.Api;
import bwie.com.moni915.model.CartModel;
import bwie.com.moni915.view.ICartView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CartPresenter {
    private CartModel cartModel;
    ICartView miCartView;

    public CartPresenter(ICartView iCartView) {
        miCartView = iCartView;
        cartModel = new CartModel();
    }

    public void getCart(String uid) {
        cartModel.cart(Api.CART_URL, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CartBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CartBean value) {

                            miCartView.Success(value);

                    }

                    @Override
                    public void onError(Throwable e) {

                            miCartView.Failure(e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bottomTabBar)
    BottomTabBar bottomTabBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        bottomTabBar.init(getSupportFragmentManager())
                .setFontSize(8)
                .setImgSize(50,50)
                .setChangeColor(Color.RED,Color.DKGRAY)
                .setTabPadding(4,6,10)
                .addTabItem("新品",R.drawable.new_icon, Fragment01.class)
                .addTabItem("精选",R.drawable.choice_icon, Fragment02.class)
                .addTabItem("分类",R.drawable.classify_icon, Fragment03.class)
                .addTabItem("我的衣橱",R.drawable.wardrobe_icon, Fragment04.class)
                .addTabItem("个人中心",R.drawable.person_icon, Fragment05.class)
                .isShowDivider(false);
    }
}

Fragment01

public class Fragment01 extends Fragment implements IProductView {

    @BindView(R.id.xRecyclerview)
    XRecyclerView xRecyclerview;
    Unbinder unbinder;
    private View view;
    private ProducePresneter producePresneter;
    private List<ProductBean.DataBean> data;
    private ProducAdapter adapter;
    private ItemTouchHelperAdapter myAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment01, container, false);

        unbinder = ButterKnife.bind(this, view);
        initData();
        initView();
        return view;
    }

    private void initView() {
        xRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(),2,LinearLayoutManager.VERTICAL,false));
        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(myAdapter);
        //用Callback构造ItemtouchHelper
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        //调用ItemTouchHelper的attachToRecyclerView方法建立联系
        touchHelper.attachToRecyclerView(xRecyclerview);
    }

    private void initData() {
        String keywords = "手机";
        producePresneter = new ProducePresneter(this);
        producePresneter.getProduct(keywords);

    }

    @Override
    public void success(ProductBean productBean) {
        if (productBean!=null&&productBean.getData()!=null){
            data = productBean.getData();
            adapter = new ProducAdapter(getActivity(), data);

            adapter.setOnItemClickListener(new ProducAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent = new Intent(getActivity(), CartActivity.class);
                    startActivity(intent);
                }
            });
            xRecyclerview.setAdapter(adapter);

        }
    }

    @Override
    public void failure(String msg) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}

CartActivity

package bwie.com.moni915.view;

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

import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import bwie.com.moni915.R;
import bwie.com.moni915.adapter.CartAdapter;
import bwie.com.moni915.bean.CartBean;
import bwie.com.moni915.inter.CartAllCheckLinstener;
import bwie.com.moni915.presneter.CartPresenter;

public class CartActivity extends AppCompatActivity implements ICartView,CartAllCheckLinstener{

    @BindView(R.id.xrecyc)
    XRecyclerView xrecyc;
    @BindView(R.id.allCheckBox)
    CheckBox allCheckBox;
    @BindView(R.id.sumprice)
    TextView sumprice;
    @BindView(R.id.buy)
    TextView buy;
    private CartPresenter cartPresenter;
    private CartAdapter cartAdapter;
    private List<CartBean.DataBean> list;
    private String uid = "71";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initView() {
        list = new ArrayList<>();
        xrecyc.setLayoutManager(new LinearLayoutManager(this));
        allCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (allCheckBox.isChecked()) {
                    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).setSelected(true);
                            }
                        }
                    }
                } 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).setSelected(false);
                            }
                        }
                    }
                }
                cartAdapter.notifyDataSetChanged();
                totalPrice();
            }
        });
    }

    private void initData() {

        cartPresenter = new CartPresenter(this);
        cartPresenter.getCart(uid);
    }
    @Override
    public void Success(CartBean cartBean) {
        if (cartBean != null && cartBean.getData() != null) {
            list = cartBean.getData();

            cartAdapter = new CartAdapter(this, list);
            xrecyc.setAdapter(cartAdapter);
        }
        cartAdapter.setCartAllCheckLinstener(this);
    }

    @Override
    public void Failure(String msg) {

    }

    @Override
    public void notifyAllCheckboxStatus() {
        StringBuilder stringBuilder = new StringBuilder();
        if (cartAdapter != null) {
            for (int i = 0; i < cartAdapter.getCartList().size(); i++) {
                stringBuilder.append(cartAdapter.getCartList().get(i).isSelected());
                for (int i1 = 0; i1 < cartAdapter.getCartList().get(i).getList().size(); i1++) {
                    stringBuilder.append(cartAdapter.getCartList().get(i).getList().get(i1).isSelected());
                }
            }
        }
        if (stringBuilder.toString().contains("false")) {
            allCheckBox.setChecked(false);
        } else {
            allCheckBox.setChecked(true);
        }
        totalPrice();
    }

    private void totalPrice() {
        double totalprice = 0;

        for (int i = 0; i < cartAdapter.getCartList().size(); i++) {
            for (int i1 = 0; i1 < cartAdapter.getCartList().get(i).getList().size(); i1++) {
                if (cartAdapter.getCartList().get(i).getList().get(i1).isSelected()) {
                    CartBean.DataBean.ListBean listBean = cartAdapter.getCartList().get(i).getList().get(i1);
                    totalprice += listBean.getBargainPrice() * listBean.getTotalNum();
                }
            }
        }
        sumprice.setText("" + totalprice);
    }

}

ProducAdapter

public class ProducAdapter extends RecyclerView.Adapter<ProducAdapter.MyViewHolder> implements View.OnClickListener{

    private Context context;
    private List<ProductBean.DataBean> list;
    private OnItemClickListener mItemClickListener;

    private MyViewHolder holder;

    public ProducAdapter(Context context, List<ProductBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = View.inflate(context, R.layout.item_product, null);
        holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MyViewHolder myViewHolder = holder;
        holder.itemView.setTag(position);
        holder.itemName.setText(list.get(position).getTitle());
        holder.itemPrice.setText(list.get(position).getPrice());
        String[] imgs = list.get(position).getImages().split("\\|");
        holder.itemSim.setImageURI(imgs[0]);
    }

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

    @Override
    public void onClick(View view) {
        if(mItemClickListener!=null){
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_name)
        TextView itemName;
        @BindView(R.id.item_price)
        TextView itemPrice;
        @BindView(R.id.item_sim)
        SimpleDraweeView itemSim;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }
}

CartAdapter

CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> implements CheckListener,ItemTouchHelperAdapter{
    private Context mContext;
    private List<CartBean.DataBean> cartList;
    private CheckListener checkListener;
    private CartAllCheckLinstener cartAllCheckLinstener;

    public CartAdapter(Context context, List<CartBean.DataBean> list) {
        mContext = context;
        cartList = list;

    }

    public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) {
        this.cartAllCheckLinstener = cartAllCheckLinstener;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.cart_item_layout, parent, false);
        CartViewHolder viewHolder = new CartViewHolder(itemView);

        return viewHolder;
    }

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

        final CartBean.DataBean bean = cartList.get(position);
        holder.nameTv.setText(bean.getSellerName());
        Log.i("cartada",bean.getSellerName());
        holder.checkBox.setChecked(bean.isSelected());

        holder.productXRV.setLayoutManager(new LinearLayoutManager(mContext));
        final ProductAdapter productAdapter = new ProductAdapter(mContext, bean.getList());

        holder.productXRV.setAdapter(productAdapter);
        productAdapter.setCheckListener(this);

        for (int i = 0; i < bean.getList().size(); i++) {

            if(!bean.getList().get(i).isSelected()){
                holder.checkBox.setChecked(false);
                break;
            }else{
                holder.checkBox.setChecked(true);
            }
        }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.checkBox.isChecked()){
                    bean.setSelected(true);
                    for (int i = 0; i < bean.getList().size(); i++) {
                        bean.getList().get(i).setSelected(true);
                    }
                }else{
                    bean.setSelected(false);
                    for (int i = 0; i < bean.getList().size(); i++) {
                        bean.getList().get(i).setSelected(false);
                    }
                }
                notifyDataSetChanged();
                if(cartAllCheckLinstener!=null){
                    cartAllCheckLinstener.notifyAllCheckboxStatus();
                }

            }
        });
    }

    public List<CartBean.DataBean> getCartList(){
        return cartList;
    }
    @Override
    public int getItemCount() {
        return cartList.size()== 0 ? 0 : cartList.size();
    }

    @Override
    public void notifyParpen() {
        notifyDataSetChanged();
        if (cartAllCheckLinstener!=null){
            cartAllCheckLinstener.notifyAllCheckboxStatus();
        }

    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(cartList,fromPosition,toPosition);
        notifyItemMoved(fromPosition,toPosition);
    }

    @Override
    public void onItemDissmiss(int position) {
        cartList.remove(position);
        notifyItemRemoved(position);
    }

    public class CartViewHolder extends RecyclerView.ViewHolder{
        private CheckBox checkBox;
        private TextView nameTv;
        private RecyclerView productXRV;
        public CartViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.sellerCheckbox);
            nameTv = itemView.findViewById(R.id.sellerNameTv);
            productXRV = itemView.findViewById(R.id.productXRV);

        }
    }
}

ProductAdapter

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.CartViewHolder>{
    private Context mcontext;
    private List<CartBean.DataBean.ListBean> listBeanList;
    private CheckListener checkListener;
    private CartAllCheckLinstener cartAllCheckLinstener;
    public ProductAdapter(Context context, List<CartBean.DataBean.ListBean> list) {
        mcontext = context;
        listBeanList = list;

    }

    public void setCheckListener(CheckListener checkListener) {
        this.checkListener = checkListener;
    }

    public void setCartAllCheckLinstener(CartAllCheckLinstener cartAllCheckLinstener) {
        this.cartAllCheckLinstener = cartAllCheckLinstener;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mcontext).inflate(R.layout.item_product_layout, parent, false);
        CartViewHolder viewHolder = new CartViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final CartViewHolder holder, int position) {
        final CartBean.DataBean.ListBean bean = listBeanList.get(position);
        holder.priceTv.setText("优惠价:¥"+bean.getBargainPrice());
        Log.i("ssss",bean.getTitle());
        holder.titleTv.setText(bean.getTitle());
        String[] imgs = bean.getImages().split("\\|");
        if(imgs!=null&&imgs.length>0){
            Glide.with(mcontext).load(imgs[0]).into(holder.productIv);
        }else{
            holder.productIv.setImageResource(R.mipmap.ic_launcher);
        }
        holder.checkBox.setChecked(bean.isSelected());

        holder.myAddReduce.setNumEt(bean.getTotalNum());

        holder.myAddReduce.setJiaJianLinstener(new My_add_reduce.JiaJianLinstener() {
            @Override
            public void getNum(int num) {
                bean.setTotalNum(num);
                if(checkListener!=null){
                    checkListener.notifyParpen();
                }
            }
        });

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.checkBox.isChecked()){
                    bean.setSelected(true);
                }else{
                    bean.setSelected(false);
                }
                if (checkListener!=null){
                    checkListener.notifyParpen();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listBeanList.size()==0 ? 0 : listBeanList.size();
    }

    public class CartViewHolder extends RecyclerView.ViewHolder {
        private CheckBox checkBox;
        private TextView titleTv,priceTv;
        private ImageView productIv;
        private My_add_reduce myAddReduce;


        public CartViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.productCheckBox);
            titleTv = itemView.findViewById(R.id.title);
            priceTv = itemView.findViewById(R.id.price);
            productIv = itemView.findViewById(R.id.product_icon);
            myAddReduce = itemView.findViewById(R.id.jiajianqi);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/SuperZhongyulong/article/details/82800045