我有一个带有选项卡式菜单 ( ViewPager
) 的应用程序,我正在尝试向其添加启动画面。
选项卡按应有的方式工作,但在显示选项卡之前我似乎无法SplashFragment
运行(直到用户通过身份验证,但那是另一回事)
我的问题是下面的代码有什么问题可能导致片段不显示?
一些信息:只有 1 个FragmentActivity
(MainActivity.java/activity_main.xml),其中有一个ViewPager
。
这是我的activity_main.xml
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
这是我MainActivity
的onCreate()
功能:
protected void onCreate(Bundle savedInstanceState) {
final ActionBar actionBar = getActionBar();
super.onCreate(savedInstanceState);
//uiHelper = new UiLifecycleHelper(this, callback);
//uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/***********************************/
/****** SETTING FRAGMENT HERE ******/
/***********************************/
FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();
if (null == fragMgr.findFragmentByTag("splashScreen")) {
xact.add(R.id.frag_container, new SplashFragment(), "splashScreen").commit();
}
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,SelectionFragment.class.getName()));
fragments.add(Fragment.instantiate(this,SplashFragment.class.getName()));
fragments.add(Fragment.instantiate(this,SelectionFragment.class.getName()));
mAdapter = new fragAdapter(getSupportFragmentManager(), fragments);
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
});
mPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
};
for(int i = 0; i < fragments.size(); i++) {
actionBar.addTab(actionBar.newTab().setText("Tab " + (i+1)).setTabListener(tabListener));
}
mPager.setCurrentItem(1);
}