Code example for ViewPager

Methods: getAdapter, getCurrentItem

0
public class ViewPagerUtil { 
 
    private static final String TAG = "ViewPagerUtil";
 
    public static Fragment currentFragmentFor(ViewPager viewPager) {
        return (Fragment) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem());
    } 
 
    public static Fragment onSearchRequestedForCurrentFragment(ViewPager viewPager) {
        Fragment fragment = currentFragmentFor(viewPager);
        Log.d(TAG, "want to invoke onSearchRequested for current fragment " + fragment);
        if (fragment instanceof OnSearchRequestedListener) {
            ((OnSearchRequestedListener) fragment).onSearchRequested();
        } 
        return fragment;
    } 
}