-
-
Save armcha/d49c4533a464b45bd37174e55e0102ed to your computer and use it in GitHub Desktop.
ViewPager with getActiveFragment for position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yapert.view.viewpager; | |
import android.content.Context; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import com.google.common.base.Charsets; | |
import java.io.ByteArrayOutputStream; | |
import java.io.PrintWriter; | |
public class SmartViewPager extends ViewPager { | |
@SuppressWarnings("UnusedDeclaration") | |
public SmartViewPager(Context context) { | |
super(context); | |
} | |
@SuppressWarnings("UnusedDeclaration") | |
public SmartViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public Fragment getActiveFragment(FragmentManager fragmentManager, int position) { | |
final String name = makeFragmentName(getId(), position); | |
final Fragment fragmentByTag = fragmentManager.findFragmentByTag(name); | |
if (fragmentByTag == null) { | |
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
fragmentManager.dump("", null, new PrintWriter(outputStream, true), null); | |
final String s = new String(outputStream.toByteArray(), Charsets.UTF_8); | |
throw new IllegalStateException("Could not find fragment via hacky way.\n" + | |
"We were looking for position: " + position + " name: " + name + "\n" + | |
"Fragment at this position does not exists, or hack stopped working.\n" + | |
"Current fragment manager dump is: " + s); | |
} | |
return fragmentByTag; | |
} | |
private static String makeFragmentName(int viewId, int index) { | |
return "android:switcher:" + viewId + ":" + index; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment