如何在运行时用 ChildFragmentManager 和没有 PagerSlidingTabStrip Xamarin.Android 标题的片段替换片段
Posted
技术标签:
【中文标题】如何在运行时用 ChildFragmentManager 和没有 PagerSlidingTabStrip Xamarin.Android 标题的片段替换片段【英文标题】:How to Replace Fragment at runtime with the ChildFragmentManager and this fragment without Title of PagerSlidingTabStrip Xamarin.Android 【发布时间】:2021-07-21 11:00:36 【问题描述】:我想在运行时使用其他片段更改分页器布局,但没有 PagerSlidingTabStrip 标题。我怎么能做到。 其次如何。我可以在另一个片段中调用方法主页片段。
这是首页布局
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:layout_marginBottom="50dp"
android:id="@+id/SlidingStrips"
android:layout_marginTop="10dp"
android:layout_below="@id/AcountBalance">
<com.refractored.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:pstsPaddingMiddle="true"
app:pstsDividerWidth="1dp"
app:pstsDividerPadding="5dp"
app:pstsDividerColor="#50FFFFFF"
android:textColor="#50FFFFFF"
app:pstsTextColorSelected="@android:color/white"
app:pstsIndicatorColor="@android:color/white"
app:pstsUnderlineColor="@android:color/white" />
<!--Change this to true if you want to center items-->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_
android:layout_weight="1"
tools:context=".HomeFragmentLayout" />
</LinearLayout>
我在这里添加片段的方式是与父片段一起添加的 ChildFragments,这两个片段添加了两个带有小时和日期选项卡的选项卡。
public class HomeFragment : Fragment, IOnTabReselectedListener, ViewPager.IOnPageChangeListener
View view;
private MyPagerAdapter adapter;
private int count = 1;
private int currentColor;
List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
public static ViewPager pager;
private PagerSlidingTabStrip tabs;
public override void OnCreate(Bundle savedInstanceState)
base.OnCreate(savedInstanceState);
// Create your fragment here
public static HomeFragment NewInstance()
var frag1 = new HomeFragment Arguments = new Bundle() ;
return frag1;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
base.OnCreateView(inflater, container, savedInstanceState);
if (view == null)
view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);
return view;
else
return view;
public override void OnViewCreated(View view, Bundle savedInstanceState)
base.OnViewCreated(view, savedInstanceState);
try
fragments.Add(new HourlyFragment());
fragments.Add(new DayFragment());
adapter = new MyPagerAdapter(ChildFragmentManager, fragments);
pager = view.FindViewById<ViewPager>(Resource.Id.pager);
tabs = View.FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
pager.Adapter = adapter;
tabs.SetViewPager(pager);
var pageMargin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 4, Resources.DisplayMetrics);
pager.PageMargin = pageMargin;
pager.CurrentItem = 0;
tabs.OnTabReselectedListener = this;
tabs.OnPageChangeListener = this;
catch (System.Exception ex)
.// public void ReplaceFragment(Fragment fragment)
//
// try
//
// FragmentManager manager = ChildFragmentManager;
// FragmentTransaction transaction = manager.BeginTransaction();
// transaction.Add(Resource.Id.content_frame, fragment);
// transaction.Commit();
//
// catch (System.Exception ex)
//
//
//
public void OnTabReselected(int position)
Toast.MakeText(Context.ApplicationContext, "Tab reselected: " + position, ToastLength.Short).Show();
public void OnPageScrollStateChanged(int state)
Console.WriteLine("Page scroll state changed: " + state);
public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
Console.WriteLine("Page Scrolled");
public void OnPageSelected(int position)
Console.WriteLine("Page selected: " + position);
这是寻呼机适配器
public class MyPagerAdapter : FragmentPagerAdapter
List<Android.Support.V4.App.Fragment> fragments;
private readonly string[] Titles =
"Hour", "Day"
;
public MyPagerAdapter(FragmentManager fm, List<Android.Support.V4.App.Fragment> fragments) : base(fm)
this.fragments = fragments;
public override ICharSequence GetPageTitleFormatted(int position)
return new String(Titles[position]);
//
public override int Count get return fragments.Count;
public override Fragment GetItem(int position)
return fragments[position];
public override long GetItemId(int position)
return base.GetItemId(position);
【问题讨论】:
【参考方案1】:你可以像下面这样,假设你需要调用 HomeFragment 中的替换方法。
在您的 MyPagerAdapter 类中添加以下代码(如何在运行时替换片段):
private bool[] flags;
public override int GetItemPosition(Java.Lang.Object @object)
return PositionNone;
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
if (flags != null && flags[position])
/** Get the cached fragment and replace it with a new fragment*/
Android.Support.V4.App.Fragment fragment = (Android.Support.V4.App.Fragment)base.InstantiateItem(container, position);
string fragmentTag = fragment.Tag;
Android.Support.V4.App.FragmentTransaction ft = fm.BeginTransaction();
ft.Remove(fragment);
fragment = fragments[position];
ft.Add(container.Id, fragment, fragmentTag);
ft.Attach(fragment);
ft.Commit();
/** Set to false when the replacement is complete*/
flags[position] = false;
if (fragment != null)
return fragment;
else
return base.InstantiateItem(container, position);
else
return base.InstantiateItem(container, position);
public void setFragments(List<Android.Support.V4.App.Fragment> fragments)
if (this.fragments != null)
flags = new bool[fragments.Count];
for (int i = 0; i < fragments.Count; i++)
flags[i] = true;
this.fragments = fragments;
NotifyDataSetChanged();
然后是 HomeFragment 中的 replace 方法:
public void ReplaceFragment()
fragments[0] = new NewFragment(); //suppose you want to replace the first HourlyFragment
fragments[1] = new DayFragment();
adapter.setFragments(fragments);
在另一个片段中(例如点击事件),您可以调用 HomeFragment 中的 replace 方法,如:
private void Button_Click(object sender, EventArgs e)
foreach (Android.Support.V4.App.Fragment fragment in Activity.SupportFragmentManager.Fragments)
if (fragment is HomeFragment)
((HomeFragment)fragment).ReplaceFragment();
【讨论】:
以上是关于如何在运行时用 ChildFragmentManager 和没有 PagerSlidingTabStrip Xamarin.Android 标题的片段替换片段的主要内容,如果未能解决你的问题,请参考以下文章
在运行时用 H2 DataSource 替换 DataSource bean
我们可以在容器运行时用 docker-compose 替换图像吗