问题
I generated a "navigationdrawer app" with android studio, it contains three nav items in drawerfragment, i.e. "Section 1", "Section 2" and "Section 3" with a PlaceHolderFragment attached to each item initially, then I substitute "Section 1" with a Fragment containing a ViewPager.
After the app launched, firstly tap the "Section 1", the behavior of the ViewPager is normally, then tap "Section 2" and return to "Section 1", something wrong happened, the fragments in ViewPager just refuse to display anything.
below is the relevant codes:
MyActivity.java
package com.example.smallqiang.drawerapp;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.TextView;
public class MyActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
if (position == 0) {
fragmentManager.beginTransaction()
.replace(R.id.container, BlankFragment.newInstance())
.commit();
}
else {
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
TextView tv = (TextView) rootView.findViewById(R.id.section_label);
int id = getArguments().getInt(ARG_SECTION_NUMBER);
tv.setText("PlaceHolderFragment with id = " + id);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MyActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
BlankFragments.java
package com.example.smallqiang.drawerapp;
import android.app.FragmentManager;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
ViewPager mViewPager;
PagerTitleStrip mPagerTitleStrip;
SectionsPagerAdapter mSectionPagerAdapter;
public static BlankFragment newInstance() {
BlankFragment fragment = new BlankFragment();
return fragment;
}
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mPagerTitleStrip = (PagerTitleStrip) rootView.findViewById(R.id.pager_title);
mSectionPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager.setAdapter(mSectionPagerAdapter);
return rootView;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return MyActivity.PlaceholderFragment.newInstance(0);
case 1:
return MyActivity.PlaceholderFragment.newInstance(1);
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "First";
case 1:
return "Second";
}
return null;
}
}
}
there is also a NavigationDrawerFragment.java which is generate by Android Studio.
The fragment_blank.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WorkoutFragment">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff"/>
</android.support.v4.view.ViewPager>
来源:https://stackoverflow.com/questions/24653697/viewpager-in-navigationdrawer-failed-to-load-the-fragment-content-when-navigatio