购物车

  1. 1.流式布局

  • package com.example.administrator.ykmn3;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.zhy.view.flowlayout.FlowLayout;
    import com.zhy.view.flowlayout.TagAdapter;
    import com.zhy.view.flowlayout.TagFlowLayout;
    
    import java.util.ArrayList;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    
    public class MainActivity extends AppCompatActivity {
    
        @BindView(R.id.edit_sousuo)
        EditText editSousuo;
        @BindView(R.id.btn_sousuo)
        Button btnSousuo;
        @BindView(R.id.taglayout)
        TagFlowLayout taglayout;
        private Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            context = MainActivity.this;
        }
    
        @OnClick(R.id.btn_sousuo)
        public void onViewClicked() {
            String s = editSousuo.getText().toString();
            ArrayList<String> list = new ArrayList<>();
            list.add(s);
            taglayout.setAdapter(new TagAdapter<String>(list) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tv, taglayout, false);
                     tv.setText(s);
                    return tv;
                }
            });
            taglayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    startActivity(new Intent(context,ShowcartActivity.class));
                    return false;
                }
            });
        }
    }
    
  • 2流式布局的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=".MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            >
            <EditText
                android:id="@+id/edit_sousuo"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:hint="请输入如值"
                />
            <Button
                android:id="@+id/btn_sousuo"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:text="搜索"
                />
        </LinearLayout>
        <com.zhy.view.flowlayout.TagFlowLayout
            android:id="@+id/taglayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"
            ></com.zhy.view.flowlayout.TagFlowLayout>
    </LinearLayout>
  • 3流逝布局的另一个xml
  • <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/tv"
        android:orientation="vertical"
        tools:context=".MainActivity"
        android:hint="默认值"
        >
    
  • 4自定义view 加减
  • public class Jiajian extends LinearLayout implements View.OnClickListener {
    
        private Button delete;
        private Button add;
        private TextView text6;
    
        public Jiajian(Context context) {
            super(context);
        }
    
        public Jiajian(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        private void init(Context context) {
            LayoutInflater.from(context).inflate(R.layout.jiajian,this);
            delete = findViewById(R.id.delete);
            add = findViewById(R.id.add);
            text6 = findViewById(R.id.text6);
            add.setOnClickListener(this);
            delete.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            String s = text6.getText().toString();
            int number = Integer.parseInt(s);
            switch (v.getId()){
                case R.id.delete:
                    if (number<1){
                        Toast.makeText(getContext(), "最少不能小于1", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number-=1;
                    text6.setText(String.valueOf(number));
                    onNumCallBack.onShowNum(number);
                    if (onNumCallBack!=null){
                        onNumCallBack.minus();
                    }
                    break;
                case R.id.add:
                    number+=1;
                    text6.setText(String.valueOf(number));
                    if (onNumCallBack!=null){
                        onNumCallBack.add1();
                    }
                    break;
            }
        }
        //接口回调
        public interface onNumCallBack{
            void onShowNum(int number);
    
    
    
            void minus();
    
            void add1();
        }
        onNumCallBack onNumCallBack;
        public void setOnNumCallBack(onNumCallBack onNumCallBack){
            this.onNumCallBack=onNumCallBack;
        }
    }
    
  • 5自定义view的布局
  • <?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"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@mipmap/delete"
            />
        <TextView
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="20dp"
            />
        <Button
            android:id="@+id/btn_add"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@mipmap/add"
            />
    </LinearLayout>
    
  • MVP
  • 6IC层
  • package com.example.administrator.ykmn3.ui;
    
    public interface IContal  {
        //v层
        public interface IView{
            void showdata(String message);
        }
    
        //p层
        public interface IPresent<IView>{
            void attache(IView iView);
            void dataache(IView iView);
            void  responseInfo();
        }
    
        //m层
        public interface IModel{
            public interface onCallBack{
              void   responseMes(String message);
            }
    
            void requestdata(onCallBack onCallBack);
        }
    }
    
  • 7IP层
  • package com.example.administrator.ykmn3.ui;
    
    import java.lang.ref.WeakReference;
    
    public class IPresenterlmp implements IContal.IPresent<IContal.IView> {
        IContal.IView iView;
        private IContal.IModel modelmp;
        private WeakReference<IContal.IView> viewWeakReference;
        private WeakReference<IContal.IModel> modelWeakReference;
    
        @Override
        public void attache(IContal.IView iView) {
            this.iView=iView;
            modelmp = new IModelmp();
            viewWeakReference = new WeakReference<>(iView);
            modelWeakReference = new WeakReference<>(modelmp);
        }
    
        @Override
        public void dataache(IContal.IView iView) {
                viewWeakReference.clear();
                modelWeakReference.clear();
        }
    
        @Override
        public void responseInfo() {
              modelmp.requestdata(new IContal.IModel.onCallBack() {
                  @Override
                  public void responseMes(String message) {
                      iView.showdata(message);
                  }
              });
        }
    }
    
  • 8IM层
  • package com.example.administrator.ykmn3.ui;
    
    import java.io.IOException;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.Response;
    
    public class IModelmp implements IContal.IModel {
        public static final String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
        @Override
        public void requestdata(final onCallBack onCallBack) {
            HttpUtils httpUtils = HttpUtils.getinstance();
            httpUtils.getdata(url, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    onCallBack.responseMes(e.getMessage());
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    onCallBack.responseMes(json);
                }
            });
        }
    }
    
  • 9OKhttp层
  • package com.example.administrator.ykmn3.ui;
    
    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.FormBody;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    
    public class HttpUtils {
        private static  HttpUtils httpUtils;
        private  OkHttpClient okHttpClient;
        public HttpUtils(){
            okHttpClient = new OkHttpClient();
        }
        public static HttpUtils getinstance(){
           if (httpUtils==null){
               synchronized (HttpUtils.class){
                   if (httpUtils==null){
                       httpUtils = new HttpUtils();
                   }
               }
           }
            return httpUtils;
        }
         //get方法
        public void getdata(String url, Callback callback){
            Request request = new Request.Builder().url(url).build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(callback);
        }
        //post方法
        public void postdata(String url, FormBody formBody,Callback callback){
            Request request = new Request.Builder().method("POST", formBody).url(url).build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(callback);
        }
    
    }
    
  • 最重要的一个页面
  • package com.example.administrator.ykmn3;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.example.administrator.ContactAdapter;
    import com.example.administrator.been.Messagebeen;
    import com.example.administrator.been.Userbeen;
    import com.example.administrator.ykmn3.ui.IContal;
    import com.example.administrator.ykmn3.ui.IPresenterlmp;
    import com.google.gson.Gson;
    
    import org.greenrobot.eventbus.EventBus;
    import org.greenrobot.eventbus.Subscribe;
    import org.greenrobot.eventbus.ThreadMode;
    
    import java.util.List;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    
    public class ShowcartActivity extends AppCompatActivity implements IContal.IView {
    
        @BindView(R.id.cx_all)
        CheckBox cxAll;
        @BindView(R.id.tv_zongjia)
        TextView tvZongjia;
        @BindView(R.id.btn_jiesuan)
        Button btnJiesuan;
        private IContal.IPresent presenterlmp;
        private Context context;
        private RecyclerView rv_view;
        private List<Userbeen.DataBean> dataBeans;
        private ContactAdapter contactAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_showcart);
            ButterKnife.bind(this);
            context = ShowcartActivity.this;
            presenterlmp = new IPresenterlmp();
            presenterlmp.attache(this);
            presenterlmp.responseInfo();
            rv_view = findViewById(R.id.rv_view);
    
    
        }
    
        @OnClick({R.id.cx_all, R.id.btn_jiesuan})
        public void onViewClicked(View view) {
            switch (view.getId()) {
    
                case R.id.cx_all:
                    if (cxAll.isChecked()){
                        for (int i = 0; i <dataBeans.size() ; i++) {
                            dataBeans.get(i).setChecked(true);
                           for (int j=0;j<dataBeans.get(i).getList().size();j++){
                               dataBeans.get(i).getList().get(j).setChecked(true);
                           }
    
                        }
                    }else{
                        for (int i = 0; i <dataBeans.size() ; i++) {
                             dataBeans.get(i).setChecked(false);
                             for (int j=0;j<dataBeans.get(i).getList().size();j++){
                                 dataBeans.get(i).getList().get(j).setChecked(false);
                             }
                        }
                    }
                    contactAdapter.notifyDataSetChanged();
                    break;
                case R.id.btn_jiesuan:
                    break;
            }
        }
    
        @Override
        public void showdata(final String message) {
            runOnUiThread(new Runnable() {
    
    
                @Override
                public void run() {
                    Gson gson = new Gson();
                    Userbeen userbeen = gson.fromJson(message, Userbeen.class);
                    dataBeans = userbeen.getData();
                    LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                    rv_view.setLayoutManager(manager);
                    contactAdapter = new ContactAdapter(context, dataBeans);
                    rv_view.setAdapter(contactAdapter);
                }
            });
    
    
    
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            presenterlmp.dataache(this);
    
        }
        @Override
        public void onStart() {
            super.onStart();
            EventBus.getDefault().register(this);
        }
    
        @Override
        public void onStop() {
            super.onStop();
            EventBus.getDefault().unregister(this);
        }
    
    
        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onMessageEvent(Messagebeen event) {
            /* Do something */
            Toast.makeText(context, event.getNum() + "..." + event.getPrice()
                    , Toast.LENGTH_SHORT).show();
        }
    }
    
    
  • 10第一个Adapter
  • package com.example.administrator;
    
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    
    import com.example.administrator.been.Userbeen;
    import com.example.administrator.ykmn3.R;
    import com.example.administrator.ykmn3.SubAdapter;
    
    import java.util.List;
    
    public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.Item> {
         Context context;
         List<Userbeen.DataBean> dataBeans;
    
        public ContactAdapter(Context context, List<Userbeen.DataBean> dataBeans) {
            this.context = context;
            this.dataBeans = dataBeans;
        }
    
        @NonNull
        @Override
        public Item onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
            Item holder = new Item(rootView);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull Item holder, final int position) {
           //设置外部商家的名称
            holder.cx_chose.setText(dataBeans.get(position).getSellerName());
            LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
            holder.rv_view2.setLayoutManager(manager);
            SubAdapter subAdapter = new SubAdapter(context, dataBeans.get(position).getList());
            holder.rv_view2.setAdapter(subAdapter);
    
            holder.cx_chose.setOnCheckedChangeListener(null);
            holder.cx_chose.setChecked(dataBeans.get(position).getChecked());
            holder.cx_chose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    dataBeans.get(position).setChecked(isChecked);
                    for (Userbeen.DataBean.ListBean listBean:dataBeans.get(position).getList()){
                        listBean.setChecked(isChecked);
                    }
                    notifyDataSetChanged();
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return dataBeans.size();
        }
    
        class Item extends RecyclerView.ViewHolder{
    
            private  CheckBox cx_chose;
            private  RecyclerView rv_view2;
    
            public Item(View itemView) {
                super(itemView);
                cx_chose = itemView.findViewById(R.id.cx_chose);
                rv_view2 = itemView.findViewById(R.id.rv_view2);
            }
        }
    }
    
  • 11第一个adapter的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="wrap_content"
        android:orientation="vertical"
        >
    
    
        <CheckBox
            android:id="@+id/cx_chose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商家一"
            />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_view2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    
            ></android.support.v7.widget.RecyclerView>
    
    </LinearLayout>
    
  • 12第二个Adapter
  • public class Gwcadapter extends RecyclerView.Adapter<Gwcadapter.SubViewHolder> {
        private int shownumber;
        private  Context context;
       private List<Gouwucbeen.DataBean.ListBean> data;
    
        public Gwcadapter(Context context, List<Gouwucbeen.DataBean.ListBean> data) {
            this.context = context;
            this.data = data;
        }
    
        @NonNull
        @Override
        public SubViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.gwc2, parent, false);
            SubViewHolder holder = new SubViewHolder(inflate);
            return holder;
        }
    
        @Override
        public void onBindViewHolder(@NonNull SubViewHolder holder, int position) {
            String images = data.get(position).getImages();
            if (images.contains("|")){
                images=images.substring(0,images.indexOf("|"));
            }else{
                images=data.get(position).getImages();
            }
            Picasso.with(context).load(images).into((holder).img6);
            holder.text_show1.setText(data.get(position).getTitle());
            holder.tv_show.setText("单价是"+data.get(position).getPrice());
           holder.jiajian.setOnNumCallBack(new Jiajian.onNumCallBack() {
               @Override
               public void onShowNum(int number) {
                   shownumber = number;
                   Toast.makeText(context, "number"+number, Toast.LENGTH_SHORT).show();
    
    
               }
    
               @Override
               public void minus() {
                   data.get(position).setNum(data.get(position).getNum()-1);
                   User user = new User();
                   EventBus.getDefault().post(user);
               }
    
               @Override
               public void add1() {
    
                   data.get(position).setNum(data.get(position).getNum()+1);
                   User user = new User();
                   EventBus.getDefault().post(user);
               }
           });
           holder.text6.setText(data.get(position).getNum()+"");
            holder.cx_box2.setChecked(data.get(position).getChecked());
            holder.cx_box2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        Gouwucbeen.DataBean.ListBean listBean = new Gouwucbeen.DataBean.ListBean();
                        listBean.setNum(shownumber);
                        listBean.setPrice(data.get(holder.getLayoutPosition()).getPrice());
                        EventBus.getDefault().post(listBean);
                    }
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return data.size();
        }
    
        class SubViewHolder extends RecyclerView.ViewHolder{
    
            private  ImageView img6;
            private  CheckBox cx_box2;
            private  TextView text_show1;
            private  TextView tv_show;
            private  Jiajian jiajian;
            private TextView text6;
    
            public SubViewHolder(View itemView) {
                super(itemView);
                img6 = itemView.findViewById(R.id.img6);
                cx_box2 = itemView.findViewById(R.id.cx_box2);
                text_show1 = itemView.findViewById(R.id.text_show1);
                tv_show = itemView.findViewById(R.id.tv_show);
                jiajian = itemView.findViewById(R.id.jiajian);
                text6 = jiajian.findViewById(R.id.text6);
            }
        }
    }
    
  • 13第二个adapter的布局
  • <?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="50dp"
        android:orientation="horizontal"
        >
        <CheckBox
            android:id="@+id/cb_box"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            />
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="52dp"
            android:layout_height="50dp"
            android:layout_marginLeft="-2dp"
            android:layout_toRightOf="@+id/cb_box"
            android:src="@mipmap/ic_launcher"
            />
    
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/img"
            android:hint="展示数据"
            />
        
        <com.example.administrator.ykmn3.Jiajian
            android:id="@+id/jiajian"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="133dp"
            android:gravity="center_vertical">
    
        </com.example.administrator.ykmn3.Jiajian>
    
    </RelativeLayout>
    

最后 写自定义view是一定要主要现在findviewBabyid前面加载与其对应的布局

然后been封装类要自己写check 写两个  databeen  和listbeen

写Eventbus时要写封装类 写一个num和price

记得到依赖

implementation 'com.hyman:flowlayout-lib:1.1.2'
implementation 'com.android.support:design:27.1.1'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'org.greenrobot:eventbus:3.1.1'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值