Guest User

Untitled

a guest
Jan 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.view.View;
  4. import android.widget.AdapterView;
  5.  
  6. /**
  7. * Used this to differentiate between user selected and prorammatically selected
  8. * Call {@link Spinner#programmaticallySetPosition} to use this feature.
  9. * Created by vedant on 6/1/15.
  10. */
  11. public class Spinner extends android.widget.Spinner implements AdapterView.OnItemSelectedListener {
  12.  
  13. OnItemSelectedListener mListener;
  14.  
  15. /**
  16. * used to ascertain whether the user selected an item on spinner (and not programmatically)
  17. */
  18. private boolean mUserActionOnSpinner = true;
  19.  
  20. @Override
  21. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  22.  
  23. if (mListener != null) {
  24.  
  25. mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner);
  26. }
  27. // reset variable, so that it will always be true unless tampered with
  28. mUserActionOnSpinner = true;
  29. }
  30.  
  31. @Override
  32. public void onNothingSelected(AdapterView<?> parent) {
  33. if (mListener != null)
  34. mListener.onNothingSelected(parent);
  35. }
  36.  
  37. public interface OnItemSelectedListener {
  38. /**
  39. * <p>Callback method to be invoked when an item in this view has been
  40. * selected. This callback is invoked only when the newly selected
  41. * position is different from the previously selected position or if
  42. * there was no selected item.</p>
  43. *
  44. * Impelmenters can call getItemAtPosition(position) if they need to access the
  45. * data associated with the selected item.
  46. *
  47. * @param parent The AdapterView where the selection happened
  48. * @param view The view within the AdapterView that was clicked
  49. * @param position The position of the view in the adapter
  50. * @param id The row id of the item that is selected
  51. */
  52. void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected);
  53.  
  54. /**
  55. * Callback method to be invoked when the selection disappears from this
  56. * view. The selection can disappear for instance when touch is activated
  57. * or when the adapter becomes empty.
  58. *
  59. * @param parent The AdapterView that now contains no selected item.
  60. */
  61. void onNothingSelected(AdapterView<?> parent);
  62. }
  63.  
  64. public void programmaticallySetPosition(int pos, boolean animate) {
  65. mUserActionOnSpinner = false;
  66. setSelection(pos, animate);
  67. }
  68.  
  69. public void setOnItemSelectedListener (OnItemSelectedListener listener) {
  70. mListener = listener;
  71. }
  72.  
  73. public Spinner(Context context) {
  74. super(context);
  75. super.setOnItemSelectedListener(this);
  76. }
  77.  
  78. public Spinner(Context context, int mode) {
  79. super(context, mode);
  80. super.setOnItemSelectedListener(this);
  81. }
  82.  
  83. public Spinner(Context context, AttributeSet attrs) {
  84. super(context, attrs);
  85. super.setOnItemSelectedListener(this);
  86. }
  87.  
  88. public Spinner(Context context, AttributeSet attrs, int defStyle) {
  89. super(context, attrs, defStyle);
  90. super.setOnItemSelectedListener(this);
  91. }
  92.  
  93. public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) {
  94. super(context, attrs, defStyle, mode);
  95. super.setOnItemSelectedListener(this);
  96. }
  97. }
Add Comment
Please, Sign In to add comment