FragmentPagerAdapter没有加载最后一个片段?

问题描述 投票:0回答:1

我正在使用FragmentPagerAdapter。行为非常奇怪,并没有加载最后一个片段,也没有在刷卡时重新加载片段。我想要在活动开始时加载所有片段,每次向左或向右滑动viewpager时都要刷新每个片段。

这是我的父片段:

namespace RateLinx.Droid.Fragments
{

    public class ShipmentsFragment : V4App.Fragment, ViewPager.IOnPageChangeListener
    {
        #region Global Used variables
        TabLayout _tabs = null;
        ViewPager _pager = null;
        //bool isFirstTimeLoad = true;//Flag to avoid Creating Fragments many times
        private ActiveShipmentFragment ActiveShipment = null;
        private AwardedShipmentFragment AwardedShipment = null;
        private RecentShipmentFragment RecentShipment = null;
        AppCompatActivity context = null;
        View view = null;
        #endregion


        public ShipmentsFragment(AppCompatActivity context)
        {
            this.context = context;
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            try
            {
                // Use this to return your custom view for this Fragment
                view = inflater.Inflate(Resource.Layout.ShipmentsFragmentLayout, container, false);
                _pager = view.FindViewById<ViewPager>(Resource.Id.pager);
                _tabs = view.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
                BindFragments();
                //if (!string.IsNullOrEmpty(CommanUtil.tokenNo))
                //{
                //    poolingInterval();
                //}
                return view;
            }
            catch
            {
                Toast.MakeText(context, Constants.strErrorOccured, ToastLength.Long).Show();
                context.Finish();
                return null;
            }
        }


        private void BindFragments()
        {
            try
            {
                SetupViewPager(_pager);
                _tabs.SetupWithViewPager(_pager);
                setupTabIcons();
                _pager.AddOnPageChangeListener(this);
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }

        private void SetupViewPager(ViewPager viewPager)
        {
            try
            {
                InitFragment();
                ShipmentDetailedAdapter adapter = new ShipmentDetailedAdapter(ChildFragmentManager);
                adapter.AddFragment(ActiveShipment, Constants.strActive);
                adapter.AddFragment(AwardedShipment, Constants.strAwarded);
                adapter.AddFragment(RecentShipment, Constants.strRecent);
                viewPager.Adapter = adapter;
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }


        private int[] tabIcons = {
            Resource.Drawable.activeiconpng,
            Resource.Drawable.awardedicon,
            Resource.Drawable.upcomingicon,
        };


        private void setupTabIcons()
        {
            try
            {
                _tabs.GetTabAt(0).SetIcon(tabIcons[0]);
                _tabs.GetTabAt(1).SetIcon(tabIcons[1]);
                _tabs.GetTabAt(2).SetIcon(tabIcons[2]);
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }


        private void InitFragment()
        {
            try
            {
                ActiveShipment = new ActiveShipmentFragment(context);// new ActiveShipmentFragment(context);
                AwardedShipment = new AwardedShipmentFragment(context);// new AwardedShipmentFragment(context);
                RecentShipment = new RecentShipmentFragment(context);// new RecentShipmentFragment(context);
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }


        public void OnPageScrollStateChanged(int state)
        {
            //throw new NotImplementedException();
        }


        public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
            try
            {
                //ViewPagerAdapter adapter = new ViewPagerAdapter(context.SupportFragmentManager);
                if (position == 0)
                {
                    _tabs.GetTabAt(0).SetIcon(Resource.Drawable.activehovericonpng);
                    _tabs.GetTabAt(1).SetIcon(Resource.Drawable.awardedicon);
                    _tabs.GetTabAt(2).SetIcon(Resource.Drawable.upcomingicon);
                }
                else if (position == 1)
                {
                    _tabs.GetTabAt(1).SetIcon(Resource.Drawable.awardedhovericon);
                    _tabs.GetTabAt(0).SetIcon(Resource.Drawable.activeiconpng);
                    _tabs.GetTabAt(2).SetIcon(Resource.Drawable.upcomingicon);
                }
                else
                {
                    _tabs.GetTabAt(2).SetIcon(Resource.Drawable.upcominghovericon);
                    _tabs.GetTabAt(0).SetIcon(Resource.Drawable.activeiconpng);
                    _tabs.GetTabAt(1).SetIcon(Resource.Drawable.awardedicon);
                }
                //throw new NotImplementedException();
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }

        public void OnPageSelected(int position)
        {
            //throw new NotImplementedException();
        }
    }

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        style="@style/MyCustomTabLayout"
        android:layout_width="match_parent"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed"
        android:layout_height="50dp" />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

这是我的FragmentPagerAdapter:

namespace RateLinx.Droid.Adapters
{

    public class ShipmentDetailedAdapter : FragmentPagerAdapter
    {

        private List<V4App.Fragment> mFragmentList = new List<V4App.Fragment>();

        private List<string> mFragmentTitleList = new List<string>();


        public ShipmentDetailedAdapter(V4App.FragmentManager manager) : base(manager)
        {
            //base.OnCreate(manager);
        }


        public override int Count
        {
            get
            {
                return mFragmentList.Count;
            }
        }


        public override V4App.Fragment GetItem(int postion)
        {
            return mFragmentList[postion];
        }


        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return new Java.Lang.String(mFragmentTitleList[position].ToLower());// display the title
        }


        public void AddFragment(V4App.Fragment fragment, string title)
        {
            try
            {
                mFragmentList.Add(fragment);
                mFragmentTitleList.Add(title);
            }
            catch
            {
                Console.Write(Constants.strErrorOccured);
            }
        }
    }
}
android android-fragments xamarin.android
1个回答
0
投票

页面滑动时不会重新加载片段。它的默认行为是viewpager存储3个页面的实例。当前页面,当前页面之前和当前页面之后。

如果需要重新加载片段Everytime,则使用framelayout在tabchangelistener方法上添加片段实例。

© www.soinside.com 2019 - 2024. All rights reserved.