首页 > 解决方案 > Layouts sometimes overlap in android fragment

问题描述

My code works very well But sometimes layouts overlap The reason for this code is that the first fragment should not be reloaded. And the second and third fragments should always be reloaded

Have you ever had problems with layout overlapping code?

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
{
    Fragment selectedFragment = null;


    switch (menuItem.getItemId())
    {
        case R.id.timer:
            fm.beginTransaction().hide(active).show(fragment1).commit();
            active = fragment1;
            break;

        case R.id.favorite:
            fm.beginTransaction().hide(active).commit();
            fm.beginTransaction().detach(fragment2).attach(fragment2).commit();
            fm.beginTransaction().show(fragment2).commit();

            active = fragment2;


            break;

        case R.id.calendar:
            fm.beginTransaction().hide(active).commit();
            fm.beginTransaction().detach(fragment3).attach(fragment3).commit();
            fm.beginTransaction().show(fragment3).commit();

            active = fragment3;

            break;
    }

    return true;

}

标签: android

解决方案


根据我的理解,您必须根据菜单点击显示一个屏幕,所以为什么不在 xml 中使用 FrameLayout 并根据点击或任何事件添加或替换片段。制作 3 个不同的 Fragment 类并使用 Google 提供的接口或任何最新的 Arch 组件。


推荐阅读