Java tutorial
package com.gqp.duoduo.view; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.media.Image; import android.net.Uri; import android.support.v4.util.ArrayMap; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import com.gqp.common.adapter.ViewHolderBind; import com.gqp.common.cache.SpCache; import com.gqp.common.rx.bus.RxBus; import com.gqp.common.view.ViewContainer; import com.gqp.duoduo.App; import com.gqp.duoduo.R; import com.gqp.duoduo.base.Constants; import com.gqp.duoduo.custom.view.NumView; import com.gqp.duoduo.database.DeliverModel; import com.gqp.duoduo.model.CommonItem; import com.gqp.duoduo.model.Settlement; import com.gqp.duoduo.printer.BlueToothPrinter; import com.gqp.duoduo.ui.activity.SettlementGoodsListActivity; import com.gqp.duoduo.utils.MapUtils; import com.gqp.duoduo.utils.StringUtil; import com.gqp.duoduo.viewmodel.DeliverViewModel; import com.gqp.duoduo.viewmodel.SettlementViewModel; import com.jakewharton.rxbinding.view.RxView; import com.jakewharton.rxbinding.widget.RxTextView; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import butterknife.Bind; import butterknife.ButterKnife; import rx.functions.Action1; import static com.gqp.common.util.ViewUtils.getView; import static com.gqp.duoduo.utils.StringUtil.priceFormat; /** * Created by guoqipan on 2015/8/19 22:35. * qq:390893576 */ public class SettlementView extends LinearLayout implements ViewContainer<SettlementViewModel> { private SettlementViewModel viewModel; private TextView commit; private TextView moneyText; @Bind(R.id.settlement_view_container) LinearLayout container; @Bind(R.id.settlement_view_nodata_layout) RelativeLayout noDataLayout; @Bind(R.id.settlement_view_data_layout) LinearLayout dataLayout; @Bind(R.id.settle_back) ImageView settleBack; @Bind(R.id.settlement_view_print) CheckBox isPrint; @Bind(R.id.print_status) ImageView printStatus; @Bind(R.id.settlement_search) ImageView settleSearch; private boolean isEdit; private List<Settlement.MarketSettleEntity> mList; private Settlement.SettleMessageEntity settleMessageEntity; // private Action1<ArrayMap<String,Object>> action; public SettlementView(Context context, AttributeSet attrs) { super(context, attrs); } public SettlementView(Context context) { super(context); } @Override protected void onFinishInflate() { super.onFinishInflate(); commit = getView(this, R.id.settlement_view_commit); moneyText = getView(this, R.id.settlement_view_money); ButterKnife.bind(this); } @Override public void setViewModel(SettlementViewModel viewModel) { if (viewModel != null) { this.viewModel = viewModel; bindData(); bindListener(); // this.viewModel.bindAction(action); this.viewModel.bindMoneyText(RxTextView.text(moneyText)); } } private StringBuffer sb = new StringBuffer(); private void bindData() { viewModel.bindContent(new Action1<Settlement.SettleMessageEntity>() { @Override public void call(Settlement.SettleMessageEntity deliverModels) { settleMessageEntity = deliverModels; moneyText.setText("" + settleMessageEntity.getSettelmentTotal().get("goodstotalamount")); if (deliverModels != null) { mList = deliverModels.getSettelment(); dataLayout.setVisibility(View.VISIBLE); noDataLayout.setVisibility(View.GONE); container.removeAllViews(); LayoutInflater inflater = LayoutInflater.from(getContext()); View headerView = inflater.inflate(R.layout.common_item_hearder_layout, null); ((TextView) headerView.findViewById(R.id.common_item_01)).setText("??"); ((TextView) headerView.findViewById(R.id.common_item_02)).setText("?"); headerView.setBackgroundResource(R.color.gray_whiter); container.addView(headerView); View headdivider = new View(getContext()); headdivider.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1)); headdivider.setBackgroundColor(Color.parseColor("#DDDDDD")); container.addView(headdivider); int index = 0; for (Settlement.MarketSettleEntity marketModel : mList) { View view = inflater.inflate(R.layout.common_item_layout, null); ViewMarketItemHolder holder = new ViewMarketItemHolder(view); holder.bind(marketModel, index); container.addView(view); View divider = new View(getContext()); divider.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1)); divider.setBackgroundColor(Color.parseColor("#DDDDDD")); container.addView(divider); index++; } if (mList != null && mList.size() > 0) { View footerdivider = new View(getContext()); footerdivider.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 38)); footerdivider.setBackgroundColor(Color.parseColor("#ededed")); container.addView(footerdivider); } int sIndex = 0; List<CommonItem> tmpList = formatData(deliverModels.getSettelmentTotal()); for (CommonItem commonItem : tmpList) { View view = inflater.inflate(R.layout.common_item_hearder_layout, null); ViewItemHolder holder = new ViewItemHolder(view); holder.bind(commonItem, index); container.addView(view); View divider = new View(getContext()); divider.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1)); divider.setBackgroundColor(Color.parseColor("#DDDDDD")); container.addView(divider); if (sIndex == 0) { View footerView01 = new View(getContext()); footerView01.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 38)); footerView01.setBackgroundResource(R.color.gray_whiter); container.addView(footerView01); View viewitem = inflater.inflate(R.layout.common_item_header_layout, null); ViewItemHeaderHolder holderitem = new ViewItemHeaderHolder(viewitem); holderitem.bind("?", index); container.addView(viewitem); View divider01 = new View(getContext()); divider01.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1)); divider01.setBackgroundColor(Color.parseColor("#DDDDDD")); container.addView(divider01); } if (sIndex <= 2) { if (sIndex == 2) { View footerView01 = new View(getContext()); footerView01 .setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 38)); footerView01.setBackgroundResource(R.color.gray_whiter); container.addView(footerView01); } } if (sIndex > 2 && sIndex <= 6) { if (sIndex == 6) { View footerView01 = new View(getContext()); footerView01 .setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 38)); footerView01.setBackgroundResource(R.color.gray_whiter); container.addView(footerView01); } } sIndex++; } } else { dataLayout.setVisibility(View.GONE); noDataLayout.setVisibility(View.VISIBLE); container.setVisibility(View.GONE); } } }); } private void bindListener() { viewModel.bindPrintImageStatusChanged(printStatus); boolean isInnerPrint = SpCache.getBoolean(Constants.IS_INNER_PRINTER, false); if (isInnerPrint) { printStatus.setBackgroundResource(R.mipmap.print_right); } else { if (BlueToothPrinter.getInstance().isConnect()) { printStatus.setBackgroundResource(R.mipmap.print_right); } else { boolean isOn = SpCache.getBoolean(Constants.IS_BLUETOOTH_ON, true); if (isOn) { SpCache.putString(Constants.IS_SEARCH_PRINTER, "Y");//? BlueToothPrinter.getInstance().searchDevicesForSelf(getContext()); } printStatus.setBackgroundResource(R.mipmap.print_wrong); } } settleSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { viewModel.bindSearch(); } }); settleBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { viewModel.bindBack(); } }); commit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isPrint.isChecked()) { viewModel.onCommitClick(settleMessageEntity); } else { viewModel.bindBack(); } } }); } /** * This class is from the v7 samples of the Android SDK. It's not by me! * <p/> * See the license above for details. */ public static class DividerItemDecoration extends RecyclerView.ItemDecoration { private final int[] ATTRS = new int[] { android.R.attr.listDivider }; public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; private Drawable mDivider; private int mOrientation; public DividerItemDecoration(Context context, int orientation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = context.getResources().getDrawable(R.drawable.deliver_view_divider); a.recycle(); setOrientation(orientation); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } public void drawVertical(Canvas c, RecyclerView parent) { final int left = parent.getPaddingLeft(); final int right = parent.getWidth() - parent.getPaddingRight(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); RecyclerView v = new RecyclerView(parent.getContext()); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int top = child.getBottom() + params.bottomMargin; final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } public void drawHorizontal(Canvas c, RecyclerView parent) { final int top = parent.getPaddingTop(); final int bottom = parent.getHeight() - parent.getPaddingBottom(); final int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = parent.getChildAt(i); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); final int left = child.getRight() + params.rightMargin; final int right = left + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } @Override public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } } public class ViewMarketItemHolder extends ViewHolderBind<Settlement.MarketSettleEntity> { private TextView commonItemId; private TextView commonItem01; private TextView commonItem02; private TextView commonItem03; private TextView commonItem04; private TextView commonItem05; private RelativeLayout bigLayout; public ViewMarketItemHolder(View view) { super(view); commonItemId = (TextView) view.findViewById(R.id.common_item_id); commonItem01 = (TextView) view.findViewById(R.id.common_item_01); commonItem02 = (TextView) view.findViewById(R.id.common_item_02); commonItem03 = (TextView) view.findViewById(R.id.common_item_03); commonItem04 = (TextView) view.findViewById(R.id.common_item_04); commonItem05 = (TextView) view.findViewById(R.id.common_item_05); } @Override public void bind(Settlement.MarketSettleEntity data, final int pos) { commonItemId.setVisibility(View.VISIBLE); commonItemId.setText(data.getSeq() + "."); commonItem01.setText(data.getCustomer()); commonItem02.setText(" " + data.getGoodstotalamount()); commonItem03.setText(": " + StringUtil.priceFormatTwo(StringUtil.doubleParse(data.getDebt()))); commonItem04.setText( ": " + StringUtil.priceFormatTwo(StringUtil.doubleParse(data.getRealincome()))); commonItem05.setText( ": " + StringUtil.priceFormatTwo(StringUtil.doubleParse(data.getClearingdebt()))); } } private List<CommonItem> formatData(Map settle) { List<CommonItem> list = new ArrayList<CommonItem>(); CommonItem itemTmp = new CommonItem(); itemTmp.setItem01("?"); itemTmp.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "saletotalamount"))); list.add(itemTmp); CommonItem itemTmp1 = new CommonItem(); itemTmp1.setItem01(""); itemTmp1.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "clearingdebt"))); list.add(itemTmp1); CommonItem itemTmp2 = new CommonItem(); itemTmp2.setItem01(""); itemTmp2.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "advanceamount"))); list.add(itemTmp2); CommonItem itemTmp3 = new CommonItem(); itemTmp3.setItem01("?"); itemTmp3.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "returnamount"))); list.add(itemTmp3); CommonItem itemTmp4 = new CommonItem(); itemTmp4.setItem01("?"); itemTmp4.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "cutdownprice"))); list.add(itemTmp4); CommonItem itemTmp6 = new CommonItem(); itemTmp6.setItem01(""); itemTmp6.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "ticketamount"))); list.add(itemTmp6); CommonItem itemTmp5 = new CommonItem(); itemTmp5.setItem01(""); itemTmp5.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "debt"))); list.add(itemTmp5); CommonItem itemTmp8 = new CommonItem(); itemTmp8.setItem01("?"); itemTmp8.setItem02("0.00"); list.add(itemTmp8); CommonItem itemTmp9 = new CommonItem(); itemTmp9.setItem01("??"); itemTmp9.setItem02("0.00"); list.add(itemTmp9); CommonItem itemTmp10 = new CommonItem(); itemTmp10.setItem01("?"); itemTmp10.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle, "useadvanceamount"))); list.add(itemTmp10); // CommonItem itemTmp7 = new_version CommonItem(); // itemTmp7.setItem01("?"); // itemTmp7.setItem02(StringUtil.priceFormat(MapUtils.doubleMapGet(settle,"useadvanceamount"))); // list.add(itemTmp7); return list; } public class ViewItemHolder extends ViewHolderBind<CommonItem> { private TextView commonItem01; private TextView commonItem02; private RelativeLayout bigLayout; public ViewItemHolder(View view) { super(view); commonItem01 = (TextView) view.findViewById(R.id.common_item_01); commonItem02 = (TextView) view.findViewById(R.id.common_item_02); } @Override public void bind(CommonItem data, final int pos) { commonItem01.setText(data.getItem01()); commonItem02.setText(" " + data.getItem02()); } } public class ViewItemHeaderHolder extends ViewHolderBind<String> { private TextView commonItem01; private View root; public ViewItemHeaderHolder(View view) { super(view); this.root = view; commonItem01 = (TextView) view.findViewById(R.id.common_item_01); } @Override public void bind(String data, final int pos) { commonItem01.setText(data); root.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getContext(), SettlementGoodsListActivity.class); getContext().startActivity(intent); } }); } } }