Close Spinner on click outside of Spinner

假装没事ソ 提交于 2019-12-01 15:14:00

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.

Ginger McMurray

Try

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

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

}

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!