gpt4 book ai didi

java - Android - 如何使用 ActionBar 在 Fragment 之间导航

转载 作者:行者123 更新时间:2023-11-29 21:45:45 25 4
gpt4 key购买 nike

ActionBar 和 Fragments 的新手,但总体上熟悉 Android。

我一直在学习一些教程。到目前为止,我有以下内容:

  • 我想在其中导航的 3 个 Fragments(每个都有一个覆盖的 onCreateView() 并加载一个简单的 View )
  • ActionBar 的自定义 TabListener(我相信问题就在那里,代码见文章底部)
  • 托管 Activity,所有这些都在其中执行

Activity 代码:

public class Activity_Home extends Activity
{
ActionBar actionBar;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

actionBar = getActionBar(); // Get reference to ActionBar

// Add some navigation tabs...

// 1. Enable ActionBar navigation tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);

// 2. Add the tabs
Tab programTab = actionBar.newTab();
Tab settingsTab = actionBar.newTab();
Tab historyTab = actionBar.newTab();

programTab.setText("Program")
.setTabListener(new TabListener<Fragment_Program>(
this, R.id.fragmentParent, Fragment_Program.class));

settingsTab.setText("Settings")
.setTabListener(new TabListener<Fragment_Settings>(
this, R.id.fragmentParent, Fragment_Settings.class));

historyTab.setText("History")
.setTabListener(new TabListener<Fragment_History>(
this, R.id.fragmentParent, Fragment_History.class));

actionBar.addTab(programTab);
actionBar.addTab(settingsTab);
actionBar.addTab(historyTab);
}
}

我的 fragment :

public class Fragment_Program extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_program, null);
}
}

..

public class Fragment_History extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_history, null);
}
}

..

public class Fragment_Settings extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_settings, null);
}
}

最后,我的 TabListener 几乎基于官方的 Google 示例。 fragmentContainer (int) 是 Activities 布局中 LinearLayout 的 XML 资源 ID,它是所有这些 Fragment 的直接父级。

public class TabListener <T extends Fragment> implements ActionBar.TabListener
{
private Fragment fragment;
private int fragmentContainer;
private final Activity activity;
private final Class<T> fragmentClass;

/** 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 fragmentClass The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, int fragmentContainer, Class<T> fragmentClass)
{
this.activity = activity;
this.fragmentContainer = fragmentContainer;
this.fragmentClass = fragmentClass;
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1)
{
// User selected the already selected tab. Usually do nothing.
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// Check if the fragment is already intialized
if (fragment == null)
{
// If not, instatiate and add it to the Activity
fragment = Fragment.instantiate(activity, fragmentClass.getName());

ft.add(fragmentContainer, fragment);
}
else
{
ft.attach(fragment);
}
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
if (fragment != null)
{
// Detach the fragment, because another one is being attached
ft.detach(fragment);
}
}

所以应用加载了,选项卡在那里,我可以选择它们,但它们似乎没有相互链接,我不确定如何继续。

任何建议都会很棒!

干杯

奖励问题:ActionBar 也在现有的 ActionBar 下方。目标是替换它,而不是在下面再做一个。再说一遍,任何建议都很好!

最佳答案

已修复!

我改变了我的方法,不再使用 Fragment Container ID(它确实对我来说更有意义......)和字符串标签,按照这个例子 Good complete Fragment and ActionBar example以及谷歌官方示例。

关于java - Android - 如何使用 ActionBar 在 Fragment 之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com