片段不会替换和隐藏另一个片段

时间:2015-09-09 04:08:30

标签: android android-fragments

我正在开发一个Android项目而且我的片段无效...

我正在尝试使用这种UI - > http://developer.android.com/training/basics/fragments/fragment-ui.html

然而,我的第二个片段不会替换或隐藏第一个片段,它只是在它下面!

这是我在主要活动中声明我的第一个片段的方式:

setContentView(R.layout.finallayout);
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("urllist", URLlist);
    // set Fragmentclass Arguments
    MainFragment main = new MainFragment();
    main.setArguments(bundle);
    getFragmentManager().beginTransaction().add(R.id.scrollcontentcontainer, main,"list").commit();

在这个的onCreateView中,我添加了一些内容:

mainview = inflater.inflate(R.layout.finallayout, container, false);
    scollviewgroup= (ViewGroup) container.findViewById(R.id.scrollcontentcontainer);
    //mainview.setOnClickListener(this);

    for (int i = 0; i < json.size(); i++) {
        lineartab.add(inflater.inflate(R.layout.relativeaqi, scollviewgroup, false));
        createMainLayout(this.json.get(i), i, lineartab.get(i), scollviewgroup, inflater);

    }

这就是我宣布第二个的方式:

 Fragment contentFragment = new CoreFragment();
                FragmentManager fm = getActivity().getFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
               // fragmentTransaction.add(contentFragment, "core");
                fragmentTransaction.replace(R.id.scrollcontentcontainer, contentFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

在onCreateView上,我正在使用其他布局文件中包含的布局进行充气:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //container.removeAllViews();
    //IF I USE THIS ONE I HAVE THE EXPECTED RESULT, HOWEVER I M NOT SURE THATS THE BEST WAY TO DO IT SINCE ITS REMOVING THE CONTENT INCLUDED IN THE OTHER FRAGMENT


    View view = inflater.inflate(R.layout.placecore, container, false);

    return view;
}

尝试使用add而不是replace,或者使用hide不起作用。

我希望我的两个片段使用相同的布局模板,但每个片段都有一个唯一的实例,我不知道它是否可能。我试图为第二个使用不同的布局文件但是我总是出错No view found for id...

任何人都知道如何解决这个问题?感谢

xml文件: finallayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/bottomcontent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:focusable="false">

</RelativeLayout>



    <LinearLayout
        android:id="@+id/scrollcontentcontainer"
        android:focusable="false"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

和placecore.xml,其中包含第二个片段的内容。 它包含许多我隐藏的文本视图和按钮以使其更清晰(我改为使用“一些内容”):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/corecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="20dp"
android:clickable="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/alayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    some content

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wlayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="#1d010101"
    android:orientation="horizontal"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp">

    some content


</LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    some content

    </LinearLayout>


</LinearLayout>

1 个答案:

答案 0 :(得分:2)

根据Michael的评论,使用支持库('android.support.v4.app.Fragment')解决了我的问题。

相关问题