SplitPopupItem.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package de.tudarmstadt.informatik.hostage.ui2.popup;
  2. import android.content.Context;
  3. import android.view.MotionEvent;
  4. import android.view.View;
  5. import android.widget.TextView;
  6. import de.tudarmstadt.informatik.hostage.R;
  7. /**
  8. * Created by Julien on 16.02.14.
  9. */
  10. public class SplitPopupItem extends AbstractPopupItem {
  11. public final static String LEFT_TITLE = "LEFT_TITLE";
  12. public final static String RIGHT_TITLE = "RIGHT_TITLE";
  13. public final static String LEFT_SUBTITLE = "LEFT_SUBTITLE";
  14. public final static String RIGHT_SUBTITLE = "RIGHT_SUBTITLE";
  15. public boolean wasRightTouch;
  16. private Context context;
  17. private View left_container;
  18. private View right_container;
  19. /**
  20. * Constructor
  21. *
  22. * @param context Context
  23. */
  24. public SplitPopupItem(Context context){
  25. super(context);
  26. this.context = context;
  27. }
  28. @Override
  29. public int getLayoutId(){
  30. return R.layout.split_popup_item;
  31. }
  32. @Override
  33. public void configureItemView(View view){
  34. String leftTitle = (String) this.data.get(LEFT_TITLE);
  35. String rightTitle = (String) this.data.get(RIGHT_TITLE);
  36. String leftSubtitle = (String) this.data.get(LEFT_SUBTITLE);
  37. String rightSubtitle = (String) this.data.get(RIGHT_SUBTITLE);
  38. TextView leftTitleView = (TextView)view.findViewById(R.id.left_title_text_view);
  39. leftTitleView.setText(leftTitle);
  40. TextView leftSubtitleView = (TextView)view.findViewById(R.id.left_subtitle_text_view);
  41. if (leftSubtitle != null){
  42. leftSubtitleView.setText(leftSubtitle);
  43. } else {
  44. leftSubtitleView.setText("-");
  45. }
  46. TextView rightTitleView = (TextView)view.findViewById(R.id.right_title_text_view);
  47. rightTitleView.setText(rightTitle);
  48. TextView rightSubtilteView = (TextView)view.findViewById(R.id.right_subtitle_text_view);
  49. if (rightSubtitle != null){
  50. rightSubtilteView.setText(rightSubtitle);
  51. } else {
  52. rightSubtilteView.setText("-");
  53. }
  54. }
  55. /**
  56. * Returns the displayed object for the clicked position in the view.
  57. * E.g. the user tapped the right side, it returns the object representing the right side of the clickt view.
  58. * @param event MotionEvent
  59. * @return Object
  60. */
  61. public Object onClickedResult(MotionEvent event){
  62. this.wasRightTouch = isRightTouch(event);
  63. return this;
  64. }
  65. /**
  66. * Returns true if the user touched the right side of the view.
  67. * @return boolean isRightTouch
  68. */
  69. private boolean isRightTouch(MotionEvent event){
  70. return event.getX() > this.getRootView().getX() + (this.getRootView().getWidth() / 2);
  71. }
  72. /**
  73. * Returns the left view.
  74. * @return View the left view
  75. */
  76. private View getLeftContainer(){
  77. if(left_container == null){
  78. left_container = this.getRootView().findViewById(R.id.popup_left_container);
  79. }
  80. return left_container;
  81. }
  82. /**
  83. * Returns the right view.
  84. * @return View the right view
  85. */
  86. private View getRightContainer(){
  87. if(right_container == null){
  88. right_container = this.getRootView().findViewById(R.id.popup_right_container);
  89. }
  90. return right_container;
  91. }
  92. @Override
  93. public void onItemSelect(MotionEvent event){
  94. int blue_color = context.getResources().getColor(android.R.color.holo_blue_light);
  95. int trans_color = context.getResources().getColor(android.R.color.transparent);
  96. if(!isRightTouch(event)){
  97. getLeftContainer().setBackgroundColor(blue_color);
  98. getRightContainer().setBackgroundColor(trans_color);
  99. } else {
  100. getLeftContainer().setBackgroundColor(trans_color);
  101. getRightContainer().setBackgroundColor(blue_color);
  102. }
  103. }
  104. @Override
  105. public void onItemDeselect(MotionEvent event){
  106. int trans_color = context.getResources().getColor(android.R.color.transparent);
  107. getLeftContainer().setBackgroundColor(trans_color);
  108. getRightContainer().setBackgroundColor(trans_color);
  109. }
  110. }