简单的购物车实现
程序员文章站
2024-03-20 13:29:52
...
购物车的接口
//购物车
public static final String GOU_API="http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=鞋&page=1&count=30";
购物车的bean包
public String message;
public String status;
public List<ResultBean> result;
public static class ResultBean {
public boolean checked=false;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public int commodityId;
public String commodityName;
public String masterPic;
public int price;
public int saleNum;
}
购物车的契约类
//p层
public abstract class LloadPresenter{
public abstract void Goupresenter(HashMap<String,String> map);
}
//m层
interface LloadModel{
void GouModel(HashMap<String,String> map,RequestCallBack callBack);
}
//v层
interface LloadView{
//购物车
void GouSuccess(String result);
void GouFailure(String msg);
}
OkHttpUtils工具类
package jiaoyibo.bawei.com.gouwuche.net;
import android.os.Handler;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkHttpUtils {
private static OkHttpUtils mInstance;
private final OkHttpClient okHttpClient;
private Handler handler = new Handler();
public static OkHttpUtils getInstance() {
if (mInstance==null){
synchronized (OkHttpUtils.class){
if (mInstance==null){
mInstance = new OkHttpUtils();
}
}
}
return mInstance;
}
private OkHttpUtils(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.writeTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.build();
}
public void doGet(String url, final OkHttpCallBack okHttpCallback){
Request build = new Request.Builder().url(url).get().build();
okHttpClient.newCall(build).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
okHttpCallback.Failure("网络异常");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.code()==200){
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
okHttpCallback.Success(string);
}
});
}
}
});
}
public void dopost(String url, HashMap<String,String> params, final OkHttpCallBack callBack){
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String,String> map:params.entrySet()) {
builder.add(map.getKey(),map.getValue());
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder().url(url).post(requestBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (callBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
callBack.Failure("网络请求失败");
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (callBack!=null){
if (response.code()==200){
final String result = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
callBack.Success(result);
}
});
}
}
}
});
}
}
OkHttpCallBack接口
void Success(String result);
void Failure(String msg);
RequestCallBack接口
void Success(String result);
void Failure(String msg);
购物车的Model层
package jiaoyibo.bawei.com.gouwuche.model;
import android.os.Handler;
import java.util.HashMap;
import jiaoyibo.bawei.com.gouwuche.api.UserApi;
import jiaoyibo.bawei.com.gouwuche.contract.LoadContract;
import jiaoyibo.bawei.com.gouwuche.net.OkHttpCallBack;
import jiaoyibo.bawei.com.gouwuche.net.OkHttpUtils;
import jiaoyibo.bawei.com.gouwuche.net.RequestCallBack;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/3/5<p>
* <p>更改时间:2019/3/5<p>
* <p>版本号:1<p>
*/
public class LoadModel implements LoadContract.LloadModel{
private Handler handler = new Handler();
@Override
public void GouModel(HashMap<String, String> map, final RequestCallBack callBack) {
OkHttpUtils.getInstance().doGet(UserApi.GOU_API, new OkHttpCallBack() {
@Override
public void Success(final String result) {
if (callBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
callBack.Success(result);
}
});
}
}
@Override
public void Failure(final String msg) {
if (callBack!=null){
handler.post(new Runnable() {
@Override
public void run() {
callBack.Failure(msg);
}
});
}
}
});
}
}
购物车的presenter层
package jiaoyibo.bawei.com.gouwuche.presenter;
import java.util.HashMap;
import jiaoyibo.bawei.com.gouwuche.contract.LoadContract;
import jiaoyibo.bawei.com.gouwuche.model.LoadModel;
import jiaoyibo.bawei.com.gouwuche.net.RequestCallBack;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/3/5<p>
* <p>更改时间:2019/3/5<p>
* <p>版本号:1<p>
*/
public class LoadPresenter extends LoadContract.LloadPresenter{
private LoadContract.LloadView lloadView;
private final LoadModel loadModl;
public LoadPresenter(LoadContract.LloadView lloadView) {
this.lloadView = lloadView;
loadModl = new LoadModel();
}
@Override
public void Goupresenter(HashMap<String, String> map) {
loadModl.GouModel(map, new RequestCallBack() {
@Override
public void Success(String result) {
if (lloadView!=null){
lloadView.GouSuccess(result);
}
}
@Override
public void Failure(String msg) {
if (lloadView!=null){
lloadView.GouFailure(msg);
}
}
});
}
}
购物车自定义View控件
package jiaoyibo.bawei.com.gouwuche.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import jiaoyibo.bawei.com.gouwuche.R;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/3/5<p>
* <p>更改时间:2019/3/5<p>
* <p>版本号:1<p>
*/
public class GouView extends LinearLayout {
private TextView jia,jian;
private EditText num;
private int num1=1;
public GouView(Context context) {
super(context);
}
public GouView(Context context, AttributeSet attrs) {
super(context, attrs);
View view=LayoutInflater.from(context).inflate(R.layout.linear4,this);
num=view.findViewById(R.id.et_num);
jian=view.findViewById(R.id.minus);
jia=view.findViewById(R.id.add);
num.setText("1");
jia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num1++;
num.setText(num1+"");
}
});
jian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num1--;
if (num1==0){
num1=1;
Toast.makeText(getContext(),"不能减了",Toast.LENGTH_SHORT).show();
return;
}else {
num.setText(num1+"");
}
}
});
}
public GouView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
购物车的适配器
package jiaoyibo.bawei.com.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 java.util.ArrayList;
import java.util.List;
import jiaoyibo.bawei.com.gouwuche.R;
import jiaoyibo.bawei.com.gouwuche.bean.GouBean;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/3/5<p>
* <p>更改时间:2019/3/5<p>
* <p>版本号:1<p>
*/
public class GouAdapter extends RecyclerView.Adapter<GouAdapter.ViewHolder> {
private List<GouBean.ResultBean> list;
private Context context;
public GouAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<GouBean.ResultBean> list) {
if (list!=null){
this.list = list;
}
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.linear3,viewGroup,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
GouBean.ResultBean resultBean = list.get(i);
viewHolder.name.setText(list.get(i).commodityName);
viewHolder.price.setText(list.get(i).price+""+"¥");
viewHolder.checkbox.setChecked(resultBean.isChecked());
String[] images = resultBean.masterPic.split("\\|");
Glide.with(context).load(images[0]).into(viewHolder.image);
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView name;
private TextView price;
private ImageView image;
private CheckBox checkbox;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
price=itemView.findViewById(R.id.price);
image=itemView.findViewById(R.id.image);
checkbox=itemView.findViewById(R.id.checkbox);
}
}
}
购物车页面操作
package jiaoyibo.bawei.com.gouwuche;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 com.google.gson.Gson;
import java.util.HashMap;
import jiaoyibo.bawei.com.gouwuche.adapter.GouAdapter;
import jiaoyibo.bawei.com.gouwuche.bean.GouBean;
import jiaoyibo.bawei.com.gouwuche.contract.LoadContract;
import jiaoyibo.bawei.com.gouwuche.presenter.LoadPresenter;
public class MainActivity extends AppCompatActivity implements LoadContract.LloadView {
private RecyclerView xrec3;
private LoadPresenter loadPresenter;
private GouAdapter gouAdapter;
private CheckBox checkbox1;
private TextView num;
private Button but;
private GouBean gouBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox1 =findViewById(R.id.checkbox1);
but = findViewById(R.id.but);
xrec3 = findViewById(R.id.xrec3);
loadPresenter = new LoadPresenter(this);
xrec3.setLayoutManager(new LinearLayoutManager(this));
loadPresenter.Goupresenter(new HashMap<String, String>());
checkbox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (GouBean.ResultBean resultBean:gouBean.result){
resultBean.setChecked(checkbox1.isChecked());
}
gouAdapter.notifyDataSetChanged();
getTouchprice();
}
private void getTouchprice() {
double touchprice=0;
for (GouBean.ResultBean resultBean:gouBean.result) {
if(resultBean.isChecked()){
touchprice+=resultBean.price;
}
}
checkbox1.setText("¥"+touchprice);
}
});
}
@Override
public void GouSuccess(String result) {
gouBean = new Gson().fromJson(result, GouBean.class);
gouAdapter = new GouAdapter(this);
xrec3.setAdapter(gouAdapter);
gouAdapter.setList(gouBean.result);
}
@Override
public void GouFailure(String msg) {
}
}
自定义View的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="-"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/et_num"
/>
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="+"/>
</LinearLayout>
</LinearLayout>
购物车适配器的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="二十多人头发一股合同工一顿饭水电费的"
android:maxLines="2"
android:textSize="20dp"
android:layout_marginLeft="5dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="@+id/price"
android:textSize="20dp"
android:text="99999"
android:textColor="#ff00"
android:layout_marginTop="10dp"
/>
<jiaoyibo.bawei.com.gouwuche.view.GouView
android:layout_width="wrap_content"
android:layout_height="wrap_content"></jiaoyibo.bawei.com.gouwuche.view.GouView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
购物车页面的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#00FFFF"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:id="@+id/xrec3"
></android.support.v7.widget.RecyclerView>
<LinearLayout
android:background="#FFFAFA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选/反选"
android:layout_gravity="center"
android:textSize="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="去结算"
android:background="#FF0000"
android:textColor="#FFFFFF"
android:gravity="center"
android:layout_marginLeft="320dp"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
上一篇: 购物车 (面向过程是实现)
下一篇: cookies