Bos*_*one 7 android landscape android-viewpager actionbarsherlock android-tabs
我有一个使用ViewPager + actionbar Sherlock标签的屏幕.我setOnPageChangeListener
在寻呼机上有一个设置,它执行以下操作:
@Override
public void onPageSelected(final int position) {
actionBar.setSelectedNavigationItem(position);
}
Run Code Online (Sandbox Code Playgroud)
如果我只有很少的标签并显示所有标签,这在纵向模式下甚至在风景中都可以正常工作.但是,如果我在横向中添加更多选项卡,则会将这些选项卡折叠为单个下拉窗口小部件.当我浏览ViewPager时,该setSelectedNavigationItem
方法被执行,但现在它对下拉选择没有影响:它保持在最后选择的值.这是非常糟糕的,因为用户缺少视觉线索:标签可能会说"一个",但用户已经在第6页.
有没有办法以编程方式更改根据位置显示哪个选项卡?
PS我知道为什么会发生这种情况:这是来自com.android.internal.app.ActionBarImpl的代码:
public void setSelectedNavigationItem(int position) {
switch (mActionView.getNavigationMode()) {
case NAVIGATION_MODE_TABS:
selectTab(mTabs.get(position));
break;
case NAVIGATION_MODE_LIST:
mActionView.setDropdownSelectedPosition(position);
break;
default:
throw new IllegalStateException(
"setSelectedNavigationIndex not valid for current navigation mode");
}
}
Run Code Online (Sandbox Code Playgroud)
当我单步执行时,我可以看到导航模式仍然是NAVIGATION_MODE_TABS,尽管标签显示为列表.现在 - 我的膝盖反射是将代码放入onConfigurationChanged以适当地设置导航模式,但这不应该自动发生吗?
PPS并且已经提交了包含补丁的Android错误
Bos*_*one 11
基于我在问题中引用的Android错误(参见PPS),这里的解决方法确实有效.只需将其添加到您的寻呼机即可
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
actionBar.getTabAt(position).select();
ViewParent root = findViewById(android.R.id.content).getParent();
findAndUpdateSpinner(root, position);
}
/**
* Searches the view hierarchy excluding the content view
* for a possible Spinner in the ActionBar.
*
* @param root The parent of the content view
* @param position The position that should be selected
* @return if the spinner was found and adjusted
*/
private boolean findAndUpdateSpinner(Object root, int position)
{
if (root instanceof android.widget.Spinner)
{
// Found the Spinner
Spinner spinner = (Spinner) root;
spinner.setSelection(position);
return true;
}
else if (root instanceof ViewGroup)
{
ViewGroup group = (ViewGroup) root;
if (group.getId() != android.R.id.content)
{
// Found a container that isn't the container holding our screen layout
for (int i = 0; i < group.getChildCount(); i++)
{
if (findAndUpdateSpinner(group.getChildAt(i), position))
{
// Found and done searching the View tree
return true;
}
}
}
}
// Nothing found
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2842 次 |
最近记录: |