如何在操作栏下拉导航中设置活动项?
我正在尝试通过重新启动方向更改活动来解决问题。
我有一个带有下拉列表导航的 ActionBar,每次旋转后都会激活该列表的第一个元素。保留 fragment
内容并不困难,但我不知道如何设置活动列表项。
这是 ActionBar
的定义:
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayAdapter<CharSequence> list = ArrayAdapter
.createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActionBar().setListNavigationCallbacks(list, this);
这是我的解决方法:但
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (!application.isRotated) {
application.activePosition = itemPosition;
application.activeId = itemId;
getFragmentManager().beginTransaction()
.replace(android.R.id.content, MyFragment.newInstance(itemPosition))
.commit();
} else {
application.isRotated = false;
this.onNavigationItemSelected(application.activePosition, application.activeId);
}
return true;
}
@Override
protected void onStop() {
super.onStop();
application.isRotated = true;
}
我不确定这是最好的解决方案。
I'm trying to fix the issue with restarting activity on orientation changes.
I have an ActionBar
with drop-down list navigation and after every rotation first element of this list is being activated. Keeping fragment
content wasn't difficult, but I don't know how to set active list item.
Here is the definition of ActionBar
:
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayAdapter<CharSequence> list = ArrayAdapter
.createFromResource(this, R.array.action_list, android.R.layout.simple_dropdown_item_1line);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActionBar().setListNavigationCallbacks(list, this);
And here is my workaround:
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (!application.isRotated) {
application.activePosition = itemPosition;
application.activeId = itemId;
getFragmentManager().beginTransaction()
.replace(android.R.id.content, MyFragment.newInstance(itemPosition))
.commit();
} else {
application.isRotated = false;
this.onNavigationItemSelected(application.activePosition, application.activeId);
}
return true;
}
@Override
protected void onStop() {
super.onStop();
application.isRotated = true;
}
I'm not sure it's the best solution though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚发现这个功能。它是
setSelectedNavigationItem(intposition)
。例子:
I just found that function. It is
setSelectedNavigationItem(int position)
.Example:
从支持库 v7 开始,您只需要保存/恢复 ActionBar 的状态:
As of support library v7, you just need to save / restore the state of the ActionBar:
我也遇到了同样的问题。经过大量研究,我在这里找到了解决方案:
http://mohitum.wordpress.com/tutorials/android / --> 在提示 5 下。
实现 OnPageChangeListener 并在 onPageSelected(intposition) 中调用此方法,如下所示:
我希望这对其他人也有帮助。
I was also experiencing the same issue. Making a lot of research I found the solution here:
http://mohitum.wordpress.com/tutorials/android/ -->under Tip 5.
Implement OnPageChangeListener and in the onPageSelected(int position) call this method like this:
I hope this may help someone else too.
Gist 上的 Codesnippet
这个围绕官方 Android Bug 的 hack 为我做到了,上面的 codesnippet 没有: /
Codesnippet on Gist
this hack around the official Android Bug did it for me, the codesnippet above did not :/
这个实现对我有用(@mohitum007 响应的修改版本):
This implementation works for me (a modified version of @mohitum007's response):