我一直在开发一个使用导航抽屉和View寻呼机的应用。我正面临着让它正常工作的问题。 问题:当我启动应用程序时,摘要选项从导航抽屉和Viewpager中选择,在UI上显示3个页面。这里一切正常。当我从导航抽屉中选择其他选项并再次返回“摘要”选项时,视图寻呼机中的视图会丢失。
CODE:
为了从导航抽屉中选择不同的选项,我写了以下代码:
@Override
public void onNavigationDrawerItemSelected(int position) {
Log.d("DataAnalyticActivity", "onNavigationDrawerItemSelected ");
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new Fragment();
switch (position) {
case 0:
fragment = SummaryFragment.newInstance();
break;
case 1:
fragment = SettingsFragment.newInstance();
break;
case 2:
fragment = HelpFragment.newInstance();
break;
}
// This code is for handling orientation change
// After orientation change it will check for existing fragment and
// if it of same class of previous orientation it will use it.
if (!mIsUserInitiatedNavItemSelection)
{
mIsUserInitiatedNavItemSelection = true;
Fragment existingFragment = getFragmentManager().findFragmentById(R.id.container);
Log.d("DataAnalyticActivity", "inisde " + existingFragment);
if (existingFragment != null && existingFragment.getClass().equals(fragment.getClass()))
{
return ;
}
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
}
SummaryFragment.java文件
public class SummaryFragment extends Fragment {
SummaryPagerAdapter mSummaryPagerAdapter;
static SummaryFragment summaryFrag ;
ViewPager mViewPager;
public static SummaryFragment newInstance() {
if (summaryFrag == null) {
summaryFrag = new SummaryFragment();
}
return summaryFrag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("SummaryFragment", "onCreate " + savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("SummaryFragment", "onCreateView " + savedInstanceState);
View v = inflater.inflate(R.layout.activity_summary, container, false);
mSummaryPagerAdapter = new SummaryPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) v.findViewById(R.id.pager);
mViewPager.setAdapter(mSummaryPagerAdapter);
return v;
}
}
SummaryPagerAdapter.java文件
public class SummaryPagerAdapter extends FragmentPagerAdapter {
DailyFragment dailyFragment;
WeeklyFragment weeklyFragment;
MonthlyFragment monthlyFragment;
public SummaryPagerAdapter(FragmentManager fm) {
super(fm);
Log.d("SummaryPager", "SummaryPagerAdapter");
dailyFragment = DailyFragment.newInstance();
weeklyFragment = WeeklyFragment.newInstance();
monthlyFragment = MonthlyFragment.newInstance();
}
@Override
public Fragment getItem(int position) {
Log.d("SummaryPager", "getItem");
switch (position) {
case 0:
return dailyFragment;
case 1:
return weeklyFragment;
case 2:
return monthlyFragment;
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Daily";
case 1:
return "Weekly";
case 2:
return "Monthly";
}
return null;
}
}
DailyFragment,WeeklyFragment和MonthlyFragment类似于下面描述的DailyFragment。他们内部有不同的观点,这只是差异。 DailyFragment.java
public class DailyFragment extends Fragment implements HttpCallTask.OnHttpCallTaskListener {
Button dateButton;
ListView list;
static DailyFragment dailyfaragment;
public DailyFragment() {
}
public static DailyFragment newInstance() {
if (dailyfaragment == null) {
Log.d("DailyFragment", "newInstance");
dailyfaragment = new DailyFragment();
}
return dailyfaragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("DailyFragment", "onCreate " + savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("DailyFragment", "onCreateView " + savedInstanceState + " check " + check);
View rootView = inflater.inflate(R.layout.fragment_daily,
container, false);
list = (ListView) rootView.findViewById(R.id.list);
dateButton = (Button) rootView.findViewById(R.id.button_date);
return rootView;
}
}
请帮助解决此问题。我花了很多时间研究它。 提前谢谢。
答案 0 :(得分:2)
由于ViewPager
位于另一个片段中,导致嵌套片段',您需要用getChildFragments()
替换适配器的参数,即
mSummaryPagerAdapter = new SummaryPagerAdapter(getChildFragmentManager());
getChildFragmentManager()
仅适用于API leval> = 17,但存在于支持库中。