android action bar onNavigationItemSelected

匿名 (未验证) 提交于 2019-12-03 01:46:01

问题:

I'm developing for android 3+

In my action bar i have a drop-down list(see how to hide/unhide the actionbar list on android 3? for the dropdown i intend). The problem is i need to do a certain action when the user selects something, but Android calls onNavigationItemSelected() as soons as it draws the view, so no selection actually happened.

How can i detect if the user actually pressed something and it is not a fake call from android ?

public class ListDittaListener implements OnNavigationListener{      private BaseActivity activity;      private ListDittaListener()     {      }      public ListDittaListener(BaseActivity activity)     {         this.activity = activity;     }      @Override     public boolean onNavigationItemSelected(int itemPosition, long itemId)     {         MyApp appState = ((MyApp)this.activity.getApplicationContext());         appState.setDittaSelezionata( (int) itemId);          SharedPreferences settings = this.activity.getSharedPreferences(MyApp.PREFS_NAME, 0);         SharedPreferences.Editor editor = settings.edit();         editor.putInt("ditta_id_selezionata", (int) itemId);              ////////restart activity this.activity.recreate();          return false;     }  } 

回答1:

You can easily just ignore the first call to onNavigationItemSelected if you like:

public class Whatever implements OnNavigationListener {     private boolean synthetic = true;      @Override     public boolean onNavigationItemSelected(int itemPosition, long itemId) {         if (synthetic) {             synthetic = false;             return true;         }          // do whatever you really wanted here      } } 


回答2:

Method onNavigationItemSelected(int itemPosition, long itemId) will be called anyway by the action bar.

What you may want to do is to tell action bar what itemPosition it should pass to the method on the first call. (In other words, to tell action bar what navigation item should be set after activity is created). Here is the code:

mActionBarMenuSpinnerAdapter = ...; mActionBar = getActionBar(); mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); mActionBar.setListNavigationCallbacks(mActionBarMenuSpinnerAdapter, this); mActionBar.setSelectedNavigationItem(###your_default_navigation_item_here###); 

After doing this you can solve your problem by applying changes in the onNavigationItemSelected(int itemPosition, long itemId) if only itemPosition is different.



回答3:

The android system will call onNavigationItemSelected(0, 0) after the activity is setup. (Which means later than onResume()).

As other guys mentioned, you'd better not do any hack like ignore first call, otherwise the android system won't call onNavigationItemSelected() again when you select the first index. (The system thought the first item is already selected)

My solution is call actionbar.setSelectedNavigationItem(the real item# you want) after you setup the actionbar. Then the system will call onNavigationItemSelected() twice. First onNavigationItemSelected(0, 0) and then the onNavigationItemSelected(the real item#).



回答4:

Well I cannot see anything wrong in your current code.

How did you create your dropdown elements. And what element is "select" by Android after the view is created. And what are your doing in your onCreate method where the ActionBar is initialized.

I did it as instructed here and it worked for me: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown



回答5:

I have viewpager with fragments and I need set custom action bar for every fragment in pager In desired page I have navigation list, fragment fires onNavigationItemSelected automatically when I swipe pages, want to avoid this behavior and run tasks only if I selected nav item manually.

public class MyFragment extends Fragment implements ActionBar.OnNavigationListener {      private boolead fireReady = false;       @Override     public void setUserVisibleHint(boolean isVisibleToUser) {         super.setUserVisibleHint(isVisibleToUser);          // every time make it false, this method invoked on swipe action         fireReady = false;           if (isVisibleToUser) {             // setup actionbar, you also can setup action bar in activity             String[] array = getActivity().getResources().getStringArray(R.array.users_order);             ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, array);             adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);              getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);             getActivity().getActionBar().setListNavigationCallbacks(adapter, this);         }      }       @Override     public boolean onNavigationItemSelected(int itemPosition, long itemId) {          if (fireReady) {             // task fire only when you directly press navigation item             UsersTask task = new UsersTask(getActivity());             task.setTaskListener(this);             task.execute(usersUrls[itemPosition]);         } else {             // make it true first time when page displayed             fireReady = true;         }         return false;     }  }  } 


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