Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. public CustomSpinnerClass(Context context, int mode) {
  2. super(context, mode);
  3. super.setOnItemSelectedListener(this);
  4. }
  5.  
  6. public CustomSpinnerClass(Context context, AttributeSet attrs, int defStyle, int mode) {
  7. super(context, attrs, defStyle, mode);
  8. super.setOnItemSelectedListener(this);
  9.  
  10. }
  11.  
  12. import android.content.Context;
  13. import android.util.AttributeSet;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16.  
  17. public class CustomSpinnerClass extends androidx.appcompat.widget.AppCompatSpinner implements AdapterView.OnItemSelectedListener {
  18.  
  19. OnItemSelectedListener mListener;
  20. private boolean mUserActionOnSpinner = true;
  21.  
  22. public CustomSpinnerClass(Context context) {
  23. super(context);
  24. super.setOnItemSelectedListener(this);
  25. }
  26.  
  27. public CustomSpinnerClass(Context context, int mode) {
  28. super(context, mode);
  29. super.setOnItemSelectedListener(this);
  30. }
  31.  
  32. public CustomSpinnerClass(Context context, AttributeSet attrs) {
  33. super(context, attrs);
  34. super.setOnItemSelectedListener(this);
  35. }
  36.  
  37. public CustomSpinnerClass(Context context, AttributeSet attrs, int defStyle) {
  38. super(context, attrs, defStyle);
  39. super.setOnItemSelectedListener(this);
  40. }
  41.  
  42. public CustomSpinnerClass(Context context, AttributeSet attrs, int defStyle, int mode) {
  43. super(context, attrs, defStyle, mode);
  44. super.setOnItemSelectedListener(this);
  45.  
  46. }
  47.  
  48. @Override
  49. public void setSelection(int position, boolean animate) {
  50. boolean sameSelected = position == getSelectedItemPosition();
  51. super.setSelection(position, animate);
  52. if (sameSelected) {
  53. //TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
  54. getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
  55. }
  56. }
  57.  
  58. @Override
  59. public void setSelection(int position) {
  60. boolean sameSelected = position == getSelectedItemPosition();
  61. super.setSelection(position);
  62. if (sameSelected) {
  63. //TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
  64. getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
  65. }
  66. }
  67.  
  68. @Override
  69. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  70. if (mListener != null) {
  71.  
  72. mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner);
  73. }
  74. // reset variable, so that it will always be true unless tampered with
  75. mUserActionOnSpinner = true;
  76. }
  77.  
  78. @Override
  79. public void onNothingSelected(AdapterView<?> parent) {
  80. if (mListener != null)
  81. mListener.onNothingSelected(parent);
  82. }
  83.  
  84. public void programmaticallySetPosition(int pos, boolean animate) {
  85. mUserActionOnSpinner = false;
  86. setSelection(pos, animate);
  87. }
  88.  
  89. public void setOnItemSelectedListener(OnItemSelectedListener listener) {
  90. mListener = listener;
  91. }
  92.  
  93. public interface OnItemSelectedListener {
  94. /**
  95. * <p>Callback method to be invoked when an item in this view has been
  96. * selected. This callback is invoked only when the newly selected
  97. * position is different from the previously selected position or if
  98. * there was no selected item.</p>
  99. * <p>
  100. * Impelmenters can call getItemAtPosition(position) if they need to access the
  101. * data associated with the selected item.
  102. *
  103. * @param parent The AdapterView where the selection happened
  104. * @param view The view within the AdapterView that was clicked
  105. * @param position The position of the view in the adapter
  106. * @param id The row id of the item that is selected
  107. */
  108. void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected);
  109.  
  110. /**
  111. * Callback method to be invoked when the selection disappears from this
  112. * view. The selection can disappear for instance when touch is activated
  113. * or when the adapter becomes empty.
  114. *
  115. * @param parent The AdapterView that now contains no selected item.
  116. */
  117. void onNothingSelected(AdapterView<?> parent);
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement