ReceiptDetailView.java 3.79 KB
package com.huaheng.mobilewms.refactor;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.huaheng.mobilewms.R;
import com.huaheng.mobilewms.bean.ReceiptDetail;

public class ReceiptDetailView extends LinearLayout {
    private Context context;

    TextView materialNameView;
    TextView materialCodeView;
    TextView materialQtyView;

    private ReceiptDetail receiptDetail;
    private String materialCode;
    private String materialName;
    private String materialQty;

    public ReceiptDetailView(Context context) {
        super(context, null);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.view_receipt_detail, this);
        materialNameView = findViewById(R.id.materialName);
        materialCodeView = findViewById(R.id.materialCode);
        materialQtyView = findViewById(R.id.materialQty);
    }

    public ReceiptDetailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void init(ReceiptDetail receiptDetail) {
        this.receiptDetail = receiptDetail;


        materialNameView.setText(receiptDetail.getMaterialName());
        materialCodeView.setText(receiptDetail.getMaterialCode());
        updateQtyLabel();
        setViewSelected(receiptDetail.isSelected());
        setGrayIfZero();
    }

    public void setGrayIfZero(){
        int gray = Color.rgb(125, 125, 125);
        if(receiptDetail.tobeCollectQty() == 0){
            materialNameView.setTextColor(gray);
            materialCodeView.setTextColor(gray);
            materialQtyView.setTextColor(gray);
        }
    }

    public void selectAll() {
        if(receiptDetail.tobeCollectQty() == 0){
            return;
        }
        if (receiptDetail.isSelected()) {
            receiptDetail.setQtySelected(0);
            setViewSelected(false);
        } else {
            receiptDetail.setQtySelected(receiptDetail.tobeCollectQty());
            setViewSelected(true);
        }
    }

    public void selectByQty(int qty){
        if (qty>0){
            receiptDetail.setQtySelected(qty);
            setViewSelected(true);
        }else{
            receiptDetail.setQtySelected(0);
            setViewSelected(false);
        }
    }

    public void setViewSelected(boolean selected) {
        updateQtyLabel();
        if (selected) {
            this.setBackgroundResource(R.color.tobe_color);
        } else {
            this.setBackgroundResource(R.color.white);
        }
    }

    /**根据收货数量显示不同格式的文字**/
    public void updateQtyLabel(){
        int qty = receiptDetail.getQtySelected();
        String unit = "";
        if (receiptDetail.getUnit() != null) {
            unit = receiptDetail.getUnit();
        }
        String labelStr = receiptDetail.tobeCollectQty() + unit;
        if(qty == receiptDetail.tobeCollectQty() || qty == 0) {
            materialQtyView.setText(labelStr);
        }else{
            labelStr = qty + " / " + labelStr;
            materialQtyView.setText(labelStr);
        }
    }

    public boolean isSelected() {
        return this.receiptDetail.isSelected();
    }

    public void viewLongClicked() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        ReceiptQtySelectorView view = new ReceiptQtySelectorView(context, receiptDetail);
        builder.setTitle("请选择收货数量   " + receiptDetail.getMaterialCode());
        builder.setView(view);
        AlertDialog dialog = builder.show();
        view.setParentDialog(dialog);
        view.setParentView(this);
    }
}