4

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>
Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • Add the code for the view that gives error. Also don't call addToBackStack() if you don't want to keep the fragment being replaced. – Want2bExpert Sep 09 '15 at 04:38
  • I want to be able to go back to my last fragment while pressing the return button. I'll add the xml. – Virthuss Sep 09 '15 at 04:44
  • u code to replace seems fine..there is error some where else..may be ur xml..could u paste some more code...and the screenshot of your output – Neha Tyagi Sep 09 '15 at 04:45
  • I put the xml code on the main post, I let you check if you find something strange there – Virthuss Sep 09 '15 at 05:37
  • So is there anything in the placecore fragment? Because what you posted shows only layouts. So maybe the fragment is there, but simply empty and transparent. – JDenais Sep 09 '15 at 05:46
  • there is dozens of buttons and textviews in it. I hide them to make it more clear. I ll precise that in the post. – Virthuss Sep 09 '15 at 05:48
  • You may want to try getSupportFragmentManager() and make sure your Fragments are created using 'android.support.v4.app.Fragment'. I've never had luck using native Fragments. I always use the support library. – mdbox Sep 09 '15 at 06:17
  • I'm targeting API 15 and compile with API 23. Is it some risk to use 'android.support.v4.app.Fragment'? I succeed to fix a part of the problem adding the second fragment in his parent and then adding a background but this is workaround... – Virthuss Sep 09 '15 at 06:45
  • There is no risk using the support library and common practice. This questions does a good job explaining it http://stackoverflow.com/questions/17295497/fragment-or-support-fragment – mdbox Sep 09 '15 at 06:50
  • I'm gonna change to the support version then. Thanks a lot for your link, I should have do that sooner. Hope it will help be problem I encounter related to fragments as well (see this link for example: http://stackoverflow.com/questions/32430331/onclick-never-called-in-a-fragment-android) – Virthuss Sep 09 '15 at 06:54

1 Answers1

2

Following Michael's comment, using the support library ('android.support.v4.app.Fragment') fixed my problem.

Virthuss
  • 3,142
  • 1
  • 21
  • 39