Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating fragments/views in viewpager with fragmentStatePagerAdapter

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;
    }
}
like image 662
user1938357 Avatar asked May 04 '13 09:05

user1938357


1 Answers

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-------
           }
like image 192
nebula Avatar answered Oct 17 '22 21:10

nebula