是否有可能有一个不在标签栏中的片段?

时间:2017-11-03 07:25:58

标签: android android-fragments tabs

enter image description here

现在,我的应用程序中有五个片段(标签),如图所示。它们都被列为MainActivity.java

中标签栏的成员
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage {

    private SectionsPageAdapter pageAdapter;
    ViewPager viewPager;
    public static String currentUser;

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

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, new ProfileTab());

        fragmentTransaction.commit();

        pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Sets up the ViewPager with the sections adapter

        viewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager((viewPager));

    }

    // Adds fragments to SectionsPageAdapter and gives names for the corresponding tab

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new InputTab(), "Search");
        adapter.addFragment(new ProfileTab(), "Profile");
        adapter.addFragment(new GithubTab(), "Github Repos");
        adapter.addFragment(new FollowerTab(), "Followers");
        adapter.addFragment(new FollowingTab(), "Followings");
        viewPager.setAdapter(adapter);
    }

由于我实现了一个搜索标签,我想知道是否可以创建另一个显示搜索结果的页面(或片段),当我点击结果时,我想直接将其设置为相应的选项卡(用户配置文件和回购搜索的Github Repos)。

简而言之,我如何创建一个不属于标签的页面,并在我按下“搜索”时仍然显示该页面。按钮?

1 个答案:

答案 0 :(得分:0)

在片段内实现TabLayout和ViewPager而不是激活,并用结果片段替换该片段。