I couldn't use context in FragmentPagerAdapter
. RadioGroup(this)
gives me undefined error. I used getContext()
instead of this but couldn't achieve it
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
final RadioGroup rg = new RadioGroup(this); // what can I use instead of "this" ?
}
When using FragmentPagerAdapter the host ViewPager must have a valid ID set. Subclasses only need to implement getItem and getCount to have a working adapter. super. onCreate(savedInstanceState);
You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).
adapter. FragmentStateAdapter instead. Implementation of PagerAdapter that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state. This version of the pager is more useful when there are a large number of pages, working more like a list view.
I am not sure why you are instantiating a RadioGroup in a FragmentPagerAdapter, but anyway you can get the context by modifying the constructor of the class:
private Context context;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm, Context c) {
super(fm);
context = c;
}
Then you can add the context when you create your FragmentPagerAdapter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With