2013-02-21 58 views
1

当用户单击操作栏中的项目时,我试图向活动添加片段,但无法显示它。这里是不工作我所期望的方式主要代码(这是正在写在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; 
    } 

当我添加此片段与在OnCreate方法的另一个片段我得到了我的期望。我尝试了几种不同的方法来尝试并显示。任何解释我做错了将不胜感激。我已经添加了下面其余的相关代码。

[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; 
    } 
    } 

XML视图:

<?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> 

当我再次尝试以后添加活动的观点从未改变,以显示片段这个作品,如果我加入片段马上,否则。几乎就像我需要告诉重新绘制的活动?任何帮助,将不胜感激。提前致谢。

回答

2

如果我帮助其他人,我找出了问题所在。添加新片段后,我需要Activity自行重新布局。从我在寻找我看到有关调用无效像这样的评论:

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

然而,这是不正确的(至少在我的情况),并没有得到它来调整(或更正确地重新布局)视图。然而,呼叫RequestLayout做了我所需要的和片段开始显示。例如:

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

希望这会有所帮助。