Reputation: 3213
I'm working on an android project and my fragment are not working...
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();
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();
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.
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 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>
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>
Upvotes: 4
Views: 1325
Reputation: 3213
Following Michael's comment, using the support library ('android.support.v4.app.Fragment') fixed my problem.
Upvotes: 2