先是布局
activity_main.xml
<?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="com.example.administrator.my1024.MainActivity"> <TextView android:gravity="center" android:text="购物车" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/xrecye" ></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:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/allCheckBox"/> <TextView android:textStyle="bold" android:layout_marginLeft="10dp" android:textSize="20sp" android:text="全选" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:padding="10dp" android:layout_marginLeft="10dp" android:layout_weight="1" android:orientation="vertical" android:layout_width="0dp" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#e53e42" android:text="总价 :¥"/> <TextView android:layout_width="70dp" android:layout_height="25dp" android:id="@+id/sumprice" android:textSize="18sp" android:text="0" android:textColor="#e53e42"/> </LinearLayout> </LinearLayout> <TextView android:layout_width="70dp" android:layout_height="40dp" android:id="@+id/buy" android:text="去结算" android:gravity="center" android:textColor="#fff" android:background="#ff00" /> </LinearLayout> </LinearLayout>
car_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" 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/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_produvt_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 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:layout_width="80dp" android:layout_height="80dp" android:id="@+id/product_icon" android:src="@mipmap/ic_launcher"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="商品标题" android:id="@+id/title"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <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" /> <com.example.administrator.my1024.zidingyi.My_add_reduce android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/jiajianqi" android:layout_alignParentRight="true"> </com.example.administrator.my1024.zidingyi.My_add_reduce> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="#999999" android:layout_marginTop="5dp"/> </LinearLayout>
num_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/jia_jian_bg" android:layout_width="wrap_content" 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代码
Api
public class Api { public final static String BASE_URL = "http://www.zhaoapi.cn/"; public final static String CART_URL = BASE_URL+"product/getCarts"; }
ICartApi
public interface ICartApi { @GET() Observable<CartBean> cart(@Url String url, @Query( "uid" ) String uid); }
MyApp
public class MyApp extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize( this ); } }
CartAllCheckLinstener
public interface CartAllCheckLinstener { void notifyAllCheckboxStantus(); }
CheckListener
public interface CheckListener { void notifyAParpen(); }
RetorfitUtil
public class RetorfitUtil { private static String BASE_URL = "https://www.zhaoapi.cn/"; private final Retrofit retrofit; public static class SingleHolder{ private static final RetorfitUtil _INSTACE = new RetorfitUtil(BASE_URL); } public static RetorfitUtil getDefault(){ return SingleHolder._INSTACE; } public RetorfitUtil(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 ); } }
IView
public interface IView { void Success(CartBean cartBean); void Failure(String msg); }
IModel
public class IModel { public Observable<CartBean> cart(String url,String uid){ return RetorfitUtil.getDefault().create( ICartApi.class ).cart( url,uid ); } }
IProsenter
public class IProsenter { private IModel iModel; IView mIview; public IProsenter(IView iView){ mIview = iView; iModel = new IModel(); } public void getCart(String uid){ iModel.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) { mIview.Success( value ); } @Override public void onError(Throwable e) { mIview.Failure( e.toString() ); } @Override public void onComplete() { } } ); } }
MainActivity
public class MainActivity extends AppCompatActivity implements IView,CartAllCheckLinstener{ @BindView(R.id.xrecye) XRecyclerView xrecye; @BindView(R.id.allCheckBox) CheckBox allCheckBox; @BindView(R.id.sumprice) TextView sumprice; @BindView(R.id.buy) TextView buy; private Unbinder unbinder; private IProsenter prosenter; private List<CartBean.DataBean> list; private String uid = "71"; private CartAdapter cartAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); unbinder = ButterKnife.bind( this ); initData(); initView(); } private void initView() { list = new ArrayList<>( ); xrecye.setLayoutManager( new LinearLayoutManager( this ) ); allCheckBox.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { 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() { prosenter = new IProsenter( this ); prosenter.getCart( uid ); } @Override public void notifyAllCheckboxStantus() { 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(); } @Override public void Success(CartBean cartBean) { if (cartBean!=null&&cartBean.getData()!=null){ list = cartBean.getData(); cartAdapter = new CartAdapter( this,list ); xrecye.setAdapter( cartAdapter ); } cartAdapter.setCartAllCheckLinstener( this ); } @Override public void Failure(String msg) { } 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 ); } }
CartAdapter
public class 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 CartAdapter.CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from( mContext ).inflate( R.layout.car_item_layout,parent,false ); CartViewHolder viewHolder = new CartViewHolder( itemView ); return viewHolder; } @Override public void onBindViewHolder(@NonNull final CartAdapter.CartViewHolder holder, int position) { final CartBean.DataBean bean = cartList.get( position ); holder.nameTv.setText( 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 v) { if (holder.checkBox.isChecked()){ bean.setSelected( true ); for (int i = 0;i<bean.getList().size();i++){ bean.getList().get( i ).setSelected( false ); } } notifyDataSetChanged(); if (cartAllCheckLinstener!=null){ cartAllCheckLinstener.notifyAllCheckboxStantus(); } } } ); } public List<CartBean.DataBean> getCartList(){ return cartList; } @Override public int getItemCount() { return cartList.size()==0?0:cartList.size(); } @Override public void notifyAParpen() { notifyDataSetChanged(); if (cartAllCheckLinstener!=null){ cartAllCheckLinstener.notifyAllCheckboxStantus(); } } @Override public void onItemMove(int fromPosition, int toPosition) { Collections.swap(cartList,fromPosition,toPosition); notifyItemMoved(fromPosition,toPosition); } @Override public void onItemDismiss(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 ProductAdapter.CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from( mContext ).inflate( R.layout.item_produvt_layout,parent,false); CartViewHolder viewHolder = new CartViewHolder( itemView ); return viewHolder; } @Override public void onBindViewHolder(@NonNull final ProductAdapter.CartViewHolder holder, int position) { final CartBean.DataBean.ListBean bean = listBeanList.get( position ); holder.priceTv.setText( "优惠价 : ¥"+bean.getBargainPrice() ); holder.titleTv.setText( bean.getTitle() ); String[] imgs = bean.getImages().split( "\\|" ); if (imgs!=null &&imgs.length>0){ Glide.with( mContext ).load( imgs[0] ).into( holder.prductIv ); }else { holder.prductIv.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.notifyAParpen(); } } } ); holder.checkBox.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { if (holder.checkBox.isChecked()){ bean.setSelected( true ); }else { bean.setSelected( false ); } if (checkListener!=null){ checkListener.notifyAParpen(); } } } ); } @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 prductIv; 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 ); prductIv = itemView.findViewById( R.id.product_icon ); myAddReduce = itemView.findViewById( R.id.jiajianqi ); } } }