简体   繁体   English

从一个片段导航到另一个片段时,第二个片段重叠

[英]When navigating from one fragment to another, the second fragment overlaps

On the click of a button I want to navigate from one fragment to another.单击一个按钮,我想从一个片段导航到另一个片段。 The problem is that when I press the button the second fragment overlaps the other.问题是当我按下按钮时,第二个片段与另一个重叠。 I searched for some answers but it doesn't seem to word in my case.我搜索了一些答案,但在我的情况下似乎没有答案。 I tried adding a background color but it still overlaps.我尝试添加背景颜色,但它仍然重叠。 So after I click the button the following thing happens:所以在我点击按钮后,会发生以下事情: 一种 乙

Here are my codes:这是我的代码:

I gave the first fragment an ID:我给第一个片段一个 ID:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"
android:background="#FFF">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="1dip"
    app:layout_constraintBottom_toTopOf="parent"
    android:background="#FFF"/>

The ID of the button is: nextFragment按钮的ID是:nextFragment

The code inside the class of the first fragment is:第一个片段的 class 里面的代码是:

        nextFragment = v.findViewById(R.id.nextFragment);
        nextFragment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Fragment fragment = new NotificationsFragment();
            FragmentManager fragmentManager = getParentFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        }
    });

Hope i gave enough information, if not don't hesitate to ask for more.希望我提供了足够的信息,如果没有,请不要犹豫,要求更多。 Thanks in advance.提前致谢。

The problem is that you are using fragment_container as the first fragment instead of the container for your first fragment.问题是您使用fragment_container作为第一个片段而不是第一个片段的容器。

In your MainActivity you have to load your first fragment inside that container.在您的MainActivity中,您必须将第一个片段加载到该容器中。 Not use it as a fragment.不要将其用作片段。

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

    if(savedInstanceState == null) {
        Fragment firstFragment = new YourFirstFragment();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragment_container, firstFragment);
        transaction.commit();
    }
}

After that, in your first fragment, when you will replace the fragment it will work great.之后,在您的第一个片段中,当您替换该片段时,它会很好用。

So the solution was simple.. I gave an Id to a framelayout inside the first fragment instead of to the root layout.所以解决方案很简单.. 我给了第一个片段内的框架布局而不是根布局的 Id。 Now the next thing is that the tab on the bottom also has to change when the button is pressed.现在接下来的事情是底部的选项卡也必须在按下按钮时更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Kotlin中从一个片段导航到另一个片段 - Navigating from one fragment to another fragment in Kotlin 导航到另一个片段时,嵌套的 ViewPager 片段会冻结一秒钟 - Nested ViewPager fragment freezes for a second when navigating to another fragment 从另一个片段传递到片段时,应用程序冻结一秒钟 - App freezes for a second when passing to a fragment from another one fragment 从一个片段导航到另一个片段时隐藏键盘 - Hide keyboard when navigating from a fragment to another DialogFragment 从它导航到另一个片段时关闭 - DialogFragment closes when navigating from it to another fragment 从另一个片段向后导航时,片段内的ViewPager为空 - ViewPager inside a fragment is empty when navigating back from another fragment 当导航到另一个片段时,Fragment中的RecyclerView崩溃 - RecyclerView in Fragment crashes when navigating to another Fragment 在将一个列表片段替换为另一个列表片段时,新片段与旧片段重叠 - On replacing one list fragment with another, the new fragment overlaps the older one 从片段导航到片段时出了点问题 - something wrong when navigating from fragment to fragment 当第二个片段已经有数据时,将数据从一个片段传递到另一个片段 - Passing data from one fragment to another when second fragment already has data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM