我有以下......
布局
<?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)
看起来可能是竞争条件?
ActionBar with ViewPager - clicking partially visible tab crashes the app
更新
如果我删除了setPrimaryItem(g,1,v);然后它似乎运行正常,直到我去另一个标签然后回来。然后它以同样的方式失败。这让我相信我做得有些正确,只是遗漏了一些东西。
答案 0 :(得分:0)
这是功能版本,我不得不改变多个方面。我仍然想加入BackStack(如果你能做到这一点你会得到支票),但这应该可以帮助同一条船上的任何人。
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener {
private ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private List<Fragment> fragments = new ArrayList<Fragment>();
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
fragments.add(new ScoreFragment());
fragments.add(new TeamFragment());
this.addTab(actionBar.newTab().setText("Score"), ScoreFragment.class, null);
this.addTab(actionBar.newTab().setText("Team"), TeamFragment.class, null);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.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) {}
@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) {}
/**
* @param f the f to set
*/
public void setF(Fragment f) {
int loc = actionBar.getSelectedTab().getPosition();
fragments.set(loc, f);
notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
if(object instanceof Fragment){
if(fragments.contains(object))
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
}
答案 1 :(得分:0)
尝试使用转换片段来替换,添加或删除片段有时可能不会发出单词并且它只显示空白布局。出现这个问题是因为某些原因。我有问题,我想出来了。这就是我在fragmentActivity中实现onCreate
方法的方式。
就是这样:
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mybooks);
}
当我将android.os.Bundle
更改为Bundle
时,问题就消失了。我希望这可以帮助任何有同样问题的人。