DavidR
DavidR

Reputation: 6952

Adding fragment to activity after initial layout set not showing

When a user clicks on an item in the action bar I am trying to add a fragment to the activity, however I can not get it to show. Here is the main code that is not working the way I expect (This is being written in Monodroid):

public override bool OnOptionsItemSelected(IMenuItem item)
    {
      if (item.ItemId == Resource.Id.show_hide_notes)
      {
        notesLayout.Visibility = ViewStates.Visible;

        notesWebViewFragment = new NotesWebViewFragment();
        // linearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 1);

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.notes_fragment_container, notesWebViewFragment);
        fragmentTransaction.Commit();

        // Adding this line doesn't help
        // FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();
      }
      return true;
    }

When I add this fragment with the other fragment in the OnCreate method I get what I expect. I have tried a few different ways to try and get this to show. Any explanation what I am doing wrong will greatly be appreciated. I have added the rest of the relevent code below.

 [Activity]
  public class BrainViewActivity : Android.Support.V4.App.FragmentActivity
  {
    private Brain activeBrain;

    PlexWebViewFragment plexWebViewFragment;
    NotesWebViewFragment notesWebViewFragment;

    FrameLayout notesLayout;

    protected override void OnCreate(Bundle savedInstanceState)
    {
      base.OnCreate(savedInstanceState);

      SetContentView(Resource.Layout.brain_view);

      notesLayout = FindViewById<FrameLayout>(Resource.Id.notes_fragment_container);
      // notesLinearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 0);
      notesLayout.Visibility = ViewStates.Gone;

      if (savedInstanceState == null)
      {
        // Create an instance of PlexWebViewFragment
        plexWebViewFragment = new PlexWebViewFragment();
      }

      // Check that the activity is using the layout version with
      // the fragment_container FrameLayout
      if (FindViewById(Resource.Id.plex_fragment_container) != null)
      {
        plexWebViewFragment.Arguments = Intent.Extras;

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.plex_fragment_container, plexWebViewFragment);

        fragmentTransaction.Commit();
      }
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
      base.OnCreateOptionsMenu(menu);
      MenuInflater.Inflate(Resource.Menu.brain_view_menu, menu);

      return true;
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
      if (item.ItemId == Resource.Id.show_hide_notes)
      {
        notesLayout.Visibility = ViewStates.Visible;

        notesWebViewFragment = new NotesWebViewFragment();
        // linearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 1);

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.notes_fragment_container, notesWebViewFragment);
        fragmentTransaction.Commit();

        // Adding this line doesn't help
        // FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();
      }
      return true;
    }
  }

The xml view:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/plex_frame_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <FrameLayout
    android:id="@+id/plex_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

  <FrameLayout
    android:id="@+id/notes_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

Again this works if I add the fragment right away, otherwise when I try and add it later the view of the activity never changes to show the fragment. Almost like I need to tell the activity to repaint? Any help would be appreciated. Thanks in advance.

Upvotes: 1

Views: 1812

Answers (1)

DavidR
DavidR

Reputation: 6952

In case this my help anyone else out, I figured out what the problem was. After adding the new fragment I needed to get the Activity to relayout itself. From what I was searching I was seeing comments about calling invalidate like this:

FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();

However, that is not correct (at least in my case) and does not get it to resize (or more correctly relayout) the view. However, calling RequestLayout did exactly what I needed and the fragment started to show. Example:

FindViewById<LinearLayout>(Resource.Id.plex_frame_container).RequestLayout();

Hope this helps.

Upvotes: 2

Related Questions