列表片段不消失

List fragment not disappearing

根据我的代码,每当我 运行 我的应用程序并单击一个列表项时,预期的片段确实会出现,但列表片段不会从视图中消失(参见随附的屏幕截图)。需要做什么才能从用户的视图中删除列表?

    FragmentItem1 newFragment = new FragmentItem1();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.master_container, newFragment);                         

transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.addToBackStack(null);
    transaction.commit();

后退按钮方法

@Override
public void onBackPressed() {
    if (mTwoPane) {
        FragmentMainList newFragment = new FragmentMainList();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the master_container view with the above fragment
        transaction.replace(R.id.master_container, newFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        // Commit the transaction
        transaction.commit();
    }
}

假设 'master_container' 是与您的 ListView 相邻的布局,您当前观察到的是正确的行为。 ListView不会自动消失

调用 ListView 上的 setVisibility 使其消失。此方法使用参数ListView.GONE