Sherlock 标签无法更改方向
Posted
技术标签:
【中文标题】Sherlock 标签无法更改方向【英文标题】:Sherlock tabs not working on orientation change 【发布时间】:2013-09-04 18:42:28 【问题描述】:我设法使用 Sherlock ActionBar 设置了三个选项卡。唯一的问题是,当方向改变时,标签不能再被点击了。似乎未调用 onTabSelected() 。示例:我是纵向的,并且选择了 tab2。我变成了风景。 Tab2 仍处于选中状态,我点击 tab3 但没有任何反应。然后当我再次回到纵向时,会显示 tab3。我正在 android 2.3.6 中进行测试。
这是主要活动:
public class Activity_Main extends SherlockFragmentActivity
ActionBar.Tab tab1, tab2, tab3;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setTabs();
void setTabs()
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tab1 = actionBar.newTab();
tab2 = actionBar.newTab();
tab3 = actionBar.newTab();
tab1.setText("Week");
tab2.setText("Today");
tab3.setText("ToDo");
tab1.setTabListener(new TabListener<Fragment_Start_Week>(this, "week", Fragment_Start_Week.class));
tab2.setTabListener(new TabListener<Fragment_Start_Today>(this, "today", Fragment_Start_Today.class));
tab3.setTabListener(new TabListener<Fragment_Start_Todo>(this, "todo", Fragment_Start_Todo.class));
private class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener
private SherlockFragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/**
* Constructor used each time a new tab is created.
*
* @param activity
* The host Activity, used to instantiate the fragment
* @param tag
* The identifier tag for the fragment
* @param clz
* The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz)
mActivity = activity;
mTag = tag;
mClass = clz;
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft)
SherlockFragment preInitializedFragment = (SherlockFragment) ((FragmentActivity) mActivity).getSupportFragmentManager().findFragmentByTag(mTag);
// Check if the fragment is already initialized
if (mFragment == null && preInitializedFragment == null)
// If not, instantiate and add it to the activity
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
else if (mFragment != null)
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
else if (preInitializedFragment != null)
ft.attach(preInitializedFragment);
mFragment = preInitializedFragment;
public void onTabUnselected(Tab tab, FragmentTransaction ft)
if (mFragment != null)
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
这是一个片段:
public class Fragment_Start_Week extends SherlockFragment implements OnClickListener
void create_table()
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
return inflater.inflate(R.layout.fragment_start_week, group, false);
@Override
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
create_table();
@Override
public void onClick(View view)
...
TIA
【问题讨论】:
你解决了这个问题吗?我很好奇,因为即使我也有同样的经历。 还没有:(我找不到任何关于这个问题的帮助。如果你找到任何解决方案,请写在这里作为答案。 【参考方案1】:经过大量试验和错误后,我找到了解决此错误的以下方法:
@Override
public void onConfigurationChanged(Configuration newConfig)
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
super.onConfigurationChanged(newConfig);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
诀窍是将导航模式更改为列表,然后再更改回选项卡。
【讨论】:
以上是关于Sherlock 标签无法更改方向的主要内容,如果未能解决你的问题,请参考以下文章
Android - Sherlock ActionBar 长按图标菜单
为啥actionbar sherlock的横向高度会发生变化?
sql [从马歇尔选择最高级别的sherlock xmatches] #xmatch #sherlock #pessto #marshall