Please tell me how to reset the content of ViewPager on android. I tried to call adapter.notifyDataSetChanged() but the adapter doesn't call getItem(position) when I scroll the view. It always returns old child views.
Update: This is my Fragment View. When I reset viewPager data, It used loaded Fragment views. It's just start at the onCreateView instead of Constructor.
public class ResultView extends Fragment {
//Declare properties
//Constructor
public ResultView(Province province, Calendar calendar) {
Log.d("xskt", "ResultView constructor");
this.province = province;
this.calendar = (Calendar) calendar.clone();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("xskt", "ResultView onCreateView");
View view = inflater.inflate(R.layout.resultview, null);
//do some thing
return view;
}
Thanks.
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. addOnPageChangeListener(new ViewPager.
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.
Return the number of views available. Called when the host view is attempting to determine if an item's position has changed. This method may be called by the ViewPager to obtain a title string to describe the specified page. This method is deprecated.
I have changed to use FragmentStatePagerAdapter
instead of FragmentPagerAdapter
, the problem is resolved. Hope this helps.
I think the best solution to reload fragments in another fragment is :
@Override
public void onPause() {
super.onResume();
List<Fragment> fragments = getFragmentManager().getFragments();
if (fragments != null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
for (Fragment fragment : fragments) {
if (fragment instanceof Fragment) {
fragmentTransaction.remove(fragment);
}
}
fragmentTransaction.commitNowAllowingStateLoss();
}
}
Check for your use case whether commitNowAllowingStateLoss
or commitAllowingStateLoss
is necessary.
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