How to disable onItemSelectedListener to be invoked when setting selected item by code

后端 未结 12 1164
慢半拍i
慢半拍i 2021-02-02 08:05

Just wondering how you handle the following problem: a result is calculated depending on two spinners\' selected items. To handle the UI things, i.e. a user picks a new item in

相关标签:
12条回答
  • 2021-02-02 08:18
        This following method will help you to stop invoking automatically the selection listener
    
    
        yourspinnerobj.post(new Runnable() {
                    @Override
                    public void run() {
                        yourspinnerobj.setOnItemSelectedListener(yourspinnerlistener);
                    }
                });
    
    0 讨论(0)
  • 2021-02-02 08:23

    First add boolean values for stopping spinner listener call

      Boolean check = false;
    

    Then you add on Touch listener and on Item click Listener Like below code

     holder.filters.setOnTouchListener(new View.OnTouchListener() {
                   @Override
                   public boolean onTouch(View v, MotionEvent event) {
    
                       check = true;
                       return false;
                   }
               });
    
               holder.filters.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
               {
    
                   @Override
                   public void onItemSelected(AdapterView<?> parent, View arg1, int position, long id)
                   {
                       flag = filterids.get(position);
    
                       if(check)
                       {
                           check = false;
                           new Applyfilters().execute(flag,"3");
                       }else{
    
                       }
    
                   }
    
                   @Override
                   public void onNothingSelected(AdapterView<?> arg0)
                   {
                       // TODO Auto-generated method stub
                   }
               });
    

    Its simple working good for stopping server call multiple times.

    0 讨论(0)
  • 2021-02-02 08:23

    When Spinner.setSelection(position) is used, it always activates setOnItemSelectedListener()

    To avoid firing the code twice I use this solution:

    private mIsSpinnerFirstCall=true;
    
    ...
    Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //If a new value is selected (avoid activating on setSelection())
            if(!mIsSpinnerFirstCall) {
                // Your code goes gere
            }
            mIsSpinnerFirstCall = false;
        }
    
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    

    This solution is valid when you are sure that Spinner.setSelection(position) us used. Also, it is important to set mIsSpinnerFirstCall=true each time before using Spinner.setSelection(position)

    0 讨论(0)
  • 2021-02-02 08:24

    A cleaner solution, in my opinion, to differentiate between programmatic and user-initiated changes is the following:

    Create your listener for the spinner as both an OnTouchListener and OnItemSelectedListener

    public class SpinnerInteractionListener implements AdapterView.OnItemSelectedListener, View.OnTouchListener {
    
        boolean userSelect = false;
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            userSelect = true;
            return false;
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if (userSelect) { 
                // Your selection handling code here
                userSelect = false;
            }
        }
    
    }
    

    Add the listener to the spinner registering for both event types

    SpinnerInteractionListener listener = new SpinnerInteractionListener();
    mSpinnerView.setOnTouchListener(listener);
    mSpinnerView.setOnItemSelectedListener(listener);
    

    This way, any unexpected calls to your handler method due to initialization or re-initialization will be ignored.

    0 讨论(0)
  • 2021-02-02 08:24

    Add the OnItemSelectedListener for each spinner after you have set any previous value in onResume.

    0 讨论(0)
  • 2021-02-02 08:25

    In my case, since I'm triggering spinner programmatically, then I just have to add spinnerSelected flag after spinner.performClick() like below.

    private var spinnerSelected = false
    
    someView.setOnClickListener {
        spinner.performClick()
        spinnerSelected = true
    }
    
    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {
            // do nothing
        }
    
        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {
            if (spinnerSelected) {
                //... do something
    
                spinnerSelected = false
            }
          }
        }
    
    0 讨论(0)
提交回复
热议问题