Android片段未被替换

时间:2016-02-19 17:07:38

标签: android android-layout android-fragments

我是碎片的新手,并且导致我的导航栏片段出现问题。我已经设置好了导航栏,当我从导航中选择一个项目时,布局很好地充气但旧的布局没有消失,并且保持坐在上面或下面然后是新的布局。

新布局应该包含工具栏和导航抽屉片段,但不应包含标签及其布局。

任何人都可以解释我的错误吗?

主要活动创建标签式布局&导航项目选择

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

    drawerFragment = (FragmentDrawer) getFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);
}

private void setupTabIcons() {
    int[] tabIcons = {
            R.drawable.ic_tab_list,
            R.drawable.ic_tab_shopping,
            R.drawable.ic_tab_add,
            R.drawable.ic_tab_search
    };

    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    tabLayout.getTabAt(3).setIcon(tabIcons[3]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new PossibleRecipes(), "ONE");
    adapter.addFrag(new ShoppingList(), "TWO");
    adapter.addFrag(new AddRecipe(), "THREE");
    adapter.addFrag(new SearchRecipes(), "FOUR");
    viewPager.setAdapter(adapter);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            //fragment = new HomeFragment();
            //title = getString(R.string.title_home);
            //break;
        case 1:
            fragment = new FavouritesFragment();
            title = getString(R.string.title_favourites);
            break;
        case 2:
            fragment = new HelpFragment();
            title = getString(R.string.title_help);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.drawer_layout, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

主要Activty.XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout2">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

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


<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.rory.pocketchef.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

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

其中一个导航抽屉XML(HELP.XML)

<RelativeLayout 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:orientation="vertical"
tools:context="info.androidhive.materialdesign.activity.HomeFragment">


<TextView
    android:id="@+id/label"
    android:layout_alignParentTop="true"
    android:layout_marginTop="100dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="45dp"
    android:text="HOME"
    android:textStyle="bold"/>

<TextView
    android:layout_below="@id/label"
    android:layout_centerInParent="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    android:text="Edit fragment_home.xml to change the appearance" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

OnDrawerItemSelected中设置fragment = new FavouritesFragment();fragment = new HelpFragment();。所以你应该这样做;

if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.container, fragment); //Here was the error. container is a FrameLayout
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }

请注意,此行fragmentTransaction.replace(R.id.drawer_layout, new FavouritesFragment());已被修改。

另请注意,container_body的高度为0dp,而layout_weightFrameLayout {{}}}中无效CoordinatorLayout ... { {1}}也设置为ViewPager

我不理解你的UI中fill_parent的目的......它应该做什么?应该在哪里出现?