无法打开新片段

时间:2016-08-07 10:12:48

标签: android android-fragments

单击按钮时打开新片段时遇到问题。当我点击设置上的按钮时,新片段位于旧片段之上。我经历过一些搜索,但似乎无法解决这个问题。

图片和相关代码放在下面。任何帮助,将不胜感激。

enter image description here

更新的图片(片段无法正常显示): enter image description here

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.phase1:
            Newsfeed1Fragment newFragment = new Newsfeed1Fragment();
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_content, newFragment);
            fragmentTransaction.addToBackStack(String.valueOf(newFragment));
            fragmentTransaction.commit();
            break;
        case R.id.phase2b:
            Fragment newFragment1 = new NewsfeedFragment();
            android.support.v4.app.FragmentManager fragmentManager1 = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
            fragmentTransaction1.replace(R.id.frame_content, newFragment1);
            fragmentTransaction1.addToBackStack(String.valueOf(newFragment1));
            fragmentTransaction1.commit();
            break;
        case R.id.phase2a:
            Fragment newFragment2 = new Newsfeed2aFragment();
            android.support.v4.app.FragmentManager fragmentManager2 = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
            fragmentTransaction2.replace(R.id.frame_content, newFragment2);
            fragmentTransaction2.addToBackStack(String.valueOf(newFragment2));
            fragmentTransaction2.commit();
            break;
        case R.id.phase3a:
            Fragment newFragment3 = new Newsfeed3aFragment();
            android.support.v4.app.FragmentManager fragmentManager3 = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction3 = fragmentManager3.beginTransaction();
            fragmentTransaction3.replace(R.id.frame_content, newFragment3);
            fragmentTransaction3.addToBackStack(String.valueOf(newFragment3));
            fragmentTransaction3.commit();
            break;
        case R.id.phase3b:
            Fragment newFragment4 = new Newsfeed3bFragment();
            android.support.v4.app.FragmentManager fragmentManager4 = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction4 = fragmentManager4.beginTransaction();
            fragmentTransaction4.replace(R.id.frame_content, newFragment4);
            fragmentTransaction4.addToBackStack(String.valueOf(newFragment4));
            fragmentTransaction4.commit();
            break;
        case R.id.phase4:
            Fragment newFragment5 = new Newsfeed4Fragment();
            android.support.v4.app.FragmentManager fragmentManager5 = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction5 = fragmentManager5.beginTransaction();
            fragmentTransaction5.replace(R.id.frame_content, newFragment5);
            fragmentTransaction5.addToBackStack(String.valueOf(newFragment5));
            fragmentTransaction5.commit();
            break;

        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}
}

activity_newsfeed:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />


</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_newsfeed" />

<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

fragment_newsfeed:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="athena.sentineljs.com.athena.NewsfeedFragment"
tools:showIn="@layout/activity_newsfeed"
>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rv" >
</android.support.v7.widget.RecyclerView>

</android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:1)

默认情况下,

FrameLayout没有背景,因此您应该定义背景。通过这种方式,以前的fragment(或当前fragment后面的视图)将不再可见。

对于您在评论中提到的第二个问题,您可以参考this主题。