我正在使用FragmentPagerAdapter创建一个包含2个片段的布局,用户可以在它们之间滑动:
mTabsAdapter = new TabsAdapter(this, mPager);
mTabsAdapter.addTab(bar.newTab().setText("Tab 1"), FirstFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Tab 2"), SecondFragment.class, null);
在那之后,我想获得添加的片段来调用它的方法:
FirstFragment firstFragment = (FirstFragment) mTabsAdapter.findFragmentByPosition(pos);
问题是此时标签尚未就绪,findFragmentByPosition
返回null。如果我在Handler().postDelayed(new Runnable(){...
内执行此操作,它会在测试时发挥作用,但我仍然会从某些用户那里得到此错误。
有没有办法知道片段是否准备就绪?
以下是TabsAdapter类:
public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final SherlockFragmentActivity mActivity;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
public void selectTabByPosition(int position){
mViewPager.setCurrentItem(position);
}
public Fragment findFragmentByPosition(int position) {
FragmentPagerAdapter fragmentPagerAdapter = this;
return mActivity.getSupportFragmentManager().findFragmentByTag(
"android:switcher:" + mViewPager.getId() + ":"
+ fragmentPagerAdapter.getItemId(position));
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
//
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//
}
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActivity = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
答案 0 :(得分:0)
MainActivity.java
//片段和ViewPager初始化
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(pageAdapter);
mViewPager.setOnPageChangeListener(MainActivity.this);
MyPageAdapter.java
公共类MyPageAdapter扩展了FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}}