I have a viewpager that contains of pages. Both are basically exactly the same, only one is with different drawables. So all I need to do is change the drawables when page two is selected, and change it back when page one is selected. I got this all to work with ONE layout, but I couldn't find out where to change the drawables. This is my code so fare:
private void initViewPager()
{
ViewPager _viewpager = FindViewById<ViewPager>(Resource.Id.viewpager);
_viewpager.Adapter = new FragmentAdapter(SupportFragmentManager);
}
public class FragmentAdapter : FragmentPagerAdapter
{
public FragmentAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
}
public override int Count
{
get { return 2; }
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return new LayoutFragment();
}
}
public class LayoutFragment : Android.Support.V4.App.Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.Viewpage1, container, false);
return view;
}
}
I'm inflating "Viewpage1". This layout contains my xml that, when page two is selected needs to have changed drawables ( just the image of 6 buttons).
How would I implement that into my code? Thanks.
Follow below steps
1.Create one more fragment like you have done for LayoutFragment
. Say NewLayoutFragment
2.Pass your new layout to the newly created fragment.
public class NewLayoutFragment : Android.Support.V4.App.Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.Viewpage2, container, false); // Viewpage2 is your new layout file with 6 buttons.
return view;
}
}
3.Put a switch
statement in your getItem()
method of your Adapter and call the new Fragment instance in the second case. Like this:
public override Android.Support.V4.App.Fragment GetItem(int position)
{
switch(position){
case 0:
return new LayoutFragment();
case 1:
return new NewLayoutFragment(); // this is your new Fragment with new layout.
}
}
I hope this should work. :) Happy coding.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments