3

I'm trying to create a Fragment composed of multiple other fragments. First I have one fragment covering the entire layout, which you could swipe left or right to go the the other fragments using a PagerAdapter. Each of these Fragments will be composed of other fragments. At the moment it is composed of one textView, one Button and one Fragment (will be more repeats of this same fragment with small adjustments).

On each fullscreen-fragment I show which page I'm on by appending it to the TextView. This works.

I want to add the smaller Fragment dynamically. This doesn't work. It works on the first page that gets loaded, and it seems to be working when you go back to the previous page (only the exact previous) even when it didn't show the square when the page was first opened. This behaviour is demonstrated here: enter image description here

You can see the text displays the fragment number. But the blue square should be on every one of the fragments. What could be wrong here?

CreateView() method of the fragment:

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

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

    Bundle bundle=getArguments();
    levelNumber=bundle.getInt("levelNumber");

    textView = (TextView) view.findViewById(R.id.text);
    textView.append(" " + levelNumber.toString());

    String[] colors = {"#0000ff", "#00ff00", "#ff0000", "#ffff00"};

    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.include1, PictureFragment.newInstance(1, "#0000ff" ));
    ft.commit();

    return view;
}

Fragment layout xml

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, I am a TextView" />

    <FrameLayout
        android:id="@+id/include1"
        android:transitionName="pic1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transitionName="pos1"
        android:text="Hello, I am a Button"
        android:onClick="sendMessage"/>

EDIT: this behaviour is only when setOffscreenPageLimit(0). If I set it higher only the first opened page has the blue square, all the others never have it. To me it seems like the getResources().getIdentifier() method always returns the object from the first page? Could this be true?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
user10088
  • 61
  • 3
  • 1
    Love the animated gif along with the textual description to show exactly what the prob is. More questions should be like this with both words and visualization. – Code-Apprentice Sep 24 '16 at 15:48
  • 1
    One small thing that would help is to wrap code snippets in a class and method just like you would when writing the code. – Code-Apprentice Sep 24 '16 at 15:50
  • 1
    Why do you have a for loop which only iterated once? – Code-Apprentice Sep 24 '16 at 15:53
  • 1
    "To me it seems like the getResources().getIdentifier() method always returns the object from the first page?" I think this is the right idea. However, the issue is with `ft.replace()` instead. It likely replaces the first view it finds with the given id. – Code-Apprentice Sep 24 '16 at 15:57
  • Thank you! I've modified the code a bit. For loop will be needed later but is iterated only once at the moment to try to isolate the problem. It has been removed from the post now to prevent confusion. – user10088 Sep 24 '16 at 15:58
  • I've solved the problem! Thanks Code-Apprentice! I'll udate with an answer in a moment. – user10088 Sep 24 '16 at 16:00

1 Answers1

1

The problem was that ft.replace() replaces the first view it finds with the given id, as suggested by Code-Apprentice i the comment on the original question.

Changing

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();

to

FragmentTransaction ft = getChildFragmentManager().beginTransaction();

Seems to have fixed the problem!

Solution from: ViewPager with multiple fragment instances of same class

Community
  • 1
  • 1
user10088
  • 61
  • 3