Disabling items in a Navigation Drawer in Android

余生颓废 提交于 2020-01-03 02:45:13

问题


I'm using the Navigation Drawer Example downloadable here.

I'd like to extend this example to disable the selection of one of the planets. For simplicity, lets say I want to permanently disable the selection of Saturn, and change the text of Saturn to dark gray, and have it not highlight when a user selects it. (in reality, I would like to disable navigation programmatically when a user has changed certain values on the screen not yet saved to the device).

The closest thing I've gotten to this is to stop the selectItem() method from being called from within the onItemClick click listener, but an issue remains even if I do this - the text of "Saturn" still appears selectable and highlights when a user clicks it.

What portion of the widgets do I need to change to prevent the text of Saturn from being highlighted?
I've tried changing

mDrawerLayout.setClickable(false);
mDrawerList.setClickable(false);

But neither of these options appear to have any affect.

Any suggestions or ideas as to how to approach this problem? A couple helpful notes:

  1. I'm aware that alternatively I can Set the Drawer Lock Mode to prevent the navigation drawer from being opened, but this is much less intuitive than having grayed out nav actions
  2. Is the more appropriate thing to do here to remove the items from the list that cannot be accessed at this time? Similar to as described in the Remove ListView implementation here? The biggest disadvantage to this is that I would probably have to change the listview to describe why items have been removed from this list temporarily.

回答1:


I'd like to extend this example to disable the selection of one of the planets

Create your own subclass of ArrayAdapter where you override areAllItemsEnabled() to return false and isEnabled() to return true or false as needed.



来源:https://stackoverflow.com/questions/17531501/disabling-items-in-a-navigation-drawer-in-android

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