android action bar onNavigationItemSelected

后端 未结 5 2127
死守一世寂寞
死守一世寂寞 2020-12-05 20: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 probl

相关标签:
5条回答
  • 2020-12-05 20:24

    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#).

    0 讨论(0)
  • 2020-12-05 20:31

    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<String> adapter = new ArrayAdapter<String>(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;
        }
    
    }
    
    }
    
    0 讨论(0)
  • 2020-12-05 20:37

    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.

    0 讨论(0)
  • 2020-12-05 20:40

    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

    0 讨论(0)
  • 2020-12-05 20:47

    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 
        }
    }
    
    0 讨论(0)
提交回复
热议问题