Issue
In short, I'm trying to wrap a fragment inside a wrapper class, and then add the wrapper to the view pager. I'm having trouble with null pointers later on in the running of the app with this (as opposed to inheriting from the fragment, and adding the inherited fragment to the viewpager).
Description
Basically, I'm adding fragments to a viewpager, and before adding the fragment, I've been inheriting from it, and wrapping it in another view that adds a boarder around it (page splits).
Each page/fragment is a distinct class that represents the view to be displayed.
The problem with this approach is that I need to extend each fragment and add the same boilerplate functionality, which isn't ideal. I also don't want to make a baseclass, say "boardered-fragment" because then the fragments then be aware of the boarder when they derive from it (i think).
So, I figured that maybe I would just use composition, where I would make a generic wrapper class, that would contain a fragment data member typed as T. The problem with this is that I can't seem to get the current activities "activity" reference into the context of the fragment data member (composed in the wrapper).
I did some ad-hoc tweaking and hid the getActivity() that returns a copy of the activity reference I manually store. However the program crash's randomly (as I suspected it would). I've tried overriding all possible lifetime methods in my wrapper, and then inside would call that lifetime method on the data-member fragment
public override void OnSomething()
{
base.OnSomething()
mFragment.OnSomething()
}
but that doesn't seem to help. I'm guessing that because the composed fragment is "hidden" from the current activities context, that I'm going to need to do all necessary operation manually. I'm just not sure what they all are ?
Here is a quick code example of what I'm trying to accomplish, and the elaboration is below. Please let me know if more code is needed, and I will adapt what I have into an example format.
Composition (having many issues/null pointers throughout app)
public class Frag1 : Fragment
{
...
}
public class Activity1 : Activity
{
public override void OnCreate(Bundle bundle)
{
...
List<Fragment> fragments = new List<Fragment>();
fragments.Add(FragmentWrapper.NewInstance(Activity, typeof(FragmentWrapper))
MyAdapter pagerAdapter = new MyAdapter(FragmentManager, fragments);
ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);
viewPager.Adapter = pagerAdapter;
}
public class FragmentWrapper
{
Fragment mFragment; // How to successfully wrap a fragment, but still behave properly ????
public static FragmentWrapper NewInstance(Activity activity, string type)
{
FragmentWrapper thisFrag = new FragmentWrapper();
...
thisFrag.mFragment = Fragment.Instantiate(activity, type);
return thisFrag;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
// add boarder around mFragment and return new view
}
}
public class MyAdapter : Android.Support.V13.App.FragmentStatePagerAdapter
{
....
}
}
Inheritance (can do, ok)
public class Frag1 : Fragment
{
...
}
public class Activity1 : Activity
{
public override void OnCreate(Bundle bundle)
{
...
List<Fragment> fragments = new List<Fragment>();
fragments.Add(FragmentExtender.NewInstance(Activity, typeof(FragmentExtender))
MyAdapter pagerAdapter = new MyAdapter(FragmentManager, fragments);
ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);
viewPager.Adapter = pagerAdapter;
}
public class FragmentExtender : Frag1
{
public static FragmentExtender NewInstance()
{
FragmentExtender thisFrag = new FragmentExtender();
...
return thisFrag;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
// add boarder around mFragment and return new view
}
}
public class MyAdapter : Android.Support.V13.App.FragmentStatePagerAdapter
{
....
}
}
Any help with this is much appreciated. Thanks.
Solution
If all you want is add a border between the fragments then you should check out ViewPager.setPageMargin()
and ViewPager.setPageMarginDrawable()
.
Answered By - patheticpat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.