[英]Home screen launcher Android
I have 5 Fragments and of course my .MainActivity. 我有5个片段,当然还有我的.MainActivity。
MainActivity
FragOne
FragTwo
---
FragFive
and then: 接着:
activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml
When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load. 加载应用程序时,它是一个空白屏幕,但是很显然,当我单击导航栏时,页面将加载。
My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want. 我的问题是,无论如何,我是否可以使用一个片段作为启动器页面,因为如果我尝试在诸如content_main或acivity_main的任何其他活动上制作主页,那么很显然它将显示在不需要的每个页面上。 But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume?
但是,如果我进行了一个新活动并将其设置为启动器,那么显然可以,但是我想我的导航栏会丢失吗? What's the best way to go about this?
最好的方法是什么?
What you want is not that big a problem. 您想要的不是什么大问题。 Just load a fragment using
SupportFragmentManager
in your MainActivity
. 只需在
MainActivity
使用SupportFragmentManager
加载片段。 If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough. 如果此片段仅是一次性视图(在使用导航抽屉更改为其他片段之前),则只需在MainActivity中简单地加载启动器片段就足够了。
Example for loading it once in MainActivity: 在MainActivity中一次加载的示例:
In your onCreate
method in MainActivity : 在MainActivity的
onCreate
方法中:
///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();
This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want) 使用NavigationDrawer更改为其他片段后,将不会看到此Launcher片段(除非您在其他地方调用它,根据您的问题,该片段是您不想要的)
If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne 如果您决定要加载FragOne本身,而不是使用单独的LauncherFragment,则需要在设置FragOne之后手动突出显示抽屉的First项目(FragOne)。
navigationView.getMenu().getItem(0).setChecked(true); ////Set Navigation drawer manually
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.