1

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

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

  • 3Fragments我想在之间导航(每个都有一个覆盖的 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) 是活动布局中 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 下方。目标是替换它,而不是在下面再做一个。再次任何建议都会很棒!

4

2 回答 2

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"
于 2013-04-09T15:58:38.783 回答
1

解决它!

我改变了我的方法,从使用带有字符串标签的Fragment Container ID(这对我来说确实并且仍然更有意义..)。

于 2013-04-09T16:04:01.260 回答