Android Studio 默认“Tabbed Activity”,如何在片段间滑动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25141219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:08:41  来源:igfitidea点击:

Android Studio default "Tabbed Activity", how to swipe through fragments?

androidandroid-fragmentsswipe

提问by itsauser

Complete beginner here..

完整的初学者在这里..

I have used the "Tabbed Activity" default from the New Project Wizard.

我使用了“新建项目向导”中的默认“选项卡式活动”。

I am trying to get it to swipe through 3 different fragments, however I simply cant see where to tell the program to do it. Do I load them in as an array, if yes where should I do it and how do I instantiate the different fragments?

我试图让它在 3 个不同的片段中滑动,但是我根本看不到在哪里告诉程序这样做。我是否将它们作为数组加载,如果是,我应该在哪里做以及如何实例化不同的片段?

Any pointers and/or solutions is very appreciated.

非常感谢任何指针和/或解决方案。

回答by Sushil

you can create a pager adapter from where you can call fragments based on tabs.

您可以创建一个寻呼机适配器,您可以从中调用基于选项卡的片段。

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment();
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

}

and initialize the tabs values in onCreate method of main activity to get tabs working

并在主要活动的 onCreate 方法中初始化选项卡值以使选项卡工作

private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

}

}

回答by Tha Leang

I know that this question is old, but hopefully it can help someone. This was really frustrating for me as well since I'm trying my best to learn Android but the wizard seems to have provided an incomplete template.

我知道这个问题很老,但希望它可以帮助某人。这对我来说也很令人沮丧,因为我正在尽最大努力学习 Android,但向导似乎提供了一个不完整的模板。

Anyways,

无论如何,

In fragment_main.xml, add some text to the TextView and the pages will show up now that there's content.

在 中fragment_main.xml,向 TextView 添加一些文本,现在页面将显示有内容。

<TextView android:id="@+id/section_label" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World" />