简体   繁体   English

如何将临时片段设置到我的“主屏幕”Android 工作室

[英]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 的removeadd方法( 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.

相关问题 如何在 Android Studio 中将 home 片段添加到应用程序 - How to add a home fragment to app in Android Studio 片段返回导航退出到主屏幕-Android - Fragment Back Navigation Exiting to Home Screen - Android 如何在Android Libgdx中设置纵向的主屏幕和横向的其他屏幕? - How to set home screen in portrait and other screens in landscape in android Libgdx? 如何从我的应用程序将小部件添加到 Android 主屏幕? - How to add a widget to the Android home screen from my app? Java Home与众不同吗? 如何在Android Studio中正确设置? - Java Home is different? How to set it correctly in Android Studio? 如何在android studio中设置Set JDK_HOME和JAVA_HOME环境变量? - How do i set in android studio the Set JDK_HOME and JAVA_HOME environment variables? 是否需要从主屏幕小部件转到mainActivity中的片段? - Need to go to a fragment in my mainActivity from a Home Screen Widget? android studio如何将导航活动中的片段设置为启动页面 - android studio how to set a fragment from navigation activity as startup page 如何在运行时在由 android 工作室创建的导航抽屉中设置默认片段 - how to set a default fragment at runtime in Navigation drawer that is created by android studio 为什么我的android.R.id.home没有在我的片段中调用? - Why is my android.R.id.home not called in my fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM