Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package com.designingknights.unboundradiogroup;
  2.  
  3. import android.app.Activity;
  4. import android.support.annotation.NonNull;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.RadioButton;
  8.  
  9. /**
  10. * Created by Timothy Winters on 5/23/2018.
  11. */
  12. public class UnboundRadioGroup implements RadioButton.OnClickListener
  13. {
  14. private ViewGroup viewGroup;
  15. private String groupTag;
  16. private int lastChecked = -1;
  17. private Activity activity;
  18.  
  19. private OnClickListener clickListener;
  20.  
  21. /**
  22. * Instantiates a new Custom radio group.
  23. *
  24. * @param activity the activity
  25. * @param viewGroupID the view group id
  26. */
  27. public UnboundRadioGroup(@NonNull Activity activity, int viewGroupID)
  28. {
  29. this.activity = activity;
  30. this.viewGroup = (ViewGroup) activity.findViewById(viewGroupID);
  31. init();
  32. }
  33.  
  34. /**
  35. * Instantiates a new Custom radio group.
  36. *
  37. * @param activity the activity
  38. * @param viewGroup the view group
  39. */
  40. public UnboundRadioGroup(@NonNull Activity activity, ViewGroup viewGroup)
  41. {
  42. this.activity = activity;
  43. this.viewGroup = viewGroup;
  44. init();
  45. }
  46.  
  47. private void init()
  48. {
  49. clickListener = null;
  50. // buildGroup(viewGroup);
  51. }
  52.  
  53. /**
  54. * Create group by tag.
  55. *
  56. * @param groupTag the group tag
  57. */
  58. public void createGroupByTag(String groupTag)
  59. {
  60. this.groupTag = groupTag;
  61. buildGroup(viewGroup);
  62. }
  63. private void buildGroup(@NonNull ViewGroup viewGroup)
  64. {
  65. int count = viewGroup.getChildCount();
  66. for (int i = 0; i < count; i++)
  67. {
  68. View view = viewGroup.getChildAt(i);
  69. if (view instanceof ViewGroup)
  70. {
  71. buildGroup((ViewGroup) view);
  72. }
  73. else if (view instanceof RadioButton)
  74. {
  75. RadioButton radioButton = (RadioButton) view;
  76. if (radioButton.getTag().equals(groupTag))
  77. {
  78. radioButton.setOnClickListener(this);
  79. }
  80. }
  81. }
  82. }
  83.  
  84. /**
  85. * Add.
  86. *
  87. * @param radioButton the radio button
  88. */
  89. public void add(@NonNull RadioButton radioButton)
  90. {
  91. radioButton.setOnClickListener(this);
  92. }
  93.  
  94. @Override
  95. public void onClick(View v)
  96. {
  97.  
  98. int checked = v.getId();
  99. if (checked != lastChecked)
  100. {
  101. ((RadioButton) v).setChecked(true);
  102. if (clickListener != null) clickListener.OnClick((RadioButton) v);
  103. if (lastChecked != -1)
  104. {
  105. ((RadioButton) activity.findViewById(lastChecked)).setChecked(false);
  106. }
  107. lastChecked = checked;
  108. }
  109. }
  110.  
  111. /**
  112. * Sets on click listener.
  113. *
  114. * @param clickListener the click listener
  115. */
  116. public void setOnClickListener(OnClickListener clickListener)
  117. {
  118. this.clickListener = clickListener;
  119. }
  120.  
  121.  
  122. /**
  123. * The interface On click listener.
  124. */
  125. public interface OnClickListener
  126. {
  127. /**
  128. * On click.
  129. *
  130. * @param radioButton the radio button
  131. */
  132. public void OnClick(RadioButton radioButton);
  133. }
  134. }
Add Comment
Please, Sign In to add comment