Android MVP点击商铺切换商品购物车

权限

<uses-permission android:name="android.permission.INTERNET"/>

接口Meau

package com.example.gouwu3.bean;

import java.util.List;

public class Meau {

    private int code;
    private String msg;
    private List<DataBean> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public class DataBean {

        private String sellerName;
        private String sellerid;
        private List<ListBean> list;

        public String getSellerName() {
            return sellerName;
        }

        public void setSellerName(String sellerName) {
            this.sellerName = sellerName;
        }

        public String getSellerid() {
            return sellerid;
        }

        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public class ListBean {

            private double bargainPrice;
            private String createtime;
            private String detailUrl;
            private String images;
            private int num;
            private int pid;
            private double price;
            private int pscid;
            private int selected;
            private int sellerid;
            private String subhead;
            private String title;

            public double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double bargainPrice) {
                this.bargainPrice = bargainPrice;
            }

            public String getCreatetime() {
                return createtime;
            }

            public void setCreatetime(String createtime) {
                this.createtime = createtime;
            }

            public String getDetailUrl() {
                return detailUrl;
            }

            public void setDetailUrl(String detailUrl) {
                this.detailUrl = detailUrl;
            }

            public String getImages() {
                return images;
            }

            public void setImages(String images) {
                this.images = images;
            }

            public int getNum() {
                return num;
            }

            public void setNum(int num) {
                this.num = num;
            }

            public int getPid() {
                return pid;
            }

            public void setPid(int pid) {
                this.pid = pid;
            }

            public double getPrice() {
                return price;
            }

            public void setPrice(double price) {
                this.price = price;
            }

            public int getPscid() {
                return pscid;
            }

            public void setPscid(int pscid) {
                this.pscid = pscid;
            }

            public int getSelected() {
                return selected;
            }

            public void setSelected(int selected) {
                this.selected = selected;
            }

            public int getSellerid() {
                return sellerid;
            }

            public void setSellerid(int sellerid) {
                this.sellerid = sellerid;
            }

            public String getSubhead() {
                return subhead;
            }

            public void setSubhead(String subhead) {
                this.subhead = subhead;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }
        }
    }

}

Mainactivity

package com.example.gouwu3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gouwu3.adapter.ListAdapterEr;
import com.example.gouwu3.adapter.ListAdapterYi;
import com.example.gouwu3.bean.Meau;
import com.example.gouwu3.core.DataCall;
import com.example.gouwu3.presenter.RequestPresenter;

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

public class MainActivity extends AppCompatActivity implements DataCall {

    String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
    private ListView listView01;
    private ListView listView02;
    private TextView zongjia;
    private RequestPresenter requestPresenter;
    //商家
    private List<Meau.DataBean> list = new ArrayList<>();
    //数据
    private List<Meau.DataBean.ListBean> list1 = new ArrayList<>();
    private List<Meau.DataBean.ListBean> beans;
    private ListAdapterYi listAdapterYi;
    private ListAdapterEr listAdapterEr;
    private List<Meau.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        listView01 = findViewById(R.id.listview01);
        listView02 = findViewById(R.id.listview02);
        zongjia = findViewById(R.id.zongjia);
        requestPresenter = new RequestPresenter(this);
        requestPresenter.getRequest(url);
        listAdapterYi = new ListAdapterYi(this,list);
        listAdapterEr = new ListAdapterEr(this,list1);
        //点击条目商铺切换商品
        listView01.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<Meau.DataBean.ListBean> beans1 = data.get(position).getList();
                //清空当前
                list1.clear();
                //刷新
                listAdapterEr.notifyDataSetChanged();
                //添加
                list1.addAll(beans1);
                //刷新
                listAdapterYi.notifyDataSetChanged();
                listAdapterEr.notifyDataSetChanged();
            }
        });

        //调用总价
        listAdapterEr.setOnNumListener(new ListAdapterEr.OnNumListener() {
            @Override
            public void onNum() {
                calculatePrice(data);
            }
        });

        //设置adapter
        listView01.setAdapter(listAdapterYi);
        listView02.setAdapter(listAdapterEr);
    }

    //成功
    @Override
    public void requestSuccess(Meau meau) {
        data = meau.getData();
        Log.i("abc", data.toString());
        calculatePrice(data);
        list.addAll(data);
        beans = data.get(1).getList();
        list1.addAll(beans);
        listAdapterYi.notifyDataSetChanged();
        listAdapterEr.notifyDataSetChanged();
        Toast.makeText(this, "成功" + meau.getData(), Toast.LENGTH_LONG).show();
    }
    //失败
    @Override
    public void requestFail(Meau meau) {
        Toast.makeText(this, "失败", Toast.LENGTH_LONG).show();
    }

    //总价格
    private void calculatePrice(List<Meau.DataBean> shopList){
        double totalPrice=0;
        int totalNum = 0;
        for (int i = 0; i < shopList.size(); i++) {//循环的商家
            Meau.DataBean shop = shopList.get(i);
            for (int j = 0; j < shop.getList().size(); j++) {
                Meau.DataBean.ListBean goods = shop.getList().get(j);
                //计算价格
                totalPrice = totalPrice + goods.getNum() * goods.getPrice();
                totalNum+=goods.getNum();//计数
            }
        }
        zongjia.setText("价格:"+totalPrice+"个数:"+totalNum);

    }

    //内存溢出
    @Override
    protected void onDestroy() {
        super.onDestroy();
        new RequestPresenter(this).unBindCall();
    }
}

activity.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="9"
        android:orientation="horizontal">
        <ListView
            android:id="@+id/listview01"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="#ccc"
            />
        <ListView
            android:id="@+id/listview02"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <TextView
        android:id="@+id/zongjia"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="总价:0¥"
        android:gravity="center"
        android:textColor="#7FFF00"/>
</LinearLayout>

RequestPresenter

package com.example.gouwu3.presenter;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.example.gouwu3.bean.Meau;
import com.example.gouwu3.core.DataCall;
import com.example.gouwu3.model.RequestModel;

public class RequestPresenter {
    private DataCall dataCall;
    public RequestPresenter(DataCall dataCall) {
        this.dataCall = dataCall;
    }
    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            Meau meau = (Meau) msg.obj;
            if (meau.getCode() == 0){
                dataCall.requestSuccess(meau);
            }else {
                dataCall.requestFail(meau);
            }
        }
    };

    //子线程
    public void getRequest(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Meau data = RequestModel.getData(url);
                Message message = handler.obtainMessage();
                Log.i("aaa1",data.toString());
                message.obj = data;
                handler.sendMessage(message);
            }
        }).start();
    }
    //内存溢出
    public void unBindCall(){
        this.dataCall = null;
    }
}

RequestModel

package com.example.gouwu3.model;


import com.example.gouwu3.bean.Meau;
import com.example.gouwu3.utils.Utils;
import com.google.gson.Gson;

public class RequestModel {
    public static Meau getData(String url){
      String s = Utils.get(url);
        Gson gson = new Gson();
        Meau meau = gson.fromJson(s, Meau.class);
        return meau;
    }
}

接口DataCall

package com.example.gouwu3.model;
import com.example.gouwu3.bean.Meau;
import com.example.gouwu3.utils.Utils;
import com.google.gson.Gson;
public class RequestModel {
    public static Meau getData(String url){
      String s = Utils.get(url);
        Gson gson = new Gson();
        Meau meau = gson.fromJson(s, Meau.class);
        return meau;
    }
}

OKHTTP

package com.example.gouwu3.utils;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Utils {
    public static String get(String url){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request build = new Request.Builder().url(url).get().build();
        try {
            Response execute = okHttpClient.newCall(build).execute();
            return execute.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

ListAdapterYi

package com.example.gouwu3.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.gouwu3.R;
import com.example.gouwu3.bean.Meau;

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

public class ListAdapterYi extends BaseAdapter {
    private List<Meau.DataBean> list = new ArrayList<>();
    private Context context;

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

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh=null;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.listview_yi_layout,null);
            vh = new ViewHolder();
            vh.text_name=convertView.findViewById(R.id.text_name);

            convertView.setTag(vh);
        }else {
             vh = (ViewHolder)convertView.getTag();
        }
        vh.text_name.setText(list.get(position).getSellerName());
        return convertView;
    }
    class ViewHolder{
        TextView text_name;
    }
}

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="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="商铺名称"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="18sp"/>

</LinearLayout>

ListAdapterEr

package com.example.gouwu3.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.gouwu3.MainActivity;
import com.example.gouwu3.R;
import com.example.gouwu3.bean.Meau;
import com.example.gouwu3.view.MyView;

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

public class ListAdapterEr extends BaseAdapter {
    private Context context;
    private List<Meau.DataBean.ListBean> list = new ArrayList<>();
    public ListAdapterEr(Context context, List<Meau.DataBean.ListBean> list) {
        this.context=context;
        this.list=list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder2 vh2=null;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.listview_er_layout,null);
            vh2=new ViewHolder2();
            vh2.textView2=convertView.findViewById(R.id.textview2);
            vh2.price=convertView.findViewById(R.id.price);
            vh2.myView=convertView.findViewById(R.id.myView);
            vh2.image=convertView.findViewById(R.id.image);

            convertView.setTag(vh2);
        }else {
             vh2 = (ViewHolder2)convertView.getTag();
        }

        vh2.textView2.setText(list.get(position).getTitle());
        vh2.price.setText(list.get(position).getPrice()+"");
        String images = list.get(position).getImages();
        String[] split = images.split("\\|");
        if (split.length>0){
            Glide.with(context).load(split[0]).into(vh2.image);
        }
        vh2.myView.setCount(list.get(position).getNum());
        vh2.myView.setMyViewListener(new MyView.MyViewListener() {
            @Override
            public void myView(int count) {
                list.get(position).setNum(count);
                onNumListener.onNum();
            }
        });
        return convertView;
    }

    class ViewHolder2{
        TextView textView2,price;
        ImageView image;
        MyView myView;
    }


    private OnNumListener onNumListener;

    public void setOnNumListener(OnNumListener onNumListener) {
        this.onNumListener = onNumListener;
    }
    public interface OnNumListener{
        void onNum();
    }

}

adapter二Xml

<?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="match_parent"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="188dp"
        android:layout_height="188dp"
        android:src="@mipmap/ic_launcher" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="商铺名称"
        android:layout_marginLeft="189dp"
        android:textColor="#0f0"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格"
        android:layout_marginTop="100dp"
        android:textColor="#f00"
        android:textSize="18sp"
        android:layout_marginLeft="189dp"/>
    <com.example.gouwu3.view.MyView
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="160dp"
        android:layout_marginLeft="258dp"/>


</RelativeLayout>

在adapterEr中引入MyView

package com.example.gouwu3.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.gouwu3.R;
public class MyView extends LinearLayout implements View.OnClickListener {

    private TextView mAddBtn,mSubBtn;
    private TextView mNumText;
    private MyViewListener myViewListener;

    public MyView(Context context) {
        super(context);
        initView();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    private void initView(){
        //加载layout布局,第三个参数ViewGroup一定写成this
        View view = View.inflate(getContext(),R.layout.car_add_sub_layout,this);
        mAddBtn = view.findViewById(R.id.btn_add);
        mSubBtn = view.findViewById(R.id.btn_sub);
        mNumText = view.findViewById(R.id.text_number);
        mAddBtn.setOnClickListener(this);
        mSubBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int number = Integer.parseInt(mNumText.getText().toString());

        switch (v.getId()){
            case R.id.btn_add:
                number++;
                mNumText.setText(number+"");
                break;
            case R.id.btn_sub:
                if (number==0){
                    Toast.makeText(getContext(),"数量不能小于0",Toast.LENGTH_LONG).show();
                    return;
                }
                number--;
                mNumText.setText(number+"");
                break;
        }
        if (myViewListener != null){
            myViewListener.myView(number);
        }
    }
    public void setCount(int count){
        mNumText.setText(count+"");
    }
    public void setMyViewListener(MyViewListener myViewListener) {
        this.myViewListener = myViewListener;
    }
    public interface MyViewListener{
        void myView(int count);
    }
}

自定义viewXml

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/btn_add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:focusable="false"
        android:textSize="20sp"
        android:gravity="center"
        android:text="+" />

    <TextView
        android:id="@+id/text_number"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:textSize="14sp"
        android:text="0" />

    <TextView
        android:id="@+id/btn_sub"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:textSize="20sp"
        android:focusable="false"
        android:gravity="center"
        android:text="-" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值