购物车+自定义view搜索+加入购物车

布局main

<?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=".activity.MainActivity">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="900dp"
      android:orientation="vertical">
     <com.example.dell.gouwuche.view.MyView
         android:id="@+id/views"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         />
     <android.support.v7.widget.RecyclerView
         android:id="@+id/xr"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="9"
         ></android.support.v7.widget.RecyclerView>
  </LinearLayout>
    <Button
        android:id="@+id/jie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="去结算"

        />
</LinearLayout>

页面布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/img"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/name"
        />
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/img"
        android:text="商品"
        app:layout_constraintTop_toTopOf="@id/img"
        app:layout_constraintBottom_toTopOf="@id/price"
        />
    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/img"
        app:layout_constraintTop_toBottomOf="@+id/name"
        android:text="¥:"
        app:layout_constraintBottom_toBottomOf="parent"
        android:textColor="#f00"
        />
    <Button
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/name"
        app:layout_constraintRight_toRightOf="parent"
        android:text="加入购物车"
        android:textColor="#f00"
        />
</android.support.constraint.ConstraintLayout>

加入购物的recycleview

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp">

  <android.support.v7.widget.RecyclerView
      android:id="@+id/cart_xr"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      ></android.support.v7.widget.RecyclerView>
</LinearLayout>

加入购物车的子条目

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp">
    <CheckBox
        android:id="@+id/ck_goods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/img_goods"
        />
    <ImageView
        android:id="@+id/img_goods"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toRightOf="@+id/ck_goods"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/title"
        />
    <TextView
        android:id="@+id/title"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/img_goods"
        android:layout_marginLeft="20dp"
        android:lines="2"
        android:ellipsize="end"
        app:layout_constraintBottom_toTopOf="@+id/price"
        />
    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="价格:"
        android:textSize="20dp"
        android:textColor="#f00"
        app:layout_constraintLeft_toRightOf="@+id/img_goods"
        app:layout_constraintTop_toBottomOf="@+id/title"
        android:layout_marginLeft="20dp"
        />
</android.support.constraint.ConstraintLayout>

View

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/ed_name"
        android:layout_width="500dp"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/suo"
        />
    <Button
        android:id="@+id/suo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索"
        app:layout_constraintLeft_toRightOf="@+id/ed_name"
        app:layout_constraintRight_toRightOf="parent"
        />
</android.support.constraint.ConstraintLayout>

MyView

package com.example.dell.gouwuche.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.dell.gouwuche.R;

public class MyView extends LinearLayout {

    public EditText name;
    public  Button btn;

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

    public MyView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.activity_myview, this, true);
        name = view.findViewById(R.id.ed_name);
        btn = view.findViewById(R.id.suo);
    }

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

api

package com.example.dell.gouwuche.api;

public class GoodsApi {
    public static final String Gou_Api="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=%E6%9D%BF%E9%9E%8B&page=1&count=10";
}

Bean实现实例化

package com.example.dell.gouwuche.bean;

import java.io.Serializable;
import java.util.List;

public class GoodsBean implements Serializable {

    private String message;
    private String status;
    private List<Data> result;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Data> getResult() {
        return result;
    }

    public void setResult(List<Data> result) {
        this.result = result;
    }

    public static class Data implements Serializable{
        /**
         * commodityId : 137
         * commodityName : 唐狮冬季男士冬休闲鞋高帮男鞋男士板鞋休闲男鞋子高帮男鞋百搭休闲板鞋男
         * masterPic : http://172.17.8.100/images/small/commodity/nx/nbx/3/1.jpg
         * price : 79
         * saleNum : 0
         */

        private int commodityId;
        private String commodityName;
        private String masterPic;
        private int price;
        private int saleNum;

        public int getCommodityId() {
            return commodityId;
        }

        public void setCommodityId(int commodityId) {
            this.commodityId = commodityId;
        }

        public String getCommodityName() {
            return commodityName;
        }

        public void setCommodityName(String commodityName) {
            this.commodityName = commodityName;
        }

        public String getMasterPic() {
            return masterPic;
        }

        public void setMasterPic(String masterPic) {
            this.masterPic = masterPic;
        }

        public int getPrice() {
            return price;
        }

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

        public int getSaleNum() {
            return saleNum;
        }

        public void setSaleNum(int saleNum) {
            this.saleNum = saleNum;
        }
    }
}

接口回调有成功和失败的方法
契约类

package com.example.dell.gouwuche.contract;

import com.example.dell.gouwuche.callback.OkHttpUtilsCallBack;
import java.util.HashMap;

public interface GoodsContract {

    //m层
    interface IModle{
        void modle(HashMap<String,String> map, final OkHttpUtilsCallBack okHttpUtilsCallBack);
    }
    //v层
    interface IView{
        void onSuccess(String result);
        void onFilure(String msg);
    }
    //p层
    public abstract class IPresenter{
        public abstract void presenter(HashMap<String, String> map);
    }
}

modle层
presenter层
utils类

package com.example.dell.gouwuche.utils;

import android.os.Handler;

import com.example.dell.gouwuche.callback.OkHttpUtilsCallBack;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtils {
    private Handler handler=new Handler();
    private static OkHttpUtils okHttpUtils;
    private final OkHttpClient okHttpClient;

    //创建私有构造方法
    private OkHttpUtils(){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
              .connectTimeout(5,TimeUnit.SECONDS)
              .writeTimeout(5,TimeUnit.SECONDS)
              .readTimeout(5,TimeUnit.SECONDS)
              .build();
    }
    //双重检验锁
    public static OkHttpUtils getInstance(){
        if (okHttpUtils==null){
            synchronized (OkHttpUtils.class){
                if (okHttpUtils==null){
                    okHttpUtils=new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }
    //doGet方法、
    public void doGet(String url, final OkHttpUtilsCallBack okHttpUtilsCallBack){
        Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                 handler.post(new Runnable() {
                     @Override
                     public void run() {
                        okHttpUtilsCallBack.onFilure("网络异常");
                     }
                 });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String result = response.body().string();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                      okHttpUtilsCallBack.onSuccess(result);
                    }
                });
            }
        });
    }
}

MainActivity

package com.example.dell.gouwuche.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.dell.gouwuche.adapter.MyAdapter;
import com.example.dell.gouwuche.R;
import com.example.dell.gouwuche.bean.GoodsBean;
import com.example.dell.gouwuche.contract.GoodsContract;
import com.example.dell.gouwuche.presenter.GoodsPresenter;
import com.example.dell.gouwuche.view.MyView;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity implements GoodsContract.IView , MyAdapter.OnItemClickListener{

    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    private GoodsPresenter goodsPresenter;
    private MyView view;
    private Button sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.xr);
        view = findViewById(R.id.views);
        sum = findViewById(R.id.jie);

        sum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this , CartActivity.class);
                intent.putExtra("name",(Serializable)sumlist);
                startActivity(intent);
            }
        });



        view.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = view.name.getText().toString();
                HashMap<String, String> map = new HashMap<>();
                map.put("keyword","鞋");
                goodsPresenter.presenter(map);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //添加适配器
        myAdapter = new MyAdapter(this);

        //创建p层
        goodsPresenter = new GoodsPresenter( this);
        goodsPresenter.presenter(new HashMap<String, String>());
    }


    List<GoodsBean.Data> sumlist = new ArrayList<>();
    @Override
    public void onClick(GoodsBean.Data data) {
        sumlist.add(data);
        Log.e("tag" ,"chang  du = " + sumlist.size());
    }


    @Override
    public void onSuccess(String result) {
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        myAdapter.setList(goodsBean.getResult());
        recyclerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onFilure(String msg) {

    }

}

CartActivity

package com.example.dell.gouwuche.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.dell.gouwuche.R;
import com.example.dell.gouwuche.adapter.CartAdapter;
import com.example.dell.gouwuche.bean.GoodsBean;

import java.util.List;

public class CartActivity extends AppCompatActivity {

    List<GoodsBean.Data> list = null;
    private CartAdapter cartAdapter;
    private RecyclerView cartXR;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        cartXR = findViewById(R.id.cart_xr);

        cartXR.setLayoutManager(new LinearLayoutManager(this));


        list = (List<GoodsBean.Data>) getIntent().getSerializableExtra("name");
        cartAdapter = new CartAdapter(this ,list);
        cartXR.setAdapter(cartAdapter);
        // Log.e("tag" ,"list.size() == " + list.size());

    }

}

CartAdapter

package com.example.dell.gouwuche.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.gouwuche.R;
import com.example.dell.gouwuche.bean.GoodsBean;

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

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartHolder> {
    private Context context;
    private List<GoodsBean.Data> data;

    public CartAdapter(Context context ,List<GoodsBean.Data> data ) {
        this.context = context;
        this.data=data;
    }

    @NonNull
    @Override
    public CartHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_product, viewGroup, false);
        CartHolder cartHolder = new CartHolder(view);
        return cartHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CartHolder cartHolder, int i) {
        GoodsBean.Data data = this.data.get(i);
        cartHolder.title.setText(data.getCommodityName());
        cartHolder.price.setText(data.getPrice() + "");
        Glide.with(context).load(data.getMasterPic()).into(cartHolder.img);
    }

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




    public class CartHolder extends RecyclerView.ViewHolder {

        private final CheckBox ck_quan;
        private final ImageView img;
        private TextView title;
        private final TextView price;

        public CartHolder(@NonNull View itemView) {
            super(itemView);
            ck_quan = itemView.findViewById(R.id.ck_goods);
            img = itemView.findViewById(R.id.img_goods);
            title = itemView.findViewById(R.id.title);
            price = itemView.findViewById(R.id.price);
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值