我有一个显示卡片视图的片段,类似仪表板。每个卡片视图都应该替换仪表板片段,并在单击时调用自己的片段,但我似乎无法做到这一点。 我尝试了以下方法:
imageId.Click += delegate {
((FragmentActivity)Activity).ShowFragment(SmokeSensor);
};
活动中的方法:
public void ShowFragment(SupportFragment fragment) {
var transaction = SupportFragmentManager.BeginTransaction();
connectionStatus.Visibility = ViewStates.Gone;
if (!ConnectionDetector.IsConnected(this)) {
connectionStatus.Visibility = ViewStates.Visible;
transaction.Detach(currentFragment);
transaction.Commit();
drawerLayout.CloseDrawer(leftDrawer);
return;
}
if (fragment == currentFragment) {
transaction.Detach(currentFragment);
transaction.Attach(currentFragment);
transaction.Commit();
drawerLayout.CloseDrawer(leftDrawer);
return;
}
transaction.SetCustomAnimations(Resource.Animation.slide_in, Resource.Animation.slide_out);
transaction.Replace(Resource.Id.fragment_container, fragment);
transaction.Commit();
drawerLayout.CloseDrawer(leftDrawer);
currentFragment = fragment;
}
答案 0 :(得分:1)
您可以在主活动中创建一个公共方法,并调用该方法来替换或加载当前片段中事件的片段。
答案 1 :(得分:0)
对于嵌套片段,请在根片段上使用子片段管理器。