2021年4月28日星期三

How to Replace Fragment at runtime with the ChildFragmentManager and this fragment without Title of PagerSlidingTabStrip Xamarin.Android

I want to Change the Pager Layout at runtime with other fragment but without the PagerSlidingTabStrip Title. How .I can make it . Second how .I can call method home Fragment in another fragments.

Here is The Home Layout

     <LinearLayout              android:layout_width="match_parent"              android:layout_height="match_parent"              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_width="match_parent"          android:layout_height="?attr/actionBarSize"          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_width="match_parent"          android:layout_height="0dp"          android:layout_weight="1"          tools:context=".HomeFragmentLayout" />                       </LinearLayout>  

And how i'm Adding fragments here is the ChildFragments that added with the parent fragment these two fragments add two tabs with Hour and day Tabs.

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

Here is the pager Adapter

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);          }           }  
https://stackoverflow.com/questions/67294280/how-to-replace-fragment-at-runtime-with-the-childfragmentmanager-and-this-fragme April 28, 2021 at 01:34PM

没有评论:

发表评论