Updating fragments/views in viewpager with fragmentStatePagerAdapter
Asked Answered
A

2

7

Need some help with my problem of updating pages while using viewpager. I am using a simple viewpager with FragmentStatePagerAdapter. All I want to do is get access to the current fragment/view so as I can update some textviews in my fragment/view. I searched around in the forum and came to know few things - One of the ways to handle this is by setting tag in instantiateItem() call back of the adapter and retreive the view by findViewbyTag. I could not understand how to implement this and I am also not sure if that will work for FragmentStatePagerAdapter. - I explored other options as suggested in various forums, but cannot make them work.

My code is very much same as in a android http://developer.android.com/training/animation/screen-slide.html. Basic components are the same (Fragment activity xml with some display components including a textview, viewpager xml with just a view pager in it, a Fragment class and the main FragmentActivity class). in my FragmentActivity class I have added a pageChangelistener to my viewpager so as I can do my textview changes during onPageSelected().

Any help is is appreciated.

Adding the code for reference.

Public class myActivity extends FragmentActivity
  //Variable declarations
  protected void onCreate(Bundle savedInstanceState) {
  ViewPager mPager = (ViewPager) findViewById(R.id.pager);
  mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
  mPager.setAdapter(mPagerAdapter);
  View CurrView;

  OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
    @Override
    public void onPageSelected(int pageSelected) {
       doTextViewChnges();//access the text view and update it based on pageSelected

   ---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
   }

  mPager.setOnPageChangeListener(pageChangelistener);
 }
  private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 
    public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager  fm) {
        super(fm);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) { 
        return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}
Anemia answered 4/5, 2013 at 9:44 Comment(0)
A
2

in your OnPageChangeListener:

      OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
            @Override
            public void onPageSelected(int pageSelected) {
 ScreenSlidePageFragment currentFragment = (ScreenSlidePageFragment) mPagerAdapter.getItem(pageSelected)
               doTextViewChnges();//access the text view and update it based on pageSelected

           ---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
           }
Adolfo answered 21/6, 2013 at 1:50 Comment(0)
S
-1

i assume you want to access your fragments from your activity and update the views.Fragments are parts of activities and their views can be accessed by them. Fragments are created dynamically in viewadapters and there is no straight forward and easy way to Tag them. You may simply access a fragment by its index number.first one is 0 second one is 1 and so on...

//access your fragment(in your case your first fragment)
//this part should be inside your doTextViewChnges()
Fragment fragment = (Fragment ) adapterViewPager
            .getFragment(0);
    if (fragment  != null) {
//call a public method inside your fragment to update your text view
        fragment .updateYourTextView();
    }

Edit: inside your fragment create the following method and update your textview from there.

void updateYourTextView() {
    yourTextView.setText("yourtext");

}
Soulier answered 4/5, 2013 at 10:0 Comment(4)
I could not understand how did you get the Fragment object in your first line. Below is the code inside by Fragment activity class where I create the adaptorAnemia
@Anemia how many fragments have you got? You may replace FragmentTwo with whatever your fragment name is.When you have a reference to your fragment, you may update its view.Soulier
I have only one fragment as I understand. The number of pages are determined dynamically in onCreate whcih I pass on while creating a new fargment in "ScreenSlidePageFragment.create(position, <other parameters I want to pass>)". You can see that in the code in my question. So i have access to the page number at any point in the call back onPageSelected(int position). Its in this method that I want to get hold of my textview on that page.Anemia
I need to call updateTextView() only when a new page is loaded. So placing that method inside the Fragment will cause it to work only when viewpager gets created in the line "mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); "Anemia

© 2022 - 2024 — McMap. All rights reserved.