[英]How to set a temporary fragment to my "home screen" Android studio
I have started coding and I really like it, I have been on/off on diffrent project (to learn diffrent things) But now i got a problem, I "app"reciate all the help i can get.我已经开始编码并且我真的很喜欢它,我一直在不同的项目上开/关(学习不同的东西)但是现在我遇到了一个问题,我“应用”了我能得到的所有帮助。
I am using the navigation drawer and got three fragments installed.我正在使用导航抽屉并安装了三个片段。 I can navigate through the navigation drawer and select a fragment and it loads on my screen.我可以在导航抽屉中导航并选择一个片段并将其加载到我的屏幕上。 But when i start my app the screen is just white and the navigation drawer is on the left and i can pick a fragment.但是当我启动我的应用程序时,屏幕是白色的,导航抽屉在左边,我可以选择一个片段。 Is it possible to have a fragment on my "home screen" so when I start the app there is a fragment already placed on the screen BUT!是否有可能在我的“主屏幕”上有一个片段,所以当我启动应用程序时,屏幕上已经放置了一个片段但是! when i chosse another fragment in the navigation drawer i would like to have my fragment who loaded at the beginning to disappear.当我在导航抽屉中选择另一个片段时,我希望在开始时加载的片段消失。 Otherwise both fragments will show on eachother (Text on text).否则两个片段将彼此显示(文本上的文本)。 Maybe to have the temporary fragment to "finish" itself somehow.也许让临时片段以某种方式“完成”自己。 Please look at my imgur image to get my idea.请查看我的 imgur 图像以了解我的想法。 Imgur image press here to view Imgur图片按这里查看
Here is a generic solution on how to use the FragmentManager
.这是有关如何使用FragmentManager
的通用解决方案。 It should give a good idea on how to display a Fragment
.它应该提供有关如何显示Fragment
的好主意。 Besides that you could include a static Fragment in your layout.除此之外,您可以在布局中包含一个静态 Fragment。 It's up to you how to approach it.如何处理它取决于你。
So the FragmentManager
solution looks like this:因此FragmentManager
解决方案如下所示:
YourActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = AnyFragment.instantiate(this, AnyFragment.class.getName());
fragmentManager.beginTransaction().add(R.id.fragment_placeholder, fragment).addToBackStack(null).commit();
}
The target id is defined in your activity layout.xml
目标 id 在您的活动layout.xml
定义
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
Later if you want to replace the current Fragment you can use the remove
and add
method of the FragmentManager (the replace
method is kinda buggy).以后如果你想替换当前的 Fragment 你可以使用 FragmentManager 的remove
和add
方法( replace
方法有点问题)。
Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_placeholder))
fragmentManager.beginTransaction().remove(currentFragment).add(R.id.fragment_placeholder, yourNewFragment).addToBackStack(null).commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.