问题描述
我用 ActionBarSherlock
来实施动作条
与标签在我的应用程序。当我启动应用程序,并从选项卡切换到标签,一切都很好。然而,当我从纵向改为横向模式,该选项卡的内容最后一个活动,这是保持可见。更改为另一个标签结果对旧的内容顶部的新内容在图中(见图片)。
I'm using ActionBarSherlock
to implement an ActionBar
with tabs in my application.When I start the app, and switch from tab to tab, everything is fine. However, when I change from portrait to landscape mode, the content of the tab that was last active stays visible. Changing to another tab results in the drawing of the new content on top of the old content (see image).
我的主类:
public class TreinVerkeer extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupTabs(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
private void setupTabs(Bundle savedInstanceState) {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tab = actionBar.newTab().setText("STATIONS").setTabListener(new TabListener<StationsFragment>(this, "stations", StationsFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText("ROUTE").setTabListener(new TabListener<RouteFragment>(this, "route", RouteFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab().setText("DELAYS").setTabListener(new TabListener<DelaysFragment>(this, "delays", DelaysFragment.class));
actionBar.addTab(tab);
if (savedInstanceState != null) {
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
}
在Android开发者网站的TabListener(从添加导航标签,提供一些小的改动):
The TabListener (from "Adding Navigations Tabs" on the Android developer site with some minor changes):
public 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) {
// Check if the fragment is already initialized
if (mFragment == 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 it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
和 StationsFragment
(RouteFragment 和 DelaysFragment
是<$ C C $>同样的,只有不同的文本)
And StationsFragment
(RouteFragment
and DelaysFragment
are the same, with only different text)
public class StationsFragment extends SherlockFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.stationsfragment, container, false);
}
}
使用,对于布局文件 StationsFragment
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stations" />
</LinearLayout>
最后清单文件:
And finally the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TreinVerkeer"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我在开始的应用程序(因此,甚至没有改变方向),这是解决了拆卸时收到的时候了同样的问题的setContentView(R.layout.main)
在的onCreate
的主类。我无法找到一个解决方案然而。谁能帮我这个?
I had the same problem before right away when starting the app (so without even changing the orientation), this was solved by removing setContentView(R.layout.main)
in the onCreate
of the main class. I can't find a solution for this however. Can anyone help me with this?
推荐答案
FragmentManager
在配置改变目前显示会自动恢复任何片段(历史)。呼叫 findFragmentByTag
来查看是否创建和连接一个新的实例之前已存在的目的片段的一个实例。
FragmentManager
will automatically restore whatever fragment (and history) was currently displayed upon a configuration change. Call findFragmentByTag
to see if an instance of the target fragment already exists before creating and attaching a new instance.
这篇关于标签内容方向变化后更改标签后仍然显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!