Fragment isVisible()如果没有则为true

时间:2013-10-16 19:58:35

标签: android android-fragments

我有一个带有片段容器的Activity和两个使用的片段,如“list fragment - > detail fragment”

private boolean isFirstFragmentAdded() {
    return getSupportFragmentManager().findFragmentByTag("FIRST_FRAGMENT_TAG") != null;
}

private boolean isFirstFragmentVisible() {
    return getSupportFragmentManager().findFragmentByTag("FIRST_FRAGMENT_TAG").isVisible();
}

private boolean isSecondFragmentAdded() {
    return getSupportFragmentManager().findFragmentByTag("SECOND_FRAGMENT_TAG") != null;
}

private boolean isSecondFragmentVisible() {
    return getSupportFragmentManager().findFragmentByTag("SECOND_FRAGMENT_TAG").isVisible();
}

private void addFirstFragment() {
    firstFragment = new FirstFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment, "FIRST_FRAGMENT_TAG").commit();
}

private void showFirstFragment() {
    firstFragment = new FirstFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, firstFragment, "FIRST_FRAGMENT_TAG").commit();
}

private void addSecondFragment() {
    secondFragment = new SecondFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, secondFragment, "SECOND_FRAGMENT_TAG").commit();
}

private void showSecondFragment() {
    secondFragment = new SecondFragment();      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, secondFragment, "SECOND_FRAGMENT_TAG").commit();
}

private void refreshVisibleFragment() {
    if (isFirstFragmentAdded() && isFirstFragmentVisible()) {
        refreshFirstFragment();
    } else if (isSecondFragmentAdded() && isSecondFragmentVisible()) {
        refreshSecondFragment();
    }
}

问题是即使我的SECOND片段可见,第一个片段总是返回isVisible() == true,所以第二个片段永远不会刷新。已经尝试删除,分离并且没有任何作用:第一个片段总是显示为已添加且可见。还要在平板电脑和手机中测试它,同样的事情发生了。也许我错过了一些东西,但无法做到正确,所以任何帮助都会受到赞赏。谢谢

1 个答案:

答案 0 :(得分:0)

尝试 设置fragment.isMenuVisible()后,fragment.setMenuUserVisibility(false)代替