开发者社区> 问答> 正文

Android中的片段重叠

我试图在片段之间切换而又不丢失先前的内容,所以我决定实现这种方式:

private void loadFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().hide(fragment);
    if (!alreadyOpened[currentIndex]) {
        alreadyOpened[currentIndex] = true;
        fragmentManager.beginTransaction().add(R.id.fragment_container,fragment).commit();
    } else {
        fragmentManager.beginTransaction().show(fragment).commit();
    }
}

使用这种方法,我检查片段是否已经打开:如果是,则显示它,如果没有,则添加它。然后,我将其声明为“ alreadyOpened”。问题是,当我在片段之间切换时,我看到所有三个片段重叠。我该如何解决?当我调用loadFragment方法时,这是菜单栏:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.bbn_item1:
                currentIndex = 0;
                loadFragment(new HomeFragment());
                return true;
            case R.id.bbn_item2:
                currentIndex = 1;
                loadFragment(new PopularFragment());
                return true;
            case R.id.bbn_item3:
                 currentIndex = 2;
                loadFragment(new PlayingFragment());
                return true;
        }
        return false;
    }
};

编辑:新代码

List<Fragment> fragments = new ArrayList<>();

private void loadFragment(Fragment fragment) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if (alreadyOpened[currentIndex]) {
        ft.replace(R.id.fragment_container, fragments.get(currentIndex));
    } else {
        fragments.add(currentIndex, fragment);
        alreadyOpened[currentIndex] = true;
        ft.replace(R.id.fragment_container, fragment);
    }
    ft.addToBackStack(null);
    ft.commit();
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 17:40:42 486 0
1 条回答
写回答
取消 提交回答
  • 您不应在每次导航单击时都创建新的片段。在某个地方创建一次并将其保存在List fragments数组中。在那之后使用fragmentManager.beginTransaction().replace(R.id.fragment_layout, fragment);这里是一个小例子:

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left); //optional 
        ft.replace(R.id.fragment_layout, fragments.get(selectedFragment));
        ft.addToBackStack(null);
        ft.commit();
    

    从您的onNavigationItemSelectedsend selectedFragment变量中,匹配fragments列表中的位置。

    编辑:怎么样:

    public HomeFragment homeFragment;
    public PopularFragment popularFragment;
    public PlayingFragment playingFragment;
    
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.bbn_item1:
               if(homeFragment == null){ homeFragment  =new HomeFragment();}
               loadFragment(homeFragment )
                return true;
            case R.id.bbn_item2:
                if(homeFragment == null){ popularFragment  =new PopularFragment();}
                loadFragment(popularFragment)
                return true;
            case R.id.bbn_item3:
                 if(homeFragment == null){ playingFragment  =new PlayingFragment();}
                loadFragment(playingFragment);
                return true;
        }
        return false;
    }
    
    public void loadFragment(Fragment fragment){
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left); //optional 
        ft.replace(R.id.fragment_layout, fragment);
        ft.addToBackStack(null);
        ft.commit();
    
    }
    

    回答来源:Stack Overflow

    2020-03-24 17:41:50
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载