Close Spinner on click outside of Spinner

2019-02-21 08:52发布

问题:

I want to close an Android spinner once I click outside of the Spinner. Is that even possible?

回答1:

I've had some luck with this, even if it doesn't completely work.

The spinner adapter's get view :

public View getView(int position, View v, ViewGroup parent) {
   if (v == null) {
      LayoutInflater mLayoutInflater = mActivity.getLayoutInflater();
      v = mLayoutInflater.inflate(R.layout.user_row, null);
   }

   View tempParent = (View) parent.getParent().getParent().getParent();
   tempParent.setOnTouchListener(new MyOnTouchListener(mActivity));
   mActivity.setOpen(true);

   User getUser = mUsers.get(position);
   return v;
}

The ontouch listener :

public class MyOnTouchListener implements OnTouchListener{
   private MyActivity mOverall;
   public MyTouchListener(MyActivity overall) {
      mOverall = overall;
   }

   public boolean onTouch(View v, MotionEvent event) {
      if (mOverall.getOpen()) {
         mOverall.getWindow().setContentView(R.layout.main); //reset your activity             screen
         mOverall.initMainLayout(); //reset any code.  most likely what's in your oncreate
      }
      return false;
   }  
}

The on item selected listener :

public class MySelectedListener implements OnItemSelectedListener {
   public void onItemSelected(AdapterView<?> parent, View view, int pos,
    long id) {
     setUser(pos); //or whatever you want to do when the item is selected
     setOpen(false);         
  }
  public void onNothingSelected(AdapterView<?> parent) {}
}

The activity

Your activity with the spinner should have a global variable mOpen with get and set methods. This is because the ontouch listener tends to stay on even after the list is closed.

The limitations of this method:

It closes if you touch between the spinner and the options or to the side of the options. Touching above the spinner and below the options still won't close it.



回答2:

Try

@Override
public void onFocusChange(View v, boolean hasFocus) {

if (!hasFocus) v.setVisibility(View.GONE);

}


回答3:

I wanted to be able to determine when the spinner menu was displayed, and dismissed as well. After a lot of searching, there wasn't a lot of good solutions. I was able to accomplish this by doing the following:

  1. Created a custom spinner class, and override the following method:

    @Override
    public boolean performClick() {
    this.isDropDownMenuShown = true; //Flag to indicate the spinner menu is shown
        return super.performClick();
    }
    
  2. In my Activity, override the method onWindowFocusChanged(boolean hasFocus). If hasFocus == true && the flag in #1 is set to true, then the spinner has been dismissed (can be either by selection or tapping outside the spinner).

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (actionBarSpinner.isDropdownShown() && hasFocus) {
        actionBarSpinner.setDropdownShown(false);
        //Do you work here
    }
    

    }

Good luck!