我是一个有用的助手,可以翻译文本。
我正在创建一个使用ActionBarSherlock的应用程序。该应用程序由三个选项卡组成,在每个选项卡中,根据用户输入连续显示多个屏幕。我能够在选项卡之间切换片段,但在选项卡内切换片段会出现问题。我已经尝试了以下方法:
在主类中:
这个代码片段用于编程,它会替换第一个片段为第二个片段,但是当我切换标签时,第二个片段仍然可见,将新标签内容显示在旧标签内容之上。我认为在
有人能帮助我吗?
为了清晰起见,这里列出了一些重要的类:
我的主类:
TabListener(来自Android开发者网站上的“添加导航选项卡”,略有改动):
我正在创建一个使用ActionBarSherlock的应用程序。该应用程序由三个选项卡组成,在每个选项卡中,根据用户输入连续显示多个屏幕。我能够在选项卡之间切换片段,但在选项卡内切换片段会出现问题。我已经尝试了以下方法:
在主类中:
SingleStationFragment singleStationFragment = new SingleStationFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, singleStationFragment);
transaction.addToBackStack(null);
transaction.commit();
这个代码片段用于编程,它会替换第一个片段为第二个片段,但是当我切换标签时,第二个片段仍然可见,将新标签内容显示在旧标签内容之上。我认为在
onTabUnselected
中需要解除第二个片段的绑定,但我不知道如何获取对该片段的引用。有人能帮助我吗?
为了清晰起见,这里列出了一些重要的类:
我的主类:
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());
}
}
TabListener(来自Android开发者网站上的“添加导航选项卡”,略有改动):
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) {
SherlockFragment preInitializedFragment = (SherlockFragment) 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);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
而 StationsFragment
(RouteFragment
和 DelaysFragment
相同,只有不同的文本)
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);
}
}