活动上的重复片段

时间:2016-03-01 20:29:10

标签: android android-fragments

我正在尝试使用newInstance()替换片段:

MainActivity.onCreate()

Fragment telaInicialFragment = TelaInicialFragment.newInstance(usuario);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, telaInicialFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

TelaInicialFragment.newInstance()

Bundle args = new Bundle();
args.putSerializable("usuario", usuario);
TelaInicialFragment fragment = new TelaInicialFragment();
fragment.setArguments(args);
return fragment;

我的 activity_main.xml 由以下内容组成:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.main.MainActivity">

    <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/main_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/main_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabGravity="fill"
            app:tabIndicatorHeight="2dp"/>
    </android.support.design.widget.AppBarLayout>

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

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

在这一行: fragmentTransaction.add(android.R.id.content, telaInicialFragment); android.R.id.content我的片段在MainActivity上显示重复: duplicated_fragment.png

当我更改为FrameLayout的id时(此屏幕的组件树位于异常之后):

fragmentTransaction.add(R.id.framelayout_tela_inicial, telaInicialFragment);

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nextapp.next/com.nextapp.next.activity.main.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c009b (com.nextapp.next:id/framelayout_tela_inicial) for fragment TelaInicialFragment{e034022 #0 id=0x7f0c009b}

fragment_tela_inicial.xml组件树 component_tree.png

如果我使用fragmentTransaction.add()fragmentTransaction.replace(),则无关紧要(在这种情况下)。

欢迎任何想法。

1 个答案:

答案 0 :(得分:0)

您需要为ViewPager使用片段适配器。

创建一个类似于以下内容的子类......

public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for that page
    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
            return FirstFragment.newInstance();
        case 1: 
            return SecondFragment.newInstance();
        default:
            return null;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + position;
    }

}

使用pager.setAdapter(new MyPagerAdapter());

将其设置为寻呼机