Xamarin Android:如果背景设置为只显示第一个片段
我有一个抽屉布局,抽屉里有两个菜单项。如果我加载应用程序,它将正确加载第一个片段。但是如果我打开抽屉并选择另一个菜单点,它不会改变片段。真正奇怪的是,如果我去掉背景色,它会工作,但有时会重叠 有人能告诉我我做错了什么吗 在抽屉里打电话:Xamarin Android:如果背景设置为只显示第一个片段,android,android-layout,android-fragments,xamarin,Android,Android Layout,Android Fragments,Xamarin,我有一个抽屉布局,抽屉里有两个菜单项。如果我加载应用程序,它将正确加载第一个片段。但是如果我打开抽屉并选择另一个菜单点,它不会改变片段。真正奇怪的是,如果我去掉背景色,它会工作,但有时会重叠 有人能告诉我我做错了什么吗 在抽屉里打电话: _list.Adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1, itemList); _list
_list.Adapter = new ArrayAdapter<string> (this, Android.Resource.Layout.SimpleListItem1, itemList);
_list.ItemClick += (sender, e) => {
_title = itemList[e.Position];
var fragmentTransaction = FragmentManager.BeginTransaction ();
switch(e.Position)
{
case 0:
fragmentTransaction.Replace (Resource.Id.content_pane, new MedicineListFragment());
break;
case 1:
fragmentTransaction.Replace (Resource.Id.content_pane, new HistoryListFragment());
break;
};
fragmentTransaction.Commit ();
_slidingLayout.SmoothSlideClosed ();
};
第一段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/medicine_list_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?android:windowBackground">
<ListView
android:id="@+id/medicine_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/history_list_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?android:windowBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="foooo2" />
第二段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/medicine_list_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?android:windowBackground">
<ListView
android:id="@+id/medicine_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/history_list_fragment"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="?android:windowBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="foooo2" />
主要布局:
有趣的是,如果我调试正确的片段出现,那么屏幕就会闪烁,旧的片段再次出现。我发现了问题。我有这样的想法:
public void FirstLayoutListener(object sender, EventArgs e){
if(_slidingLayout.CanSlide () && !_slidingLayout.IsOpen){
ActionBar.SetDisplayHomeAsUpEnabled (true);
ActionBar.SetHomeButtonEnabled (true);
ActionBar.Title = _title;
} else {
ActionBar.SetDisplayHomeAsUpEnabled (false);
ActionBar.SetHomeButtonEnabled (false);
ActionBar.Title = _drawerTitle;
}
var fragmenTransaction = FragmentManager.BeginTransaction ();
fragmenTransaction.Add (Resource.Id.content_pane, new MedicineListFragment());
fragmenTransaction.Commit ();
_slidingLayout.ViewTreeObserver.GlobalLayout -= FirstLayoutListener;
}
我以为这个方法只在第一次加载时被调用,但不知怎的,它是在片段的每次刷新/加载时被调用的。因此,它总是重新生成第一个碎片
问候
恩帕德鲁特