1. 程式人生 > >購物車左右聯動

購物車左右聯動

Main:

public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
    private RecyclerView recy_left;
    private RecyclerView recy_right;
    private FooterNumView price_num_all;
    private IPresenterImpl presenter;
    private List<Work.DataBean> mlefts = new ArrayList<>();
    private List<Work.DataBean.SpusBean> mright = new ArrayList<>();
    private String lefturl = "http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
    private MyLeftAdapter leftAdapter;
    private MyRightAdapter rightAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        presenter.requestData(lefturl);
        setListener();
    }

    private void setListener() {
        leftAdapter.setCountLeftListener(new MyLeftAdapter.setLeftCount() {
            @Override
            public void setCountNum(Work.DataBean dataBean) {
                rightAdapter.setListClear();
                rightAdapter.setListCon(dataBean.getSpus());
                mright.addAll(dataBean.getSpus());
                addNumPrice();
            }
        });
        rightAdapter.getNumBerPr(new MyRightAdapter.SetUpdateNum() {
            @Override
            public void setOnNum(int num, int position) {
                addNumPrice();
            }
        });
    }

    private void initView() {
        recy_left = (RecyclerView) findViewById(R.id.recy_left);
        recy_right = (RecyclerView) findViewById(R.id.recy_right);
        price_num_all = (FooterNumView) findViewById(R.id.price_num_all);

        recy_left.setLayoutManager(new LinearLayoutManager(this));
        recy_right.setLayoutManager(new LinearLayoutManager(this));

        presenter = new IPresenterImpl(MainActivity.this);

        leftAdapter = new MyLeftAdapter(this,mlefts);
        recy_left.setAdapter(leftAdapter);
        rightAdapter = new MyRightAdapter(this,mright);
        recy_right.setAdapter(rightAdapter);
    }

    @Override
    public void showRequestData(Object data) {
        mlefts = (List<Work.DataBean>) data;
        //mright = (List<Work.DataBean.SpusBean>) data;
        leftAdapter.setMdatas(mlefts);
        //rightAdapter.setMspus(mright);
    }
    public void addNumPrice(){
        float price = 0;
        int mnum = 0;
        for(int i=0;i<mright.size();i++){
            final Work.DataBean.SpusBean bean = mright.get(i);

            price = price + bean.getPraise_num() * Float.parseFloat(bean.getSkus().get(0).getPrice());

            mnum = mnum + bean.getPraise_num();

        }
        price_num_all.setNumber_all(mnum);
        Log.e("mnum",mnum+"");
        price_num_all.setPrice_all(price);
        Log.e("price",price+"");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public void onClick(View v) {

    }
}

LeftAdapter:

public class MyLeftAdapter extends RecyclerView.Adapter<MyLeftAdapter.LeftViewHolder> {
    private Context context;
    private List<Work.DataBean> mdatas;

    public MyLeftAdapter(Context context, List<Work.DataBean> mdatas) {
        this.context = context;
        this.mdatas = mdatas;
    }

    public void setMdatas(List<Work.DataBean> mdatas) {
        this.mdatas = mdatas;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LeftViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context,R.layout.left_item,null);
        LeftViewHolder holder = new LeftViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull LeftViewHolder leftViewHolder, int i) {
        final Work.DataBean data = (Work.DataBean) mdatas.get(i);
        leftViewHolder.titleLeft.setText(mdatas.get(i).getName());
        leftViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msetLeftCount.setCountNum(data);
            }
        });
        leftViewHolder.itemView.setTag(i);
    }

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

    public class LeftViewHolder extends RecyclerView.ViewHolder {
        private TextView titleLeft;
        public LeftViewHolder(@NonNull View itemView) {
            super(itemView);
            titleLeft = itemView.findViewById(R.id.left_tv_item);
        }
    }
    public interface setLeftCount{
        void setCountNum(Work.DataBean dataBean);
    }
    private setLeftCount msetLeftCount;

    public void setCountLeftListener(setLeftCount setLeftCount){
        this.msetLeftCount = setLeftCount;
    }
}

RightAdapter:

public class MyRightAdapter extends RecyclerView.Adapter<MyRightAdapter.RightViewHolder> {
    private Context context;
    private List<Work.DataBean.SpusBean> mspus;

    public MyRightAdapter(Context context, List<Work.DataBean.SpusBean> mspus) {
        this.context = context;
        this.mspus = mspus;
    }

    public void setMspus(List<Work.DataBean.SpusBean> mspus) {
        this.mspus = mspus;
        notifyDataSetChanged();
    }
    public void setListCon(List<Work.DataBean.SpusBean> spusBean){
        mspus.addAll(spusBean);
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public RightViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context,R.layout.right_item,null);
        RightViewHolder holder = new RightViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RightViewHolder rightViewHolder, final int i) {
        Glide.with(context).load(mspus.get(i).getPic_url()).into(rightViewHolder.img_right);
        rightViewHolder.title_right.setText(mspus.get(i).getName());
        rightViewHolder.mouth_right.setText("月銷:"+mspus.get(i).getMonth_saled());
        rightViewHolder.price_right.setText("¥:"+mspus.get(i).getSkus().get(0).getPrice());
        rightViewHolder.price_right_item.setCount(mspus.get(i).getPraise_num());
        rightViewHolder.price_right_item.getMsetRightNumber(new AddNumPrice.setRightNumber() {
            @Override
            public void setCountNum(int count) {
                mspus.get(i).setPraise_num(count);
                Log.e("count",count+"");
                msetUpdateNum.setOnNum(count,i);
            }
        });
        rightViewHolder.itemView.setTag(i);
    }
    public void setListClear(){
        mspus.clear();
    }


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

    public class RightViewHolder extends RecyclerView.ViewHolder {
        private ImageView img_right;
        private TextView title_right;
        private TextView mouth_right;
        private TextView price_right;
        private AddNumPrice price_right_item;
        public RightViewHolder(@NonNull View itemView) {
            super(itemView);
            img_right = itemView.findViewById(R.id.img_right);
            title_right = itemView.findViewById(R.id.title_right);
            mouth_right = itemView.findViewById(R.id.mouth_right);
            price_right = itemView.findViewById(R.id.price_right);
            price_right_item = itemView.findViewById(R.id.price_right_item);
        }
    }
    public interface SetUpdateNum{
        void setOnNum(int num,int position);
    }
    private SetUpdateNum msetUpdateNum;

    public void getNumBerPr(SetUpdateNum setUpdateNum){
        this.msetUpdateNum = setUpdateNum;
    }
}

加減自定義view:

public class AddNumPrice extends LinearLayout implements View.OnClickListener {

    private TextView tv_jian;
    private TextView num_tv;
    private TextView tv_add;
    private int mnum;

    public AddNumPrice(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.num_price_view, this);
        tv_jian = findViewById(R.id.tv_jian);
        num_tv = findViewById(R.id.num_tv);
        tv_add = findViewById(R.id.tv_add);
        tv_jian.setOnClickListener(this);
        tv_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_add:
                mnum++;
                setCount(mnum);

                if(msetRightNumber!=null){
                    msetRightNumber.setCountNum(mnum);
                }
                break;
            case R.id.tv_jian:
                if(mnum>0){
                    mnum--;
                    setCount(mnum);

                    if(msetRightNumber!=null){
                        msetRightNumber.setCountNum(mnum);
                    }
                }else{
                    Toast.makeText(getContext(), "不能低於零", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    public void setCount(int num){
        this.mnum = num;
        num_tv.setText(mnum+"");
    }

    private setRightNumber msetRightNumber;

    public interface setRightNumber{
        void setCountNum(int count);
    }

    public void getMsetRightNumber(setRightNumber setRightNumber) {
        this.msetRightNumber = setRightNumber;
    }
}

總數自定義View:

public class FooterNumView extends LinearLayout {
    private TextView price_all;
    private TextView number_all;

    public FooterNumView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.number_price_footer, this);
        price_all = findViewById(R.id.price_all);
        number_all = findViewById(R.id.number_all);


    }

    public TextView getNumber_all() {
        return number_all;
    }

    public TextView getPrice_all() {
        return price_all;
    }

    public void setPrice_all(float price){
        price_all.setText("總價: "+price);
    }

    public void setNumber_all(int num) {
        number_all.setText("總數: "+num);
    }
}

導的依賴:
在這裡插入圖片描述