2013-02-21 62 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(); 

希望這會有所幫助。