購物車
- //依賴
- compile 'io.reactivex.rxjava2:rxjava:2.0.1' //Rxjava
- compile 'io.reactivex.rxjava2:rxandroid:2.0.1' //Rx安卓
- compile 'com.squareup.retrofit2:retrofit:2.3.0' //Retrofit請求網絡
- compile 'com.squareup.retrofit2:converter-gson:2.3.0'//Gson
- compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
- compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'//攔截器
- compile 'com.youth.banner:banner:1.4.9' //Banner依賴
- //圖片的依賴
- compile 'com.facebook.fresco:fresco:0.14.1'
- compile 'com.facebook.fresco:animated-base-support:0.14.1'
- compile 'com.facebook.fresco:animated-gif:0.14.1'
- compile 'com.facebook.fresco:webpsupport:0.14.1'
- compile 'com.facebook.fresco:animated-webp:0.14.1'
- compile 'com.facebook.fresco:imagepipeline-okhttp3:0.14.1'
- //網絡狀態
- <uses-permission android:name="android.permission.INTERNET" />
- //sd讀寫權限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
-
- android:name=".MApp"
- 封裝Activity
- /**
- * Description:基類,封裝功用功能,方便子類繼承,子類只須要實現子類須要的功能便可
- */
- public abstract class BaseActivity extends AppCompatActivity{
-
- private boolean isStatus = false;//沉浸式透明狀態欄標示
- private boolean isShowActionBar = true;//是否隱藏actionbar
- private boolean isFullScreen = false;
-
-
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(bindLayout());
- initView();
- initData();
- setListener();
- }
-
- protected abstract int bindLayout();//綁定佈局
-
- protected abstract void initView();//初始化控件
-
- protected abstract void initData();//初始化數據
-
- protected abstract void setListener();//初始化點擊事件
-
-
- /**
- * 跳轉activity方法
- * @param clazz
- */
- public void startActivity(Class<?> clazz) {
- Intent intent = new Intent(this, clazz);
- startActivity(intent);
- }
- /**
- * 跳轉activity方法,帶參數
- * @param clazz
- */
- public void startActivity(Bundle bundle,Class<?> clazz) {
- Intent intent = new Intent(this, clazz);
- intent.putExtras(bundle);
- startActivity(intent);
- }
-
- public void setStatus(boolean status) {
- isStatus = status;
- if (isStatus){
- //判斷當前設備的版本號》=19的時候,走這個代碼塊,這個用於版本適配
- if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
-
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- }
- }
- }
-
- public void setShowActionBar(boolean showActionBar) {
- isShowActionBar = showActionBar;
-
- if (isShowActionBar){
- getSupportActionBar().show();
- }else{
- getSupportActionBar().hide();
- }
- }
-
- public void setFullScreen(boolean fullScreen) {
- isFullScreen = fullScreen;
- if (isFullScreen){//是全屏的時候
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
- }
- }
-
- public void showToast(String msg){
- Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
- }
- }
- //ApiService網絡請求接口
- public interface ApiService {
- @GET("product/getCarts?source=android")
- Observable<CartBean> rxjavaShowCart(@Query("uid") String uid);
-
- @POST("product/deleteCart")
- @FormUrlEncoded
- Observable<AddCartBean> rxjavaDeleteCart(@FieldMap Map<String, String> map);
-
- @POST("product/updateCarts")
- @FormUrlEncoded
- Observable<AddCartBean> rxjavaUpdateCarts(@FieldMap Map<String, String> map);
- }
- //model層
- //--------刪除
- public class DeleteCartModel {
- public void deleteCart(String uid,String pid,final DeleteCartCallback deleteCartCallback){
- Map<String, String> map = new HashMap<>();
- map.put("uid", uid);
- map.put("pid", pid);
- OkHttpClient okHttpClient = new OkHttpClient.Builder()
- .addNetworkInterceptor(new HttpLoggingInterceptor()
- .setLevel(HttpLoggingInterceptor.Level.BODY))
- .build();
- Retrofit retrofit = new Retrofit.Builder()
- .client(okHttpClient)
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .baseUrl(HttpConfig._URL).build();
- ApiService apiService = retrofit.create(ApiService.class);
- apiService.rxjavaDeleteCart(map).subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<AddCartBean>() {
- @Override
- public void onSubscribe(Disposable d) {
-
- }
-
- @Override
- public void onNext(AddCartBean value) {
- if (value.getCode().equals("0")) {
- deleteCartCallback.deleteCartSuccess(value);
- }
- }
-
- @Override
- public void onError(Throwable e) {
- deleteCartCallback.failure(e);
- }
-
- @Override
- public void onComplete() {
-
- }
- });
-
-
- }
- public interface DeleteCartCallback {
- void deleteCartSuccess(AddCartBean bean);//網絡請求成功
- void failure(Throwable t);//網絡請求失敗
- }
- }
- //presenter層
- public class ShowCartPresenter {
- private final IShowCartView view;
- private final ShowCartModel showCartModel;
- private final DeleteCartModel deleteCartModel;
- private final UpdateCartsModel updateCartsModel;
-
- public ShowCartPresenter(IShowCartView iShowCartView) {
- showCartModel = new ShowCartModel();
- deleteCartModel = new DeleteCartModel();
- updateCartsModel = new UpdateCartsModel();
- this.view=iShowCartView;
- }
- public void showCart(String uid) {
- showCartModel.showCart(uid, new ShowCartModel.ShowCartCallback() {
- @Override
- public void showCartSuccess(CartBean bean) {
- List<CartBean.DataBean> data = bean.getData();
- view.showCartSuccess(data);
- }
-
- @Override
- public void failure(Throwable t) {
- view.showCartfailure(t);
- }
- });
- }
-
- public void deleteCart(String uid,String pid){
- deleteCartModel.deleteCart(uid, pid, new DeleteCartModel.DeleteCartCallback() {
- @Override
- public void deleteCartSuccess(AddCartBean bean) {
- view.deleteCartSuccess(bean.getMsg());
- }
-
- @Override
- public void failure(Throwable t) {
- view.deleteCartfailure(t);
- }
- });
- }
- public void updateCarts(String uid,String sellerid,String pid,String selected,String num){
- updateCartsModel.updateCarts(uid, sellerid, pid, selected, num, new UpdateCartsModel.UpdateCartsCallback() {
- @Override
- public void updateCartsSuccess(AddCartBean bean) {
- view.updateCartsSuccess(bean.getMsg());
- }
-
- @Override
- public void failure(Throwable t) {
- view.updateCartsfailure(t);
- }
- });
- }
- }
- //view層
- public interface IShowCartView {
- void showCartSuccess(List<CartBean.DataBean> data);//網絡請求成功
- void showCartfailure(Throwable t);//網絡請求失敗(斷網,服務器崩潰,接口地址不正確,弱網)
- void jisuan(String count,String priceAll);
- void deleteCartSuccess(String str);//網絡請求成功
- void deleteCartfailure(Throwable t);//網絡請求失敗(斷網,服務器崩潰,接口地址不正確,弱網)
- void updateCartsSuccess(String str);//網絡請求成功
- void updateCartsfailure(Throwable t);//網絡請求失敗(斷網,服務器崩潰,接口地址不正確,弱網)
- }
- //自定義加減器
- public class AddMinusView extends LinearLayout {
-
- private OnAddMinusClickListener listener;
- private EditText etNumber;
-
- //對外提供一個點擊的回調接口
- public interface OnAddMinusClickListener{
- void onAddClick(View v);
- void onMinusClick(View v);
- void onEtNumberClick(View v);
- }
-
- public void setOnAddMinusClickListener(OnAddMinusClickListener listener){
- if(listener!=null){
- this.listener=listener;
- }
- }
- public AddMinusView(Context context) {
- this(context,null);
- }
-
- public AddMinusView(Context context, @Nullable AttributeSet attrs) {
- this(context, attrs,0);
- }
-
- public AddMinusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- initView(context, attrs, defStyleAttr);
- }
-
- private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
- View.inflate(context, R.layout.layout_add_minus,this);
-
- //獲取控件
- TextView txtminus=findViewById(R.id.txt_minus);
- TextView txtAdd=findViewById(R.id.txt_add);
- etNumber = findViewById(R.id.et_number);
-
-
- TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddMinusViewStyle);
-
- String leftText = typedArray.getString(R.styleable.AddMinusViewStyle_left_text);
- String rightText = typedArray.getString(R.styleable.AddMinusViewStyle_right_text);
- String middleText = typedArray.getString(R.styleable.AddMinusViewStyle_middle_text);
- int color = typedArray.getColor(R.styleable.AddMinusViewStyle_left_text_color, Color.RED);
-
- txtminus.setText(leftText);
- txtAdd.setText(rightText);
- etNumber.setText(middleText);
- txtminus.setTextColor(color);
-
- //回收
- typedArray.recycle();
-
-
- txtminus.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- listener.onMinusClick(view);
- }
- });
-
- txtAdd.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- listener.onAddClick(view);
- }
- });
- etNumber.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- listener.onEtNumberClick(view);
- }
- });
-
- }
- //對外提供一個修改數字的方法
- public void setNumber(int number){
- if(number>0){
- etNumber.setText(number+"");
- }
- }
- //對外提供一個獲取當前數字的方法
- public int getNumber(){
- String string = etNumber.getText().toString();
- int i = Integer.parseInt(string);
- return i;
- }
- }
-
- //---XML
- <LinearLayout
- android:orientation="horizontal"
- xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/txt_minus"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:gravity="center"
- android:background="@drawable/shopcart_minus_grey"/>
-
- <EditText
- android:id="@+id/et_number"
- android:layout_marginTop="2dp"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:background="@drawable/edit"
- android:layout_weight="0.00"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:id="@+id/txt_add"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:gravity="center"
- android:background="@drawable/shopcart_add_red"/>
- </LinearLayout>
- //MainActivity
- public class MainActivity extends BaseActivity implements IShowCartView,View.OnClickListener {
-
- private ExpandableListView expandable;
- private CheckBox checkBox;
- private TextView cart_priceAll;
- private TextView cart_count;
- private List<CartBean.DataBean> data;
- private CartAdapter cartAdapter;
- private ShowCartPresenter showCartPresenter;
- private TextView cart_editor;
- private boolean flag=true;
- private Button cart_jiesuan;
- private Button cart_delete;
-
- @Override
- protected int bindLayout() {
- return R.layout.activity_main;
- }
-
- @Override
- protected void initView() {
- showCartPresenter = new ShowCartPresenter(this);
- showCartPresenter.showCart("12400");
- cart_editor = findViewById(R.id.cart_editor);
- expandable = findViewById(R.id.cart_expandable);
- checkBox = findViewById(R.id.cart_checkBox);
- cart_priceAll = findViewById(R.id.cart_priceAll);
- cart_count = findViewById(R.id.cart_count);
- cart_jiesuan = findViewById(R.id.cart_jiesuan);
- cart_delete = findViewById(R.id.cart_delete);
- }
-
- @Override
- protected void initData() {
-
- }
-
- @Override
- protected void setListener() {
- checkBox.setOnClickListener(this);
- cart_editor.setOnClickListener(this);
- cart_delete.setOnClickListener(this);
- }
-
- @Override
- public void showCartSuccess(final List<CartBean.DataBean> data) {
- this.data=data;
- cartAdapter = new CartAdapter(MainActivity.this,data, this);
- expandable.setAdapter(cartAdapter);
- //展開全部的列表
- int childCount = cartAdapter.getGroupCount();
- for (int i = 0; i < childCount; i++) {
- expandable.expandGroup(i);
- }
- }
-
- @Override
- public void showCartfailure(Throwable t) {
- Log.d("異常showCart", t.toString());
- }
-
- @Override
- public void jisuan(String count, String priceAll) {
- cart_count.setText("共"+count+"件商品");
- cart_priceAll.setText("總計:"+priceAll);
- }
- //--------------------------------刪除購物車
- @Override
- public void deleteCartSuccess(String str) {
- Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void deleteCartfailure(Throwable t) {
- Log.d("異常deleteCart", t.toString());
- }
- //----------------------------------修改數量
- @Override
- public void updateCartsSuccess(String str) {
- Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void updateCartsfailure(Throwable t) {
- Log.d("異常updateCarts", t.toString());
- }
-
-
- //----------------------點擊事件
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.cart_checkBox:
- for (int i = 0; i < data.size(); i++) {
- data.get(i).setCheck(!data.get(i).isCheck());
- List<CartBean.DataBean.ListBean> list = data.get(i).getList();
- for (int j = 0; j < list.size(); j++) {
- list.get(j).setCheck(!list.get(j).isCheck());
- }
- }
- cartAdapter.jS();
- cartAdapter.notifyDataSetChanged();
- break;
- case R.id.cart_editor:
- if (flag){
- cart_editor.setText("完成");
- cart_delete.setVisibility(View.VISIBLE);
- cart_jiesuan.setVisibility(View.INVISIBLE);
- cart_priceAll.setVisibility(View.INVISIBLE);
- cart_count.setVisibility(View.INVISIBLE);
- flag=false;
- }else {
- cart_editor.setText("編輯");
- cart_delete.setVisibility(View.INVISIBLE);
- cart_jiesuan.setVisibility(View.VISIBLE);
- cart_priceAll.setVisibility(View.VISIBLE);
- cart_count.setVisibility(View.VISIBLE);
- flag=true;
- }
- break;
- case R.id.cart_delete:
- List<CartBean.DataBean> toBeDeleteGroups = new ArrayList<CartBean.DataBean>();
- //待刪除的組元素
- for (int i = 0; i < data.size(); i++) {
- CartBean.DataBean group = data.get(i);
- if (data.get(i).isCheck()) {
- toBeDeleteGroups.add(group);
- }
- List<CartBean.DataBean.ListBean> toBeDeleteChilds = new ArrayList<CartBean.DataBean.ListBean>();//待刪除的子元素
- List<CartBean.DataBean.ListBean> list = data.get(i).getList();
- for (int j = 0; j < list.size(); j++) {
- if (list.get(j).isCheck()) {
- toBeDeleteChilds.add(list.get(j));
- showCartPresenter.deleteCart("12400",list.get(j).getPid()+"");
-
- }
- }
- list.removeAll(toBeDeleteChilds);
- }
- data.removeAll(toBeDeleteGroups);
- cart_count.setText("共"+0+"件商品");
- cart_priceAll.setText("總計:"+0.0);
- //從新設置購物車
- cartAdapter.notifyDataSetChanged();
- break;
- }
- }
- }
-
- //---適配器
- public class CartAdapter extends BaseExpandableListAdapter {
- private final IShowCartView IShowCartView;
- private final Context context;
- private final List<CartBean.DataBean> list;
- private GroupViewHolder holder;
- private ChildViewHolder childholder;
-
-
- public CartAdapter(Context context, List<CartBean.DataBean> data, IShowCartView IShowCartView) {
- this.IShowCartView=IShowCartView;
- this.context=context;
- this.list=data;
- }
- @Override
- public int getGroupCount() {
- return list.size();
- }
-
- @Override
- public int getChildrenCount(int groupPosition) {
- return list.get(groupPosition).getList().size();
- }
- @Override
- public Object getGroup(int groupPosition) {
- return list.get(groupPosition);
- }
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- return list.get(groupPosition).getList().get(childPosition);
- }
- @Override
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- @Override
- public boolean hasStableIds() {
- return false;
- }
- @Override
- public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
- if(convertView==null){
- holder = new GroupViewHolder();
- convertView = View.inflate(context, R.layout.cart_groupitem, null);
- holder.group_box = convertView.findViewById(R.id.group_box);
- holder.group_name = convertView.findViewById(R.id.group_name);
- convertView.setTag(holder);
- }else{
- holder = (GroupViewHolder) convertView.getTag();
- }
- holder.group_box.setChecked(list.get(groupPosition).isCheck());
- holder.group_name.setText(list.get(groupPosition).getSellerName());
- holder.group_box.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- boolean isCleked = list.get(groupPosition).isCheck();
- isCleked=!isCleked;
- list.get(groupPosition).setCheck(isCleked);
- List<CartBean.DataBean.ListBean> beans = CartAdapter.this.list.get(groupPosition).getList();
- for (int i = 0; i < beans.size() ; i++) {
- beans.get(i).setCheck(isCleked);
- }
- jS();
- notifyDataSetChanged();
- }
- });
- return convertView;
- }
- @Override
- public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
- if (convertView==null){
- convertView = View.inflate(context, R.layout.cart_childitem, null);
- childholder = new ChildViewHolder();
- childholder.child_box = convertView.findViewById(R.id.child_box);
- childholder.child_img = convertView.findViewById(R.id.child_img);
- childholder.child_subhead = convertView.findViewById(R.id.child_subhead);
- childholder.child_price = convertView.findViewById(R.id.child_price);
- childholder.child_adm = convertView.findViewById(R.id.child_adm);
- convertView.setTag(childholder);
- }else{
- childholder = (ChildViewHolder) convertView.getTag();
- }
- childholder.child_box.setChecked(list.get(groupPosition).getList().get(childPosition).isCheck());
- String images = list.get(groupPosition).getList().get(childPosition).getImages();
- String image = images.split(".jpg")[0] + ".jpg";
- childholder.child_img.setImageURI(Uri.parse(image));
- childholder.child_subhead.setText(list.get(groupPosition).getList().get(childPosition).getSubhead());
- childholder.child_price.setText("售價爲:"+list.get(groupPosition).getList().get(childPosition).getPrice());
- childholder.child_adm.setNumber(list.get(groupPosition).getList().get(childPosition).getNum());
- childholder.child_box.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- boolean isCleked = list.get(groupPosition).getList().get(childPosition).isCheck();
- isCleked=!isCleked;
- list.get(groupPosition).getList().get(childPosition).setCheck(isCleked);
- List<CartBean.DataBean.ListBean> beans = CartAdapter.this.list.get(groupPosition).getList();
- boolean flag=true;
- for (int i = 0; i < beans.size() ; i++) {
- if (!beans.get(i).isCheck()) {
- flag=false;
- }
- }
- list.get(groupPosition).setCheck(flag);
- jS();
- notifyDataSetChanged();
- }
- });
- childholder.child_adm.setOnAddMinusClickListener(new AddMinusView.OnAddMinusClickListener() {
- @Override
- public void onAddClick(View v) {
- int num = list.get(groupPosition).getList().get(childPosition).getNum();
- num++;
- list.get(groupPosition).getList().get(childPosition).setNum(num);
- jS();
- notifyDataSetChanged();
- }
- @Override
- public void onMinusClick(View v) {
- int num = list.get(groupPosition).getList().get(childPosition).getNum();
- num--;
- if(num<=0) {
- Toast.makeText(context, "最小數量爲1", Toast.LENGTH_SHORT).show();
- return;
- }
- list.get(groupPosition).getList().get(childPosition).setNum(num);
- jS();
- notifyDataSetChanged();
- }
-
- @Override
- public void onEtNumberClick(View v) {
- final AlertDialog builder = new AlertDialog.Builder(context).create();
- builder.setTitle("舒適提示");
- View inflate = View.inflate(context, R.layout.cart_update, null);
- builder.setView(inflate);
- TextView add = inflate.findViewById(R.id.goods_add);
- TextView minus = inflate.findViewById(R.id.goods_minus);
- final EditText goods_num = inflate.findViewById(R.id.goods_num);
- Button cancel = inflate.findViewById(R.id.goods_cancel);
- Button confirm = inflate.findViewById(R.id.goods_confirm);
- goods_num.setText(list.get(groupPosition).getList().get(childPosition).getNum()+"");
- add.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int num = Integer.parseInt(goods_num.getText().toString());
- num++;
- goods_num.setText(num+"");
- }
- });
- minus.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int num = Integer.parseInt(goods_num.getText().toString());
- num--;
- if(num<=0) {
- Toast.makeText(context, "最小數量爲1", Toast.LENGTH_SHORT).show();
- return;
- }
- goods_num.setText(num+"");
- }
- });
- confirm.setOnClickListener(new View.OnClickListener() {
- private String uid;
- @Override
- public void onClick(View v) {
- CartBean.DataBean.ListBean bean = list.get(groupPosition).getList().get(childPosition);
- new ShowCartPresenter(IShowCartView).updateCarts("12400",bean.getSellerid()+"",bean.getPid()+"",bean.getSelected()+"",goods_num.getText().toString());
- list.get(groupPosition).getList().get(childPosition).setNum(Integer.parseInt(goods_num.getText().toString()));
- notifyDataSetChanged();
- builder.dismiss();
- }
- });
- builder.show();
- }
- });
-
- return convertView;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return false;
- }
- class GroupViewHolder{
- public CheckBox group_box;
- public TextView group_name;
- }
- class ChildViewHolder{
- public CheckBox child_box;
- public ImageView child_img;
- public TextView child_subhead;
- public TextView child_price;
- public AddMinusView child_adm;
-
- }
-
- /**
- * 計算數量和價格
- * 經過遍歷一級的獲得二級的CheckBox,若是是選擇的選中狀態的話,就統計它們的總計的數量和價格
- */
- public void jS(){
- double priceAll = 0;
- int cou=0;
- for (int i = 0; i <list.size() ; i++) {
- List<CartBean.DataBean.ListBean> beans = this.list.get(i).getList();
- for (int j = 0; j <beans.size() ; j++) {
- boolean check = beans.get(j).isCheck();
- if(check==true){
- double mul = JisuanUtils.mul(beans.get(j).getPrice(), beans.get(j).getNum());
- priceAll = JisuanUtils.add(priceAll, mul);
- cou+=beans.get(j).getNum();
- }
- }
- }
- Log.d("數據呢", cou+"");
- IShowCartView.jisuan(cou+"",priceAll+"");
- }
- }
- //values下
- //attrs.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="AddMinusViewStyle">
-
- <attr name="left_text" format="string"/>
-
- <attr name="right_text" format="string"/>
- <attr name="middle_text" format="integer"/>
-
- <attr name="left_text_color" format="color"/>
- </declare-styleable>
- </resources>
-
- //---color.XML
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <color name="colorPrimary">#3F51B5</color>
- <color name="colorPrimaryDark">#303F9F</color>
- <color name="colorAccent">#FF4081</color>
- <color name="Cornsilk">#FFF8DC</color>
- <color name="BlueViolet">#8A2BE2</color>
- <color name="White">#FFFFFF</color>
- <color name="Silver">#C0C0C0</color>
- <color name="Red">#FF0000</color>
- <color name="Black">#000000</color>
- </resources>
- //mapp全局初始化
- public class MApp extends Application {
- @Override
- public void onCreate() {
- super.onCreate();
- Fresco.initialize(this);
- }
- }
- //drawable下
- //edit.XML
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <solid android:color="#FFFFFF" />
- <stroke
- android:width="1dip"
- android:color="#BDC7D8" />
- </shape>
- //適配器子佈局
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- android:orientation="horizontal"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="100dp">
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:id="@+id/child_box"/>
- <com.facebook.drawee.view.SimpleDraweeView
- android:id="@+id/child_img"
- android:layout_width="100dp"
- android:layout_height="100dp"
- android:layout_centerVertical="true"
- android:layout_toRightOf="@+id/child_box"
- android:background="@color/Red"
- roundAsCircle="true"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/child_subhead"
- android:layout_toRightOf="@+id/child_img"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:text="標題"
- />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/child_price"
- android:layout_toRightOf="@+id/child_img"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="70dp"
- android:text="價格"
- />
-
- <com.example.hp.lianjunnan0406.view.AddMinusView
- android:id="@+id/child_adm"
- android:layout_width="110dp"
- android:layout_height="40dp"
- android:layout_alignParentRight="true"
- android:layout_marginRight="40dp"
- android:layout_marginTop="60dp"
- android:focusable="false" >
- </com.example.hp.lianjunnan0406.view.AddMinusView>
-
- </RelativeLayout>
- //適配器父佈局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:orientation="horizontal"
- xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/group_box"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/group_name"
- />
- </LinearLayout>
- //適配器彈框
- <?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="match_parent">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/goods_minus"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:gravity="center"
- android:background="@drawable/shopcart_minus_grey"/>
-
- <EditText
- android:id="@+id/goods_num"
- android:layout_marginTop="2dp"
- android:layout_width="100dp"
- android:layout_height="35dp"
- android:background="@drawable/edit"
- android:layout_weight="0.00"
- android:gravity="center"
- android:text="1"/>
- <TextView
- android:id="@+id/goods_add"
- android:layout_width="35dp"
- android:layout_height="35dp"
- android:gravity="center"
- android:background="@drawable/shopcart_add_red"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="horizontal">
- <Button
- android:id="@+id/goods_cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@color/White"
- android:textColor="@color/Red"
- android:text="取消" />
- <Button
- android:id="@+id/goods_confirm"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@color/White"
- android:textColor="@color/Red"
- android:text="肯定" />
- </LinearLayout>
- </LinearLayout>
- //model顯示數據
- public class ShowCartModel {
- public void showCart(String uid, final ShowCartCallback showCartCallback){
- OkHttpClient okHttpClient = new OkHttpClient.Builder()
- .addNetworkInterceptor(new HttpLoggingInterceptor()
- .setLevel(HttpLoggingInterceptor.Level.BODY))
- .build();
- Retrofit retrofit = new Retrofit.Builder()
- .client(okHttpClient)
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .baseUrl(HttpConfig._URL).build();
- ApiService apiService = retrofit.create(ApiService.class);
- apiService.rxjavaShowCart(uid).subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<CartBean>() {
- @Override
- public void onSubscribe(Disposable d) {
-
- }
-
- @Override
- public void onNext(CartBean value) {
- if (value.getCode().equals("0")) {
- showCartCallback.showCartSuccess(value);
- }
- }
-
- @Override
- public void onError(Throwable e) {
- showCartCallback.failure(e);
- }
-
- @Override
- public void onComplete() {
-
- }
- });
-
-
- }
- public interface ShowCartCallback {
- void showCartSuccess(CartBean bean);//網絡請求成功
-
- void failure(Throwable t);//網絡請求失敗
- }
- }
-
- //----------修改
- public class UpdateCartsModel {
- public void updateCarts(String uid,String sellerid,String pid,String selected,String num,final UpdateCartsCallback updateCartsCallback){
- Map<String, String> map = new HashMap<>();
- map.put("uid", uid);
- map.put("sellerid", sellerid);
- map.put("pid", pid);
- map.put("selected", selected);
- map.put("num", num);
- OkHttpClient okHttpClient = new OkHttpClient.Builder()
- .addNetworkInterceptor(new HttpLoggingInterceptor()
- .setLevel(HttpLoggingInterceptor.Level.BODY))
- .build();
- Retrofit retrofit = new Retrofit.Builder()
- .client(okHttpClient)
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .baseUrl(HttpConfig._URL).build();
- ApiService apiService = retrofit.create(ApiService.class);
- apiService.rxjavaUpdateCarts(map).subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<AddCartBean>() {
- @Override
- public void onSubscribe(Disposable d) {
歡迎關注本站公眾號,獲取更多信息