片段不会替换并隐藏另一个片段 [英] Fragment doesn't replace and hide another fragment
问题描述
我正在处理一个android项目,而我的片段不起作用...
I'm working on an android project and my fragment are not working...
我正在尝试使用这种UI-> http://developer.android.com/training/basics/fragments/fragment-ui.html
I'm trying to have this kind of UI -> http://developer.android.com/training/basics/fragments/fragment-ui.html
但是我的第二个片段不会替换或隐藏第一个片段,它只是在下面!
However my second fragment doesn't replace or hide the first one, it's simply going under it!
这是我在主要活动中声明我的第一个片段的方式:
This is how I declare my first fragment in the main activity:
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中,我添加了一些内容:
In the onCreateView of this one, I'm adding some content:
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);
}
这就是我声明第二个的方式:
and that's the way I declare the second one:
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上,我正在夸大其他布局文件中包含的布局:
On the onCreateView, I'm inflating with a layout contained in a other layout file:
@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;
}
尝试使用添加而不是替换,或使用隐藏不起作用.
Trying to use add instead of replace, or using hide doesn't work.
我希望我的两个片段使用相同的布局模板,但是每个片段都有一个唯一的实例,所以我不知道这是否可行.我尝试在第二个布局文件中使用其他布局文件,但随后总是出现一个错误,提示未找到ID的视图...
I want my two fragments to use the same layout template, but with an unique instance of it for each fragment, I don't know if it's possible or not. I tried to use a different layout file for the second one but then I had always an error saying No view found for id...
有人知道如何解决该问题吗?谢谢
Anyone know how to fix that? thanks
xml文件:finallayout.xml
xml files: 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,其中包含第二个片段的内容.它包含许多文本视图和按钮,我将其隐藏起来以使其更清晰(我改用某些内容"):
and placecore.xml, which contains the content of the 2nd fragment. It contains many textviews and buttons I hide to make it more clear(i put "some content" instead):
<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>
推荐答案
按照迈克尔的评论,使用支持库('android.support.v4.app.Fragment')解决了我的问题.
Following Michael's comment, using the support library ('android.support.v4.app.Fragment') fixed my problem.
这篇关于片段不会替换并隐藏另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!