How to replace Fragments of different types?
Asked Answered
L

1

1

Good morning. I'm developing an app in which I have a main layout that extends from activity and inside this one I have one fragment of one data type, in my case FragmentCover (it's a class).

During my app, I push a button and I want to change this fragment layout for another layout that extends from fragment but of different type, called SongList.

My problem is that I have defined this fragment for the class of Cover and when I change I don't have any problem, but when I want to get the views and set to one variable of my class, the funciont songList = (ListView) getView().findViewById(R.id.songList); it's null and it gives me error.

I put it here what I do.

layout

<fragment android:name="es.xxx.ui.FragmentCover"
    android:id="@+id/pruebaa"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/songBar"
    android:layout_below="@id/header"/>

mainclass change

public void onClick(View v) {
if(isCoverFragment){
                FragmentSongList fragmentSongList = new FragmentSongList();
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentSongList);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("LIST");
                transaction.commit();
                isCoverFragment=false;
            }
            else{
                transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pruebaa, fragmentCover);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                transaction.addToBackStack("COVER");
                transaction.commit();
                getSongCover();
                isCoverFragment=true;
            }

The problem is not the change, is when I try to make the findViewById, it's probabbly because it doesn't load the view associated with FragmentSongList.

Laceylach answered 3/9, 2013 at 13:34 Comment(0)
C
2

You need to use a FrameLayout inside your layout file, instead of defining the Fragment in .xml

Into that FrameLayout, you can inflate any Fragment you want. Just inflate the First Fragment you want to be displayed directly in your onCreate(...) method.

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
        </FrameLayout>

This is how to inflate the Fragments programatically into the FrameLayout.

 FragmentSongList fragmentSongList = new FragmentSongList();
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
         transaction.replace(R.id.content_frame, fragmentSongList);
         transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         transaction.addToBackStack("LIST");
         transaction.commit();

You can leave the code inside your onClick() method just the way it is, just change the id to "content_frame". Furthermore, as mentioned above you will have to inflate the first Fragment that should be displayed inside your onCreate(...) method.

Chaqueta answered 3/9, 2013 at 13:39 Comment(5)
So, if I'm not wrong I change the fragment into my main_activity.xml and change it by framelayout and then I inflate one of that classes, isn't it? In that case, it's necessary that my classes extends from fragment?Laceylach
Instead of defining the Fragment in .xml like you did, you use a FrameLayout in .xml - then you inflate / replace in your onCreate method using FragmentManager yes. And yes, your Classes need to extend Fragment, otherwise they cannot be "replaced" using the FragmentManager.Chaqueta
I'll proove and I tell you, but I think it's the key to make it worksLaceylach
Yes, this is definitely correct, I use it in my projects all the time and it works just fine :-)Chaqueta
It works, but I have a question, when I change of layout and comeback to the previous one, my cover has dissapeared. I try to do a setter of views after do the first commit translation, but it gives me error because it said that getView() is nullLaceylach

© 2022 - 2024 — McMap. All rights reserved.