TaskShipItemView.java 3.07 KB
package com.huaheng.mobilewms.view;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;

import com.huaheng.mobilewms.R;
import com.huaheng.mobilewms.bean.TaskDetail;
import com.huaheng.mobilewms.util.WMSUtils;

public class TaskShipItemView extends LinearLayout {

    private int index = 0;
    private boolean itemSelected = false;
    private int colorNormal = Color.rgb(255,255,255);
    private int colorDone = Color.rgb(245, 245, 245);
    private int colorSelected = Color.rgb(163-10,214-10,231-10);
    private TaskDetail taskDetal;

    public TaskShipItemView(Context context, int index, TaskDetail taskDetal){
        super(context);
        this.setOrientation(VERTICAL);
        this.index = index;
        this.taskDetal = taskDetal;

        this.setBackgroundColor(colorNormal);
        LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(lp);

        this.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                selectSwitch();
            }
        });
        if(isDone()){
            this.setBackgroundColor(colorDone);
        }
        initView();
    }

    public void initView(){
        newLine(getContext().getString(R.string.commodity_name), String.valueOf(taskDetal.getMaterialName()));
        String qty = WMSUtils.cleanFloatZero(String.valueOf(taskDetal.getQty()));
        String taskQty = WMSUtils.cleanFloatZero(String.valueOf(taskDetal.getTaskQty()));
        newLine(getContext().getString(R.string.commodity_number), taskQty + " / " + qty);
        addView(WMSUtils.newDevider(getContext()));
    }

    public void newLine(String label, String info){
        if(WMSUtils.isEmpty(info)){
            return;
        }
        TaskShipItem item = new TaskShipItem(this.getContext(), label, info);
        if(this.isDone()){
            //设置已经完成的样式:灰色字体
            item.setDone();
        }
        item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        this.addView(item);
    }

    @Override
    public void addView(View view){
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        super.addView(view);
    }

    public void selectSwitch(){
        select(!this.itemSelected);
    }

    public void select(boolean isSelect){
        if(this.isDone()){
            return;
        }
        this.itemSelected = isSelect;
        if(itemSelected){
            this.setBackgroundColor(this.colorSelected);
        }else{
            this.setBackgroundColor(this.colorNormal);
        }
    }

    public boolean isItemSelected(){
        return this.itemSelected;
    }

    /**
     * qty <= taskQty 表示Done
     * @return
     */
    public boolean isDone(){
        return taskDetal.getQty().compareTo(taskDetal.getTaskQty()) <= 0;
    }

    public TaskDetail getTaskDetal() {
        return taskDetal;
    }
}