我有一个片段 AroundMeFragment,它有一个带有两个片段的视图寻呼机MapFragment
和ListFragment
. 当我第一次进入这个片段时一切正常,但是当我单击一个项目进入新片段中的详细视图并返回到 AroundMeFragment 时,视图是空白的,没有项目被绘制。而且,如果我退出这个片段并进入另一个时间片段是空白的。
当我从细节片段返回到 AroundMeFragment 时,如何查看片段的内容?
ArouundMeFragment:
public ArroundMeFragment() {
}
public static Fragment newInstance(Bundle bundle) {
ArroundMeFragment f = new ArroundMeFragment();
f.setArguments(bundle);
return f;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sectionShowed = MAP_VIEW_ARROUND_ME;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.arround_me_main_layout, container, false);
mViewPager = (ViewPager) v.findViewById(R.id.viewPager);
if (fragments == null) {
fragments = new ArrayList<Fragment>();
fragments.add(MapMainFragment.newInstance());
Bundle bundle = new Bundle();
bundle.putInt(ConstantsTypeSection.TYPE_VIEW_SECTION, ConstantsTypeSection.AROUND_ME_SECTION);
fragments.add(DetailShapelessListViewFragment.newInstance(bundle));
}
mAdapter = new AroundMePagerAdapter(getFragmentManager(), fragments);
mViewPager.setAdapter(mAdapter);
// BTN CHANGE VIEW
btn_change_view = (ImageButton) v.findViewById(R.id.btn_change_view);
btn_change_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
changeView();
}
});
return v;
}
/**
* Cambia una vista u otra
*/
private void changeView() {
try {
if (sectionShowed == LIST_VIEW_ARROUND_ME) {
mViewPager.setCurrentItem(0);
sectionShowed = MAP_VIEW_ARROUND_ME;
btn_change_view.setImageResource(R.drawable.selector_btn_change_map_arroundme);
} else if (sectionShowed == MAP_VIEW_ARROUND_ME) {
mViewPager.setCurrentItem(1);
sectionShowed = LIST_VIEW_ARROUND_ME;
btn_change_view.setImageResource(R.drawable.selector_btn_change_list_arroundme);
}
} catch (Exception e) {
e.printStackTrace();
}
}// End Method
private class AroundMePagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
public AroundMePagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
主要活动。只有一个活动支持应用程序中的所有片段。
....
case ConstantsMenu.ARROUND_ME_SECTION:
Fragment = ArroundMeFragment.newInstance(bundle);
break;
....
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, mFragment);
if (hasToAddToBackStack) {
ft.addToBackStack(null);
}
ft.commit();