I have this class for PagerAdapter
:
public class MyPagerAdapter : PagerAdapter
{
private List<string> tab_names = new List<string>();
int[] page = { Resource.Layout.page0, Resource.Layout.page1, Resource.Layout.page2, Resource.Layout.page3, Resource.Layout.page4 };
public Action OnAllPagesAdded = delegate { };
public override Java.Lang.Object InstantiateItem(ViewGroup container, int page_offset)
{
var view = LayoutInflater.From(container.Context).Inflate(page[page_offset], container, false);
container.AddView(view);
if (page_offset == page.Length - 1)
OnAllPagesAdded();
return view;
}
public MyPagerAdapter(List<string> tab_names)
{
this.tab_names = tab_names;
}
public override int Count => tab_names.Count;
public override bool IsViewFromObject(View view, Java.Lang.Object obj) => view == obj;
public string GetTabName(int position) => tab_names[position];
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object obj)
=> container.RemoveView((View)obj);
}
I need an event to be triggers when all pages are instantiated and ready to use. I improvised the OnAllPagesAdded
event which is triggered with:
if (page_offset == page.Length - 1)
OnAllPagesAdded();
triggered when the last page is instantiated. I have set:
view_pager.OffscreenPageLimit = 100;
so all pages will be instantiated from the outset.
Is there a better/safer way to trigger the event OnAllPagesAdded
?
You could add page data in PageScrolled
method of ViewPager , it will detect whehter the last page is shown.
For example:
MyPagerAdapter myPagerAdapter = ...
...
viewPager.PageScrolled += ViewPager_PageScrolled;
private void ViewPager_PageScrolled(object sender, ViewPager.PageScrolledEventArgs e)
{
if(e.Position == page.Length - 1){
// add page data
myPagerAdapter.OnAllPagesAdded();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.