PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/BMovil_v940/app/src/main/java/suitebancomer/classes/gui/views/SeleccionHorizontalViewController.java

https://gitlab.com/edddea/api_contratacion_arq
Java | 295 lines | 214 code | 33 blank | 48 comment | 33 complexity | c2245e644f6f8d36d61e6c7ca23c2b16 MD5 | raw file
  1. package suitebancomer.classes.gui.views;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.view.LayoutInflater;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.widget.HorizontalScrollView;
  8. import android.widget.ImageButton;
  9. import android.widget.LinearLayout;
  10. import com.bancomer.mbanking.R;
  11. import com.bancomer.mbanking.SuiteApp;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import bancomer.api.common.model.Compania;
  15. import bancomer.api.common.model.Convenio;
  16. import suitebancomer.classes.common.GuiTools;
  17. import suitebancomer.classes.gui.delegates.BaseDelegate;
  18. public class SeleccionHorizontalViewController extends LinearLayout implements View.OnClickListener, View.OnTouchListener {
  19. public final static String SELECT_SUFFIX = "_select";
  20. public final static String IMAGE_FORMAT = ".png";
  21. public final static String OTHER_ELEMENT_IMAGE_NAME = "otro.png";
  22. private static HashMap<String, Integer> imagenes;
  23. private static HashMap<String, Integer> imagenesSelec;
  24. private HorizontalScrollView componenteHorizontal;
  25. private LinearLayout contenidoComponenteHorizontal;
  26. private Object itemSeleccionado;
  27. private LayoutInflater inflater;
  28. private ArrayList<Object> componentes;
  29. private ArrayList<ImageButton> arregloBotones;
  30. private BaseDelegate delegate;
  31. private boolean blocked;
  32. public SeleccionHorizontalViewController(Context context,LinearLayout.LayoutParams layoutParams,
  33. ArrayList<Object> listaComponentes, BaseDelegate delegate,
  34. boolean addOtherElement) {
  35. super(context);
  36. inflater = LayoutInflater.from(context);
  37. LinearLayout viewLayout = (LinearLayout) inflater.inflate(R.layout.layout_seleccion_horizontal_view, this, true);
  38. viewLayout.setLayoutParams(layoutParams);
  39. componenteHorizontal = (HorizontalScrollView)findViewById(R.id.scroll_seleccion_horizontal);
  40. contenidoComponenteHorizontal = (LinearLayout) findViewById(R.id.contenido_seleccion_horizontal);
  41. GuiTools.getCurrent().scalePaddings(contenidoComponenteHorizontal);
  42. this.componentes = listaComponentes;
  43. if (addOtherElement) {
  44. Convenio otroConvenio = new Convenio(SuiteApp.appContext.getString(R.string.servicesPayment_otherServices), OTHER_ELEMENT_IMAGE_NAME, "-1", Integer.toString(componentes.size()+1));
  45. componentes.add(otroConvenio);
  46. }
  47. this.delegate = delegate;
  48. reloadItems();
  49. }
  50. static {
  51. imagenes = new HashMap<String, Integer>();
  52. imagenesSelec = new HashMap<String, Integer>();
  53. imagenes.put("telcel.png", R.drawable.telcel);
  54. imagenesSelec.put("telcel.png", R.drawable.telcel_select);
  55. imagenes.put("nextel.png", R.drawable.nextel);
  56. imagenesSelec.put("nextel.png", R.drawable.nextel_select);
  57. imagenes.put("unefon.png", R.drawable.unefon);
  58. imagenesSelec.put("unefon.png", R.drawable.unefon_select);
  59. imagenes.put("iusacell.png", R.drawable.iusacel);
  60. imagenesSelec.put("iusacell.png", R.drawable.iusacel_select);
  61. imagenes.put("movistar.png", R.drawable.movistar);
  62. imagenesSelec.put("movistar.png", R.drawable.movistar_select);
  63. imagenes.put("telmex.png", R.drawable.telmex);
  64. imagenesSelec.put("telmex.png", R.drawable.telmex_select);
  65. imagenes.put("cfe.png", R.drawable.cfe);
  66. imagenesSelec.put("cfe.png", R.drawable.cfe_select);
  67. imagenes.put("liverpool.png", R.drawable.liverpool);
  68. imagenesSelec.put("liverpool.png", R.drawable.liverpool_select);
  69. imagenes.put("sky.png", R.drawable.btn_sky);
  70. imagenesSelec.put("sky.png", R.drawable.btn_skyon);
  71. imagenes.put("telcelcie.png", R.drawable.telcelcie);
  72. imagenesSelec.put("telcelcie.png", R.drawable.telcelcie_select);
  73. imagenes.put("avon.png", R.drawable.avon);
  74. imagenesSelec.put("avon.png", R.drawable.avon_select);
  75. imagenes.put("jafra.png", R.drawable.jafra);
  76. imagenesSelec.put("jafra.png", R.drawable.jafra_select);
  77. imagenes.put("otro.png", R.drawable.otro);
  78. imagenesSelec.put("otro.png", R.drawable.otro_select);
  79. }
  80. public void setSelectedItem(Object item) {
  81. this.itemSeleccionado = item;
  82. changeSelection();
  83. }
  84. public void setOtroAsSelected() {
  85. this.itemSeleccionado = componentes.get(componentes.size()-1);
  86. changeSelection();
  87. }
  88. public Object getSelectedItem()
  89. {
  90. return itemSeleccionado;
  91. }
  92. public void changeSelection()
  93. {
  94. contenidoComponenteHorizontal.removeAllViews();
  95. reloadItems();
  96. }
  97. public void reloadItems()
  98. {
  99. GuiTools guitools = GuiTools.getCurrent();
  100. int selectedLateralSize = guitools.getEquivalenceInPixels(68);
  101. int basePading = guitools.getEquivalenceInPixels(8);
  102. LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
  103. params.setMargins(SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_top_margin_no_selected),
  104. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_side_margin_no_selected),
  105. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_top_margin_no_selected),
  106. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_side_margin_no_selected));
  107. LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
  108. params2.setMargins(SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_top_margin_selected),
  109. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_side_margin_selected),
  110. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_top_margin_selected),
  111. SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_side_margin_selected));
  112. LinearLayout.LayoutParams selectedButtonParams = new LayoutParams(selectedLateralSize, selectedLateralSize);
  113. selectedButtonParams.setMargins(basePading, 0, basePading, 0);
  114. arregloBotones = new ArrayList<ImageButton>();
  115. ImageButton imagen = null;
  116. ImageButton selectedButton = null;
  117. boolean imageFound = false;
  118. Object obj = null;
  119. Integer currentImage = null;
  120. int componentsSize = componentes.size();
  121. // for (int i = 0; i < componentsSize; i++) {
  122. // imagen = new ImageButton(getContext());
  123. // if ((itemSeleccionado == componentes.get(i)) ||
  124. // (!imageFound && i==componentsSize-1)) {
  125. // if (itemSeleccionado instanceof Convenio) {
  126. // currentImage = (Integer)imagenesSelec.get(((Convenio)itemSeleccionado).getNombreImagen());
  127. // if (currentImage != null) {
  128. // imagen.setBackgroundResource(currentImage.intValue());
  129. // } else {
  130. // imagen.setBackgroundResource(imagenesSelec.get(OTHER_ELEMENT_IMAGE_NAME));
  131. // }
  132. // } else if (itemSeleccionado instanceof Compania) {
  133. // currentImage = imagenesSelec.get(((Compania)itemSeleccionado).getNombreImagen());
  134. // if (currentImage != null) {
  135. // imagen.setBackgroundResource(currentImage.intValue());
  136. // } else {
  137. // imagen.setBackgroundResource(imagenesSelec.get(OTHER_ELEMENT_IMAGE_NAME));
  138. // }
  139. // }
  140. // imagen.setLayoutParams(params2);
  141. // selectedButton = imagen;
  142. // imageFound = true;
  143. // } else {
  144. // obj = componentes.get(i);
  145. // if (obj instanceof Convenio) {
  146. // currentImage = imagenes.get(((Convenio)obj).getNombreImagen());
  147. // if (currentImage != null) {
  148. // imagen.setBackgroundResource(currentImage.intValue());
  149. // } else {
  150. // imagen.setBackgroundResource(imagenes.get(OTHER_ELEMENT_IMAGE_NAME));
  151. // }
  152. // } else if (obj instanceof Compania) {
  153. // currentImage = imagenes.get(((Compania)obj).getNombreImagen());
  154. // if (currentImage != null) {
  155. // imagen.setBackgroundResource(currentImage.intValue());
  156. // } else {
  157. // imagen.setBackgroundResource(imagenes.get(OTHER_ELEMENT_IMAGE_NAME));
  158. // }
  159. //
  160. // }
  161. // imagen.setLayoutParams(params2);
  162. // }
  163. //
  164. // imagen.setId(i);
  165. // imagen.setOnClickListener(this);
  166. // arregloBotones.add(imagen);
  167. // contenidoComponenteHorizontal.addView(imagen);
  168. // }
  169. for (int i = 0; i < componentsSize; i++) {
  170. imagen = new ImageButton(getContext());
  171. if ((itemSeleccionado == componentes.get(i)) || (!imageFound && i==componentsSize-1)) {
  172. if (itemSeleccionado instanceof Convenio) {
  173. currentImage = (Integer)imagenesSelec.get(((Convenio)itemSeleccionado).getNombreImagen());
  174. if (currentImage != null) {
  175. imagen.setBackgroundResource(currentImage.intValue());
  176. } else {
  177. imagen.setBackgroundResource(imagenesSelec.get(OTHER_ELEMENT_IMAGE_NAME));
  178. }
  179. } else if (itemSeleccionado instanceof Compania) {
  180. currentImage = imagenesSelec.get(((Compania)itemSeleccionado).getNombreImagen());
  181. if (currentImage != null) {
  182. imagen.setBackgroundResource(currentImage.intValue());
  183. } else {
  184. imagen.setBackgroundResource(imagenesSelec.get(OTHER_ELEMENT_IMAGE_NAME));
  185. }
  186. }
  187. imagen.setLayoutParams(selectedButtonParams);
  188. selectedButton = imagen;
  189. imageFound = true;
  190. } else {
  191. obj = componentes.get(i);
  192. if (obj instanceof Convenio) {
  193. currentImage = imagenes.get(((Convenio)obj).getNombreImagen());
  194. if (currentImage != null) {
  195. imagen.setBackgroundResource(currentImage.intValue());
  196. } else {
  197. imagen.setBackgroundResource(imagenes.get(OTHER_ELEMENT_IMAGE_NAME));
  198. }
  199. } else if (obj instanceof Compania) {
  200. currentImage = imagenes.get(((Compania)obj).getNombreImagen());
  201. if (currentImage != null) {
  202. imagen.setBackgroundResource(currentImage.intValue());
  203. } else {
  204. imagen.setBackgroundResource(imagenes.get(OTHER_ELEMENT_IMAGE_NAME));
  205. }
  206. }
  207. imagen.setLayoutParams(selectedButtonParams);
  208. }
  209. imagen.setId(i);
  210. imagen.setOnClickListener(this);
  211. arregloBotones.add(imagen);
  212. contenidoComponenteHorizontal.addView(imagen);
  213. }
  214. }
  215. public void bloquearComponente() {
  216. blocked = true;
  217. componenteHorizontal.setOnTouchListener(this);
  218. componenteHorizontal.setFocusable(false);
  219. componenteHorizontal.setClickable(false);
  220. int arregloBotonesSize = arregloBotones.size();
  221. ImageButton currentImageButton = null;
  222. for (int i=0; i<arregloBotonesSize; i++) {
  223. currentImageButton = arregloBotones.get(i);
  224. currentImageButton.setOnTouchListener(this);
  225. currentImageButton.setFocusable(false);
  226. currentImageButton.setClickable(false);
  227. }
  228. }
  229. @Override
  230. public void onClick(View v) {
  231. ImageButton selectedButton = null;
  232. for (int i = 0; i < arregloBotones.size(); i++) {
  233. selectedButton = arregloBotones.get(i);
  234. if (v == selectedButton) {
  235. Object selectedComponent = componentes.get(selectedButton.getId());
  236. setSelectedItem(selectedComponent);
  237. if (delegate != null) {
  238. delegate.performAction(selectedComponent);
  239. }
  240. }
  241. }
  242. }
  243. @Override
  244. public void onWindowFocusChanged(boolean hasWindowFocus) {
  245. super.onWindowFocusChanged(hasWindowFocus);
  246. if (hasWindowFocus) {
  247. if (itemSeleccionado == null) {
  248. setSelectedItem(componentes.get(0));
  249. }
  250. ImageButton selectedButton = arregloBotones.get(componentes.indexOf(itemSeleccionado));
  251. Rect outRect = new Rect();
  252. float rightOffset = SuiteApp.appContext.getResources().getDimensionPixelOffset(R.dimen.seleccionHorizontal_side_margin_no_selected);
  253. contenidoComponenteHorizontal.getLocalVisibleRect(outRect);
  254. int scroller = selectedButton.getRight() - outRect.right + (int)rightOffset;
  255. componenteHorizontal.scrollBy(scroller, outRect.top);
  256. }
  257. }
  258. @Override
  259. public boolean onTouch(View v, MotionEvent event) {
  260. return blocked;
  261. }
  262. }