|
| 1 | +package com.tick.conditiondialog.carrier; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.v4.content.ContextCompat; |
| 5 | +import android.util.DisplayMetrics; |
| 6 | +import android.view.Gravity; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.GridView; |
| 10 | +import android.widget.PopupWindow; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | +import com.tick.conditiondialog.ConditionDialogAdapter; |
| 14 | +import com.tick.conditiondialog.R; |
| 15 | +import com.tick.conditiondialog.ViewUtil; |
| 16 | +import com.tick.conditiondialog.vehicle.VehicleCondition; |
| 17 | +import com.tick.conditiondialog.vehicle.VehicleConditionSelectListener; |
| 18 | +import com.tick.conditiondialog.vehicle.VehicleMeter; |
| 19 | +import com.tick.conditiondialog.vehicle.VehicleType; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +/** |
| 25 | + * 承运商类型条件选择器 |
| 26 | + * Created by wangcheng on 2017/11/13. |
| 27 | + */ |
| 28 | + |
| 29 | +public class CarrierTypeCdSelector extends PopupWindow { |
| 30 | + private CarrierTypeSelectListener mListener; |
| 31 | + private ConditionDialogAdapter<CarrierType> mTypeAdapter; |
| 32 | + private View mTop; |
| 33 | + |
| 34 | + public CarrierTypeCdSelector(Context context, List<CarrierType> carrierTypes, CarrierTypeSelectListener listener) { |
| 35 | + mListener = listener; |
| 36 | + try { |
| 37 | + if (carrierTypes == null) { |
| 38 | + return; |
| 39 | + } |
| 40 | + ArrayList<CarrierType> list = new ArrayList<>(); |
| 41 | + for (CarrierType carrierType : carrierTypes) { |
| 42 | + list.add(carrierType.clone()); |
| 43 | + } |
| 44 | + View container = LayoutInflater.from(context).inflate(R.layout.carrier_type_condition_popwindow_layout, |
| 45 | + null, false); |
| 46 | + mTop = container.findViewById(R.id.v_top); |
| 47 | + TextView title = container.findViewById(R.id.tv_title); |
| 48 | + TextView tvCancel = container.findViewById(R.id.tv_cancel); |
| 49 | + TextView tvSure = container.findViewById(R.id.tv_sure); |
| 50 | + GridView meterGridView = container.findViewById(R.id.gv_carrier_type); |
| 51 | + mTypeAdapter = new ConditionDialogAdapter<>(context, list, ConditionDialogAdapter.TYPE_MULTIPLY); |
| 52 | + meterGridView.setAdapter(mTypeAdapter); |
| 53 | + meterGridView.setOnItemClickListener((parent, view, position, id) -> mTypeAdapter.onCheckItemClick |
| 54 | + (position)); |
| 55 | + tvCancel.setOnClickListener(v -> hide()); |
| 56 | + tvSure.setOnClickListener(v -> { |
| 57 | + if (mListener != null) { |
| 58 | + List<CarrierType> results = new ArrayList<>(); |
| 59 | + try { |
| 60 | + List<CarrierType> carrierTypeList = mTypeAdapter.getSelectChilds(); |
| 61 | + if (carrierTypeList != null) { |
| 62 | + for (CarrierType carrierType : carrierTypeList) { |
| 63 | + results.add(carrierType.clone()); |
| 64 | + } |
| 65 | + } |
| 66 | + } catch (CloneNotSupportedException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + mListener.onSure(results); |
| 70 | + } |
| 71 | + hide(); |
| 72 | + }); |
| 73 | + //去除点击时的背景色。 |
| 74 | + title.setHighlightColor(ContextCompat.getColor(context, android.R.color.transparent)); |
| 75 | + DisplayMetrics metrics = ViewUtil.getDisplayMetrics(context); |
| 76 | + setWidth(metrics.widthPixels); |
| 77 | + setHeight(metrics.heightPixels); |
| 78 | + setContentView(container); |
| 79 | + setFocusable(true); |
| 80 | + } catch (CloneNotSupportedException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void show(View v) { |
| 86 | + showAtLocation(v, Gravity.TOP, 0, 0); |
| 87 | + mTop.animate().alpha(1f).setDuration(100).setStartDelay(600); |
| 88 | + } |
| 89 | + |
| 90 | + public void hide() { |
| 91 | + mTop.setAlpha(0f); |
| 92 | + mTop.postDelayed(this::dismiss, 10); |
| 93 | + } |
| 94 | +} |
0 commit comments