I have a fragment AroundMeFragment which has a view pager with two fragments MapFragment
and ListFragment
. When I enter the first time in this fragment all is ok, but when I click an item to enter to a detail view in a new fragment, and return back to AroundMeFragment, the view is blank and no item is draw. And, if I exit from this fragment and enter another time the fragment is blank.
How I can see the content of the fragments when I return back to AroundMeFragment from a detail fragment?
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();
}
}
MainActivity. Only one activity to support all fragments in the app.
....
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();
To enable / disable the swiping, just overide two methods: onTouchEvent and onInterceptTouchEvent . Both will return "false" if the paging was disabled. You just need to call the setPagingEnabled method with false and users won't be able to swipe to paginate.
Using the ViewPager. OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager.
yes...you can use View instead of Fragment in viewpager.
mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());
Instead of
mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
as by Austyn Mahoney
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With