viewpager的第一页根本没有加载

时间:2013-07-22 11:29:21

标签: android android-viewpager fragment

听到我的FragmentActivity,在我运行之后,我的第一个标签是空的。

public class MainActivity extends FragmentActivity implements
    OnTabChangeListener, OnPageChangeListener {


private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;
private TabInfo mLastTab = null;

private class TabInfo {
    private String tag;
    private Class clss;
    private Bundle args;
    private Fragment fragment;

    TabInfo(String tag, Class clazz, Bundle args) {
        this.tag = tag;
        this.clss = clazz;
        this.args = args;
    }

}

class TabFactory implements TabContentFactory {

    private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    Log.d("checking", "setup tabs...");
    setContentView(R.layout.activity_main);
    // //
    initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
    }
    intialiseViewPager();
    // //

}

protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                            // selected
    super.onSaveInstanceState(outState);
}

private void intialiseViewPager() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments
            .add(Fragment.instantiate(this, CoachFragment.class.getName()));
    fragments
            .add(Fragment.instantiate(this, LogingFragment.class.getName()));
    fragments.add(Fragment.instantiate(this,
            HistoryFragment.class.getName()));
    this.mPagerAdapter = new PagerAdapter(
            super.getSupportFragmentManager(), fragments);
    //
    this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(this.mPagerAdapter);
    this.mViewPager.setOnPageChangeListener(this);
}

private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    View tabView1 = createTabView(this, "coach");
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost
            .newTabSpec("Tab1").setIndicator(tabView1),
            (tabInfo = new TabInfo("Tab1", CoachFragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    View tabView2 = createTabView(this, "log book");
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost
            .newTabSpec("Tab2").setIndicator(tabView2),
            (tabInfo = new TabInfo("Tab2", LogingFragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    View tabView3 = createTabView(this, "history");
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost
            .newTabSpec("Tab3").setIndicator(tabView3),
            (tabInfo = new TabInfo("Tab3", HistoryFragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
            mTabHost.setOnTabChangedListener(this);
}

private static void AddTab(MainActivity activity, TabHost tabHost,
        TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}



public void onTabChanged(String tag) {
    // TabInfo newTab = this.mapTabInfo.get(tag);
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context)
            .inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabsText);
    tv.setText(text);
    return view;
}

@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    this.mTabHost.setCurrentTab(position);
}

}

听到的是CoachFragment代码:

public class CoachFragment extends Fragment implements AnimationListener {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }
    return (LinearLayout)inflater.inflate(R.layout.coach_activity, container, false);
}

其他两个片段是相同的,我使用相同的布局文件为所有三个,其他两个没有问题。

1 个答案:

答案 0 :(得分:4)

根据Android Documentation,针对您的案例的最佳方法是实现 FragmentPagerAdapter

你可以这样做:

1 - 创建自己的适配器:

private class YourAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener
{
    Fragment screens[];
    public CheckinHistoryAdapter(FragmentManager fm) {
        super(fm);
        screens = new Fragment[3];
        screens[0] = new CoachFragment();
        screens[1] = new LogingFragment();
        screens[2] = new HistoryFragment();
    }

    @Override
    public Fragment getItem(int index) {
        if(index <= screens.length)
        {
            return screens[index];
        }
        return null;
    }

    @Override
    public int getCount() {
        return screens.length;
    }

}

在您的Activity的 intialiseViewPager 上会是这样的:

private void intialiseViewPager() {

this.mPagerAdapter = new YourAdapter(super.getSupportFragmentManager());
//
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);

}