I'm using the FragmentTabsPager class from
http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html
in one of my apps. The FragmentTabsPager contains 3 Fragments. Within my layout, I have a list on the left side which I want to send a message (onListItemClick) to the current Fragment within the FragmentTabsPager. I'm having serious difficulty getting the current Fragment instance within the pager.
Sorry, I know this is vague but if anyone has an idea that would be great.
The mechanics of passing the message from the List fragment is fine, I can get the message into my FragmentTabsPage开发者_如何学Pythonr class no problem. It's getting the message into the current Fragment within the TabHost that's causing me stress.
I FOUND THE ANSWER FINALLY!
To pass a message to the internal fragments, you simple invoke a public function within it. The tricky part comes when looking for the correct fragment!
So, to first get the fragment, add this within the CustomFragmentPagerAdapter :
public static class TabsAdapter extends FragmentPagerAdapter implements .. {
..
public Fragment findFragment(int position) {
String name = "android:switcher:" + mViewPager.getId() + ":" + position;
FragmentManager fm = ((FragmentActivity) mContext).getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag(name);
if (fragment == null) {
fragment = getItem(position);
}
return fragment;
}
Then access the fragment from the main activity like so
CustomFragment fragment = mTabsAdapter.findFragment(1);
if(fragment != null)
fragment.customFunction(args); //<-- your custom function
Please do note that, I'm actually using ActionBarSherlock for all the fragments classes. But this is will still able to solve your problem.
精彩评论