android - 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;
}
解决方案
根据我的理解,您必须根据菜单点击显示一个屏幕,所以为什么不在 xml 中使用 FrameLayout 并根据点击或任何事件添加或替换片段。制作 3 个不同的 Fragment 类并使用 Google 提供的接口或任何最新的 Arch 组件。
推荐阅读
- python - command.MissingRole(missing_role) 错误处理
- asp.net-core - ASP.NET Core 中的 Web.Config 文件
- javascript - 打字稿返回“this”的类型
- mocking - KDB+/Q:表示可变数量参数的规范方法?
- c++ - 使用 CMake 构建确定性库
- vb.net - 如何将 Hashtable 添加为另一个哈希表 Option Strict On 设置中的对象?
- php - 问题整合新方法 Plaid ACH with Stripe
- sql - 查询中的重复结果
- sql - 使用条件 SQL 从两个表中获取数据
- flutter - 如何在加载时获取 Sqlite 值