我有以下...
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
适配器代码...
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener {
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private Fragment f;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
this.addTab(actionBar.newTab().setText("Score"), ScoreFragment.class, null);
f = new TeamFragment();
this.addTab(actionBar.newTab().setText("Team"), TeamFragment.class, null);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
TabInfo info = mTabs.get(position);
return Fragment.instantiate(context, info.clss.getName(), info.args);
}
else{
return f;
}
}
@Override
public int getCount() {
return mTabs.size();
}
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);
actionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onTabReselected(Tab arg0,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction arg1) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab arg0,
android.app.FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
/**
* @return the f
*/
public Fragment getF() {
return f;
}
/**
* @param f the f to set
*/
public void setF(Fragment f) {
startUpdate((ViewGroup)findViewById(R.id.pager));
destroyItem((ViewGroup)findViewById(R.id.pager), 1, this.f);
this.f = f;
instantiateItem((ViewGroup)findViewById(R.id.pager), 1);
finishUpdate((ViewGroup)findViewById(R.id.pager));
notifyDataSetChanged();
}
}
但是,当我使用新片段调用 setF 时,旧片段会按预期消失,但新片段永远不会出现。有人可以帮我解决我做错的事情吗?
我试着改成...
public void setF(Fragment f) {
ViewGroup g = (ViewGroup)findViewById(R.id.pager);
startUpdate(g);
destroyItem(g, 1, this.f);
this.f = f;
WebViewFragment v = (WebViewFragment)instantiateItem(g, 1);
FragmentManager fm = v.getFragmentManager();
finishUpdate(g);
Fragment f3 = v;
f3.setUserVisibleHint(true);
setPrimaryItem(g,1,v);
notifyDataSetChanged();
}
但现在我得到...
10-24 17:10:12.290: E/AndroidRuntime(1113): java.lang.NullPointerException
10-24 17:10:12.290: E/AndroidRuntime(1113): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819)
10-24 17:10:12.290: E/AndroidRuntime(1113): at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:152)
10-24 17:10:12.290: E/AndroidRuntime(1113): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
看起来可能是比赛条件?
带有 ViewPager 的 ActionBar - 单击部分可见的选项卡会使应用程序崩溃
更新
如果我删除 setPrimaryItem(g,1,v); 然后它似乎运行良好,直到我转到另一个选项卡并回来。然后它以同样的方式失败。这让我相信我做的有点正确,只是错过了一些东西。