Android使用RecyclerView实现简易的外卖购物车

一个小创意实现购物车功能,希望能帮到大家。
先来看看效果图吧
Android使用RecyclerView实现简易的外卖购物车_第1张图片
要是觉得有帮助的话就继续看吧,参考了
[2016yiyu]大神的文章(https://www.jianshu.com/p/bf3854e60830)Android使用RecyclerView实现简易的外卖购物车_第2张图片
上图是整个目录结构
这篇文章主要是实现价格相加,先来说下思路
第一步先为RecyclerView设置适配器,然后再adapter中的onBindViewHolder中为加和减布局设置监听事件以及加和减的背景变化设置
第二步在Adapter中为activity设置回调接口用来监听数字变化计算总价格
第三步实现加和减的方法onAddition和onSubtraction
第四步在Activity中设置监听,并改变底部的总价格
下面就直接上代码了
先上适配器jieshaoAdapter的代码


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class jieshaoAdapter extends RecyclerView.Adapter {
    private List mJieshaoList;
    NumberCallback numberCallback;
    private Context mContext;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView caiming;
        TextView jiage;
        EditText shuliang_tv;
        Button jia;
        Button jian;
        public ViewHolder(View itemView){
            super(itemView);
            caiming=(TextView) itemView.findViewById(R.id.caiming_tv);
            jiage=(TextView) itemView.findViewById(R.id.jiage_tv);
            shuliang_tv=(EditText) itemView.findViewById(R.id.shuliang_et);
            jia=(Button) itemView.findViewById(R.id.jia_btn);
            jian=(Button) itemView.findViewById(R.id.jian_btn);
        }
    }
    jieshaoAdapter(List list, Context mContext) {
        this.mContext = mContext;
        mJieshaoList=list;
    }
    @Override
    public jieshaoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.caiming_item,parent
        ,false);
        final ViewHolder holder=new ViewHolder(view);
        //点击+按钮的事件
        holder.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            
                onAddition(holder);
            }
        });
         //点击-按钮的事件
        holder.jian.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                onSubtraction(holder);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(jieshaoAdapter.ViewHolder holder, int position) {
        jieshao mjieshao=mJieshaoList.get(position);
        holder.caiming.setText(mjieshao.getMingzi());
        holder.jiage.setText(mjieshao.getJiage());
        holder.shuliang_tv.setTag(position);
    }
    public interface NumberCallback {
        // 给Activity提供数字变化后的回调接口
        //@param  number 数量
        // @param  price 单价
        //点击加时候监听
        void numberaddLoad(int number, int price);
        //点击减时候监听
        void numbersubLoad(int number, int price);
    }
   public void setNumberCallback(NumberCallback callback){
        numberCallback=callback;
   }

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

    /** * String 转换int */
    public int toInt(String tostring) {
        return Integer.parseInt(tostring);
    }
    /** * 添加数量 */
    private synchronized void onAddition(jieshaoAdapter.ViewHolder holder) {
//        onClickbg(holder.subtraction_ll);
//得到当前购物车数量,然后加1
        int i = toInt(holder.shuliang_tv.getText().toString());
        holder.shuliang_tv.setText("" + (i + 1));

        if (numberCallback != null){
            numberCallback.numberaddLoad(toInt(holder.shuliang_tv.getText().toString()) - i, toInt(holder.jiage.getText().toString()));
        }
    }
    /** * 减数量 */
    private synchronized void onSubtraction(jieshaoAdapter.ViewHolder holder) {
        //判断当前的数量,如果是不大于0则不做任何处理
        if (toInt(holder.shuliang_tv.getText().toString()) > 0) {
            int i = toInt(holder.shuliang_tv.getText().toString());
            holder.shuliang_tv.setText("" + (i - 1));
            if (numberCallback != null) {
                numberCallback.numbersubLoad(i - toInt(holder.shuliang_tv.getText().toString()), toInt(holder.jiage.getText().toString()));
            }
        }
    }

}

再来MainActivity的代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    List jieshaoList = new ArrayList<>();
    TextView zongjine;
    int num = 0;
    jieshaoAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initcai();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler);
        zongjine = (TextView) findViewById(R.id.tv3);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new jieshaoAdapter(jieshaoList, MainActivity.this);
        recyclerView.setAdapter(adapter);
        adapter.setNumberCallback(numberCallback);

    }

    private void initcai() {
        jieshao jieshao1 = new jieshao("肉末茄子", "7");
        jieshaoList.add(jieshao1);
        jieshao jieshao2 = new jieshao("梅菜扣肉", "10");
        jieshaoList.add(jieshao2);
        jieshao jieshao3 = new jieshao("狮子头", "20");
        jieshaoList.add(jieshao3);
    }

    jieshaoAdapter.NumberCallback numberCallback = new jieshaoAdapter.NumberCallback() {
        @Override
        public void numberaddLoad(int number, int price) {
            num = num + (price * number);
            zongjine.setText(Html.fromHtml("合计:" + getBlueText("" + num)));
        }
        @Override
        public void numbersubLoad(int number, int price) {
            num = num - (price * number);
            zongjine.setText(Html.fromHtml("合计:" + getBlueText("" + num)));
        }
    };
    /**
     * 为字符串加红色 * * @param string * @return
     */
    private String getBlueText(String string) {
        return String.format("%s", string); // string 会替换 %s
    }

}


接下来就是jieshao实体类的代码了


public class jieshao {
    private String mingzi;
    private String jiage;

    public jieshao(String mingzi, String jiage) {
        this.mingzi = mingzi;
        this.jiage = jiage;
    }

    public String getMingzi() {
        return mingzi;
    }

    public String getJiage() {
        return jiage;
    }
}

布局代码相信不用我放出来了吧,都在GIF中
算了,还是把caiming_item 的贴出来吧,省的大家麻烦



    
    

嗯,activity_main.xml的就自己写了吧。就俩控件。
Android使用RecyclerView实现简易的外卖购物车_第3张图片
想要偷懒就来这下载吧,我把源码上传到CSDN了添加链接描述

你可能感兴趣的:(项目分享)