1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| public class extends BaseAdapter {
public final static int BLUETOOTH_HEADER = 0; public final static int BLUETOOTH_PRINTER = 1; public final static int NET_HEADER = 2; public final static int NET_PRINTER = 3; public final static int CLOUD_HEADER = 4; public final static int CLOUD_PRINTER = 5;
private Context mContext; private LayoutInflater mInflater; private OnItemFunctionClickListener listener;
private List<PrinterInfo> mPrinters = new ArrayList<>(); private int blueToothCount = 0; private int netCount = 0; private int cloudCount = 0;
public (Context context, List<PrinterInfo> printers, SwipePartMenuListView listView) { mContext = context; mPrinters = printers; mInflater = LayoutInflater.from(context);
getNotSwipeItem(listView); }
private void getNotSwipeItem(SwipePartMenuListView listView) { blueToothCount = 0; netCount = 0; cloudCount = 0;
for (PrinterInfo info : mPrinters) { if (info.printerType == PrinterInfo.TYPE_BLUETOOTH) { blueToothCount++; } else if (info.printerType == PrinterInfo.TYPE_NETWORK) { netCount++; } else if (info.printerType == PrinterInfo.TYPE_CLOUD) { cloudCount++; } }
List<Integer> titleList = new LinkedList<>(); titleList.add(0); titleList.add(1 + blueToothCount); titleList.add(2 + blueToothCount + netCount); listView.setCannotSwipePositionList(titleList); }
public void setPrinters(List<PrinterInfo> printers, SwipePartMenuListView listView) { this.mPrinters = printers;
getNotSwipeItem(listView); }
public void setListener(OnItemFunctionClickListener listener) { this.listener = listener; }
public View getView(int position, View convertView, ViewGroup parent) { PrinterManagerViewHolder holder; final int viewType = getItemViewType(position); holder = new PrinterManagerViewHolder(); if (viewType == BLUETOOTH_HEADER || viewType == NET_HEADER || viewType == CLOUD_HEADER) { if (convertView == null) { convertView = mInflater.inflate(R.layout.item_printer_title, parent, false); holder.headerTitle = (TextView) convertView.findViewById(R.id.printer_category_tv); holder.headerIcon = (ImageView) convertView.findViewById(R.id.printer_icon_iv); holder.headerAdd = (ImageView) convertView.findViewById(R.id.printer_add_device_iv); holder.headerDivider = convertView.findViewById(R.id.printer_title_divider); convertView.setTag(holder); } else { holder = (PrinterManagerViewHolder) convertView.getTag(); } } else { if (convertView == null) { convertView = mInflater.inflate(R.layout.item_printer_devices, parent, false); holder.connect = (TextView) convertView.findViewById(R.id.printer_connect_tv); holder.divider = convertView.findViewById(R.id.printer_last_divider); holder.setting = (TextView) convertView.findViewById(R.id.printer_setting_tv); holder.title = (TextView) convertView.findViewById(R.id.printer_title_tv); holder.connectIcon = (ImageView) convertView.findViewById(R.id.printer_connect_iv); convertView.setTag(holder); } else { holder = (PrinterManagerViewHolder) convertView.getTag(); } } switch (viewType) { case BLUETOOTH_HEADER: holder.headerTitle.setText("蓝牙打印机"); holder.headerAdd.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_setting_goods_add)); holder.headerAdd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { BlueToothPrinterActivity.navigateTo(mContext); } }); break; case NET_HEADER: holder.headerTitle.setText("网络打印机"); holder.headerAdd.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_setting_goods_add)); holder.headerAdd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { NetPrinterActivity.navigateTo(mContext); } }); break; case CLOUD_HEADER: holder.headerTitle.setText("云打印机"); holder.headerAdd.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_setting_goods_scan)); if (position == getCount() - 1) { holder.headerDivider.setVisibility(View.VISIBLE); } break; case BLUETOOTH_PRINTER: PrinterInfo printer = mPrinters.get(position - 1); initPrinter(holder, printer); break; case NET_PRINTER: PrinterInfo netPrinter = mPrinters.get(position - 2); initPrinter(holder, netPrinter); break; case CLOUD_PRINTER: if (position == getCount() - 1) { holder.divider.setVisibility(View.VISIBLE); } PrinterInfo cloudPrinter = mPrinters.get(position - 3); initPrinter(holder, cloudPrinter); break; } return convertView; }
private void initPrinter(PrinterManagerViewHolder holder, final PrinterInfo printerInfo) { boolean connected = false; holder.title.setText(printerInfo.name);
HashMap<String, IPrinter> printers = PrinterManager.getInstance().getPrinterList(); if (printers.keySet().contains(printerInfo.mac)) { connected = true; holder.connect.setText("断开"); holder.connectIcon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_connect)); } else { holder.connect.setText("连接"); holder.connectIcon.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_break)); }
holder.setting.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { PrinterSettingActivity.navigateTo(mContext, printerInfo); } }); final boolean finalConnected = connected; holder.connect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (listener != null) { listener.onConnectClicked(printerInfo, finalConnected); } } }); }
@Override public int getItemViewType(int position) { if (position == 0) { return BLUETOOTH_HEADER; } else if (position > 0 && position <= blueToothCount) { return BLUETOOTH_PRINTER; } else if (position == blueToothCount + 1) { return NET_HEADER; } else if (position > blueToothCount + 1 && position <= netCount + blueToothCount + 1) { return NET_PRINTER; } else if (position == netCount + blueToothCount + 2) { return CLOUD_HEADER; } else { return CLOUD_PRINTER; } }
@Override public int getCount() { if (mPrinters != null) { return mPrinters.size() + 3; } return 0; }
@Override public Object getItem(int position) { return null; }
@Override public long getItemId(int position) { return position; }
private class PrinterManagerViewHolder {
private TextView headerTitle; private ImageView headerIcon; private ImageView headerAdd; private View headerDivider; private TextView connect; private TextView title; private View divider; private TextView setting; private ImageView connectIcon; }
public interface OnItemFunctionClickListener { void onConnectClicked(PrinterInfo printer, boolean connected); }
}
|