Reagan Gallant Reagan Gallant - 2 months ago 55
C# Question

Reload data on tab selected in a fragment using view pager

I have my fragments inside view page but I would like to reload the data on the tab selected.I tried returning

PositionNone
in the
GetItemPosition
method in my
FragmentPagerAdapter
but it does not work.

I tried adding
notifyDataSetChanged();
on tab selected but it throws nullpointer exception.
I even tried setting the
viewPager.setOffscreenPageLimit

I managed to find the link :
Replace Fragment inside a ViewPager
but it is on java.
I have managed to convert most of the code to c# but gets stuck on the commented code. I am not sure how to call the listener and instantiate the class at the same time? There is a lot of similar question but unfortunately not one in Xamarin Android.

I need to do this in xamarin android. Any help will be greatly appreciated.

This is my FragmentAdapter

public class TabsFragmentPagerAdapter : FragmentPagerAdapter
{
private readonly Android.Support.V4.App.Fragment[] fragments;
static readonly int NUM_ITEMS = 2;
private readonly Android.Support.V4.App.FragmentManager FragmentManager;
private Android.Support.V4.App.Fragment mFragmentAtPos0;
private readonly ICharSequence[] titles;

public TabsFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm, Android.Support.V4.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;

}
public override int Count
{
get
{
return fragments.Length;
}
}

public override int GetItemPosition(Java.Lang.Object objectValue)
{
if (objectValue.GetType() == typeof(startJobFrag) && mFragmentAtPos0.GetType() == typeof(jobCaptureFrag))
{
return PositionNone;
}
return PositionUnchanged;
}


public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}


public override Android.Support.V4.App.Fragment GetItem(int position)
{
if (position == 0)
{
if (mFragmentAtPos0 == null)
{
//mFragmentAtPos0 = new startJobFrag(new FirstPageFragmentListener()
//{
// public void onSwitchToNextFragment()
// {
// mFragmentManager.BeginTransaction().Remove(mFragmentAtPos0).Commit();
// mFragmentAtPos0 = new jobCaptureFrag();
// NotifyDataSetChanged();
// }
//});
}
return mFragmentAtPos0;
}
else
{
return new jobCaptureFrag();
}
}


This is my Actvity

void FnInitTabLayout()
{
var fragments = new Android.Support.V4.App.Fragment[]
{
new frag 1(),
new frag 2(),
new frag 3(),
};

//Tab title array
var titles = CharSequence.ArrayFromStringArray(new[] {
"Page 1",
"Page 2",
"Page 3",
});

var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
//viewpager holding fragment array and tab title text
//viewPager.
viewPager.Adapter = new TabsFragmentPagerAdapter(SupportFragmentManager, fragments, titles);

// Give the TabLayout the ViewPager
tabLayout.SetupWithViewPager(viewPager);

viewPager.PageSelected += (object sender, ViewPager.PageSelectedEventArgs e) =>
{
//FnInitTabLayout();
if(e.Position == 0)
{
//handle tab click/selected

}
};
}

public interface FirstPageFragmentListener
{
void onSwitchToNextFragment();
}


Fragment

public class Fragment1: Android.Support.V4.App.Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your fragment here
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);

View view = inflater.Inflate(Resource.Layout.myview, container, false);

}
}

Answer

I managed to solve it on my own. In My activity I called NotifyDataSetChanged() on Pageselected event

Activity

_pager.PageSelected += (object sender, ViewPager.PageSelectedEventArgs e) =>
        {
            if (e.Position == 1)
            {
                _pager.Adapter.NotifyDataSetChanged();
            }
        };

Then I changed my Adapter from FragmentPagerAdapter to FragmentStatePagerAdapter and override the GetItemPosition.

Adapter

public class TabsFragmentPagerAdapter : FragmentStatePagerAdapter
{



    public override int GetItemPosition(Java.Lang.Object objectValue)
    {
        return PositionNone;
    }
}
Comments