I'm a new when it comes to Xamarin and I've ran into an issue that i could not resolve no matter what i tried That's the error message I've been getting "cannot implicitly convert type 'Fragment1' to 'andriod.support.V4.App.Fragment
This is my MainActivity
public class MainActivity : AppCompatActivity
{
int count = 1;
PagerSlidingTabStrip _tabs;
ViewPager _pager;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
_pager = FindViewById<ViewPager>(Resource.Id.pager);
_tabs = FindViewById<PagerSlidingTabStrip>(Resource.Id.tabs);
_pager.PageMargin = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, 4, Resources.DisplayMetrics);
_pager.CurrentItem = 0;
string[] titles = new string[]
{
"Fragment1",
"Fragment2"
};
var adapter = new PagerAdapter(SupportFragmentManager, titles);
_pager.Adapter = adapter;
_pager.OffscreenPageLimit = titles.Length;
_tabs.SetViewPager(_pager);
}
}
this is my pagerAdapter Code
public class PagerAdapter:FragmentPagerAdapter
{
private string[] Titles;
public PagerAdapter(FragmentManager fm, string[] titles)
: base(fm)
{
Titles = titles;
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(Titles[position]);
}
public override int Count
{
get { return Titles.Length; }
}
public override Fragment GetItem(int position)
{
switch (position)
{
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
default:
return new Fragment1();
}
}
}
And this is my fragment
public class Fragment1 : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Fragment1Layout, null);
return view;
}
}
Any help is appreciated
I'm not familiar with Xamarin, but I have been dealing with fragments a lot recently and have run into this issue several times. I found that sometimes when I'm initializing the Fragments, it was using
android.app.Fragment
instead of using theandroid.support.v4.app.Fragment
So, wherever you initialize your Fragment(again not familiar with Xamarin), make sure you use
android.support.v4.app.Fragment
. Hope this helps!