ProfileManagerListAdapter.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package de.tudarmstadt.informatik.hostage.ui2.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.ArrayAdapter;
  10. import android.widget.ImageButton;
  11. import android.widget.ImageView;
  12. import android.widget.RelativeLayout;
  13. import android.widget.TextView;
  14. import android.widget.ViewSwitcher;
  15. import java.util.List;
  16. import de.tudarmstadt.informatik.hostage.R;
  17. import de.tudarmstadt.informatik.hostage.ui2.listeners.OnSwipeTouchListener;
  18. import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
  19. /**
  20. * @author Alexander Brakowski
  21. * @created 14.01.14 18:00
  22. */
  23. public class ProfileManagerListAdapter extends ArrayAdapter<ProfileListItem> {
  24. private class ViewHolder {
  25. public TextView labelView;
  26. public TextView textView;
  27. public ImageView imageSelected;
  28. public ImageView itemIcon;
  29. public ImageButton buttonEdit;
  30. public ImageButton buttonDelete;
  31. public ViewSwitcher switcher;
  32. public ImageView itemIconBack;
  33. public TextView labelViewBack;
  34. }
  35. private final Context context;
  36. private final List<ProfileListItem> values;
  37. private float mLastX, upX;
  38. private static final int MIN_DISTANCE = 50;
  39. private Animation inAnimationRL;
  40. private Animation outAnimationRL;
  41. private Animation inAnimationLR;
  42. private Animation outAnimationLR;
  43. public ProfileManagerListAdapter(Context context, List<ProfileListItem> objects) {
  44. super(context, R.layout.profile_manager_list_item, objects);
  45. this.context = context;
  46. this.values = objects;
  47. inAnimationRL = AnimationUtils.loadAnimation(getContext(), R.anim.in_right_to_left);
  48. outAnimationRL = AnimationUtils.loadAnimation(getContext(), R.anim.out_right_to_left);
  49. inAnimationLR = AnimationUtils.loadAnimation(getContext(), R.anim.in_left_to_right);
  50. outAnimationLR = AnimationUtils.loadAnimation(getContext(), R.anim.out_left_to_right);
  51. }
  52. @Override
  53. public View getView(final int position, View convertView, ViewGroup parent) {
  54. LayoutInflater inflater = (LayoutInflater) context
  55. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  56. View rowView = convertView;
  57. ViewHolder holder = null;
  58. final ProfileListItem item = values.get(position);
  59. if(rowView == null){
  60. rowView = inflater.inflate(R.layout.profile_manager_list_item, parent, false);
  61. holder = new ViewHolder();
  62. holder.labelView = (TextView) rowView.findViewById(R.id.profile_manager_item_label);
  63. holder.textView = (TextView) rowView.findViewById(R.id.profile_manager_item_text);
  64. holder.imageSelected = (ImageView) rowView.findViewById(R.id.profile_manager_item_activated);
  65. holder.itemIcon = (ImageView) rowView.findViewById(R.id.profile_manager_item_image);
  66. holder.buttonEdit = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_edit);
  67. holder.buttonDelete = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_delete);
  68. holder.switcher = (ViewSwitcher) rowView.findViewById(R.id.profile_manager_viewswitcher);
  69. holder.itemIconBack = (ImageView) rowView.findViewById(R.id.profile_manager_item_image_back);
  70. holder.labelViewBack = (TextView) rowView.findViewById(R.id.profile_manager_item_label_back);
  71. rowView.setTag(holder);
  72. } else {
  73. holder = (ViewHolder) rowView.getTag();
  74. }
  75. final ViewSwitcher switcher = holder.switcher;
  76. holder.switcher.setOnTouchListener(new OnSwipeTouchListener(){
  77. public void onSwipeRight() {
  78. if(item.isBackVisible){
  79. switcher.setInAnimation(inAnimationLR);
  80. switcher.setOutAnimation(outAnimationLR);
  81. switcher.setDisplayedChild(0);
  82. item.isBackVisible = false;
  83. }
  84. }
  85. public void onSwipeLeft() {
  86. if(!item.isBackVisible){
  87. switcher.setInAnimation(inAnimationRL);
  88. switcher.setOutAnimation(outAnimationRL);
  89. switcher.setDisplayedChild(1);
  90. item.isBackVisible = true;
  91. }
  92. }
  93. });
  94. holder.buttonEdit.setFocusable(false);
  95. holder.buttonDelete.setFocusable(false);
  96. holder.buttonEdit.setFocusableInTouchMode(false);
  97. holder.buttonDelete.setFocusableInTouchMode(false);
  98. holder.textView.setText(item.label);
  99. holder.labelView.setText(item.text);
  100. holder.labelViewBack.setText(item.text);
  101. holder.itemIconBack.setImageBitmap(item.icon);
  102. holder.itemIcon.setImageBitmap(item.icon);
  103. holder.switcher.setInAnimation(null);
  104. holder.switcher.setOutAnimation(null);
  105. if(item.isBackVisible){
  106. holder.switcher.setDisplayedChild(1);
  107. } else {
  108. holder.switcher.setDisplayedChild(0);
  109. }
  110. holder.switcher.setInAnimation(inAnimationRL);
  111. holder.switcher.setOutAnimation(outAnimationRL);
  112. if(!item.activated){
  113. RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams )holder.textView.getLayoutParams();
  114. lp.setMargins(0,0,0,0);
  115. holder.textView.setLayoutParams(lp);
  116. holder.imageSelected.setVisibility(View.GONE);
  117. }
  118. return rowView;
  119. }
  120. }