【问题标题】:Android: "Navigation Type: Fixed Tabs + Swipe"Android:“导航类型:固定标签+滑动”
【发布时间】:2013-10-27 00:40:23
【问题描述】:

我正在尝试在应用程序中使用 Tabs + Swipe,并希望使用 ADT 在创建 Activity 时为我提供的导航类型“Fixed Tabs + Swipe”。

现在 ADT 吐出了不错的代码,我稍微修改了一下...

我完全理解代码和发生了什么......但是我怎样才能教应用程序使用我的三个 Fragment 而不是愚蠢的 Dummy Frag? :(

我找不到任何涉及 ADT“导航类型”的教程...

感谢您的帮助!

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    //Adding Tabs
        actionBar.addTab(actionBar.newTab().setText("Tab 1").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Tab 2").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Tab 3").setTabListener(this));
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
        TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
        dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}
}

【问题讨论】:

标签: android android-layout tabs


【解决方案1】:

设置片段的开关盒很简单,而且非常清晰。让你的每个片段膨胀你的 xml 中的根视图

@Override
public Fragment getItem(int index) {

    Fragment fragment = null;
    switch(index){
    case 0:
         fragment = new Fragment1();
         break;
    case 1:
         fragment = new Fragment2();
         break;
    case 2:
         fragment = new Fragment3();
         break;
    default:
        break;
    }

    //set args if necessary (which it isn't?)
    Bundle args = new Bundle();
    args.putInt(ObjectFragment.ARG_OBJECT, index + 1);
    fragment.setArguments(args);

    //return fragment
    return fragment;
}

【讨论】:

  • ObjectFragment 是什么意思?
【解决方案2】:

但是我怎样才能教应用程序使用我的三个 Fragment 而不是愚蠢的 Dummy Frag?

您会注意到DummySectionFragment 在getItem() 的SectionsPagerAdapter 中被引用:

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a DummySectionFragment (defined as a static inner class
    // below) with the page number as its lone argument.
    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
    fragment.setArguments(args);
    return fragment;
}

如果您想使用不同的片段,请修改getItem() 以返回您想要的片段,给定提供的position(从0 开始的页码)。

【讨论】:

  • 谢谢,现在我根据“位置”将 getItem() 更改为 switch-case,并在 MainActivity.java 的末尾添加了三个类(每个 Fragment 一个)... 有吗有什么方法可以更好或更好地解决我的问题? :)
  • @Jonas:因为我不知道你认为什么是“更好或更好”,所以我无法真正回答这个问题。我通常将我的片段作为单独的公共 Java 类,因为它们可能会有点冗长。否则,你所拥有的听起来很合理。
  • 好吧 :) Muchas gracias @CommonsWare !
  • Jonas - 我也遇到了同样的问题,我想知道您是否可以将您的“getItem() 共享到 switch-case”,并且可能是您完整的“公共片段 getItem(int position)”示例/编辑。我刚刚学习 Java,和你一样,我理解片段的概念,但掌握这个例子并不容易。我假设它的逻辑线路是 arg = 1 然后调用片段 xxx ?我已经按照@CommonsWare 的建议设置了新片段。提前致谢。 T
猜你喜欢
  • 1970-01-01
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多