Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset viewpager content on Android?

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.

like image 708
Nguyen Minh Binh Avatar asked Dec 04 '12 15:12

Nguyen Minh Binh


People also ask

How do I refresh ViewPager fragments?

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.

How do I refresh Fragmentstateadapter?

You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.

What is ViewPager Android?

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.

Is ViewPager deprecated in Android?

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.


2 Answers

I have changed to use FragmentStatePagerAdapter instead of FragmentPagerAdapter, the problem is resolved. Hope this helps.

like image 51
Nguyen Minh Binh Avatar answered Oct 21 '22 12:10

Nguyen Minh Binh


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.

like image 25
Hadi Note Avatar answered Oct 21 '22 13:10

Hadi Note