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

时间:2013-04-09 15:13:16

标签: java android android-fragments android-activity android-actionbar

ActionBar和Fragments的新手,但一般熟悉Android。

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

    我想要在其间导航的
  • 3 Fragments(每个都有覆盖onCreateView()并加载一个简单的视图)
  • ActionBar的自定义TabListener(我相信问题就在那里,请参阅帖子底部的代码)
  • 托管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);
}
}

我的碎片:

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,它是所有这些片段的直接父级。

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 ..目标是替换它,而不是在另一个下面..再次任何建议都会很棒!

2 个答案:

答案 0 :(得分:1)

我想您希望以可以在标签之间滑动的方式连接标签,为了实现这一点,您应该考虑使用ViewPager

顺便说一下,这是XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

<android.support.v4.view.ViewPager 
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

</LinearLayout>

以下是您可以使用的示例代码:

public class MyTabActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener  {

  //What item on the pager is being displayed
    private int mCurPagerItem = 0;
  //This will be set to the pageer to handle navigation
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

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

        final ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //add the tabs
        mActionBar.addTab(mActionBar.newTab().setIcon(R.drawable.your_icon).setTabListener(this));
        mActionBar.addTab(mActionBar.newTab().setIcon(R.drawable.your_icon2).setTabListener(this));
        mActionBar.addTab(mActionBar.newTab().setIcon(R.drawable.your_icon3).setTabListener(this));

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setOnPageChangeListener(this);           

    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
                Fragment one = new Fragment_Program();
                return one ;
            case 1:
                Fragment two = new Fragment_Settings();
                return two ;
            case 2:
                Fragment three = new Fragment_History();
                return three ;
            default:
                break;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.title_one);
            case 1:
                return getString(R.string.title_two);
            case 2:
                return getString(R.string.title_three);
            }
            return null;
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {}

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {}

    @Override
    public void onPageSelected(int arg0) {
        //When a Page is selected, make that the current navigation item
        getActionBar().setSelectedNavigationItem(arg0);
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        //Back to top maybe?
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        //get the position
        final int pos = tab.getPosition();
        //Tell the viewpager to scroll to that page
        mViewPager.setCurrentItem(pos);
        mCurPagerItem = pos;
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

要回答您的红利问题,请在您的清单上尝试使用指定的活动代码添加以下行:

android:uiOptions="splitActionBarWhenNarrow"

答案 1 :(得分:1)

修正了它!

我改变了我的方法,使用Fragment Container ID(对我来说确实有用并且对我来说更有意义..),根据此示例Good complete Fragment and ActionBar example和Google官方示例使用String标记。