安卓:导航类型-固定选项卡+滑动

3

我正在尝试在应用程序中使用Tab和Swipe,并希望使用ADT创建Activity时提供的“固定选项卡+滑动”导航类型。

现在,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;
    }
}
}

我发现了一个稍作修改的解决方案,解决了代码中的ObjectFragment片段。它对我来说很有效。 :) https://dev59.com/yXPYa4cB1Zd3GeqPoeWz - triple_t91
2个回答

5

使用switch-case设置片段非常容易,而且可以使其变得更加清晰明了。让每个片段在您的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;
}

1
什么是ObjectFragment? - Sami Eltamawy

3
但是我该如何教这个应用程序使用我的三个Fragment而不是愚蠢的Dummy Fragment呢?
您会注意到,在SectionsPagerAdapter的getItem()方法中,引用了DummySectionFragment:
@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()更改为基于“position”的switch-case,并在MainActivity.java的末尾添加了三个类(每个Fragment一个)...有没有更好或更优雅的方法来解决我的问题? :) - Jonas
@Jonas:既然我不知道你认为什么样的代码“更好或更美观”,我无法真正回答这个问题。通常我会将我的代码片段作为单独的公共Java类,因为它们可能会有点冗长。否则,你所写的代码看起来很合理。 - CommonsWare
好的 :) 非常感谢 @CommonsWare! - Jonas
Jonas - 我一直面临着同样的问题,我想知道你是否能分享你的“将getItem()转换为switch-case”以及你完整的“public Fragment getItem(int position)”示例/编辑。我正在学习Java,像你一样,我理解片段的概念,但是很难掌握示例。我假设它沿着逻辑线路进行,如果arg = 1,则调用片段xxx?我已经按照@CommonsWare的建议设置了新片段。提前致谢。 - Terran Brown

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接