I have a ViewPager with some Fragments inside. When creating each Fragment I call constructor where I pass name of this Fragment:
public KwejkFragment(String tagName)
{
this.fragmentTag = tagName;
}
In MainActivity ofcourse I create Adapter and ViewPager instance. Then I set listener of ViewPager:
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
FragmentManager fm = getSupportFragmentManager();
Fragment fr = mAdapter.getItem(position);
if(fr.equals(KwejkFragment.class))
{
(KwejkFragment) fr.refresh(); //I'm trying to call method here for example showMeSomething() but it is unavailable.
}
}
And here I'd like to:
1)Get current visible Fragment in PagerView
2)Call specified method of this Fragment
How to do this?
First make the method in the fragment to public and then use the following code
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
FragmentManager fm = getSupportFragmentManager();
Fragment fr = mAdapter.getItem(position);
if(fr instanceOf KwejkFragment))
{
fr.refresh();
}
}
Put the debugger in the refresh method and check whether it is getting called or not.
This code will work. Enjoy :)
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