I have an app with a tabbed menu ( ViewPager
) and I'm trying to add a splash screen to it.
The tabs are working as they should, but I can't seem to make SplashFragment
run before the tabs are shown (until a user is authenticated, but that's a different story)
My question is what is wrong with the code below that could cause the fragment not to show?
Some info: There is only 1 FragmentActivity
(MainActivity.java/activity_main.xml), which has a ViewPager
in it.
Here's my activity_main.xml
layout file:
<?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"
/>
Here's my MainActivity
's onCreate()
function:
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);
}
This is just a layout problem. Your ViewPager
is layout_height="match_parent"
, so when it gets measured, it gets the full screen height. Then, when the FrameLayout
gets measured, there's no space left for it, so it gets 0px height. Either hide the ViewPager
while you're showing the splash screen ( GONE
, not INVISIBLE
) or make it layout_height="wrap_content"
and use weights to figure out how big they should be (or don't use a LinearLayout
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.