Updating fragments / views in the viewpager using the fragmentStatePagerAdapter

Need help on my page refresh issue when using viewpager. I am using a simple viewpager with a FragmentStatePagerAdapter. All I want to do is access the current fragment / view in order to update some text fragments in my fragment / view. I searched around the forum and found out a few things - One way to deal with this is to set the tag in the instantiateItem () callback back and look for the findViewbyTag view. I could not figure out how to implement this, and I am also not sure if this will work for the FragmentStatePagerAdapter. - I studied other options proposed in different forums, but I can not get them to work.

My code is very similar to android http://developer.android.com/training/animation/screen-slide.html . The basic components are the same (Fragment activity xml with some display components, including textview, viewpager xml with only the presentation pager in it, the fragment class and the main FragmentActivity class). in my FragmentActivity class, I added a pageChangelistener to my viewer so that I can make changes to the text while onPageSelected ().

Any help is appreciated.

Adding 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; } } 
+6
android android-fragments android-viewpager
source share
2 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------- } 
+2
source share

I assume that you want to access your snippets from your activity and update views. Fragments are parts of an activity, and their representations may be available to them. Snippets are created dynamically in viewadapters, and there is no direct and easy way to tag them. You can simply access the fragment by its index number. The first - 0 seconds one - 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 the fragment, create the following method and update the text.

 void updateYourTextView() { yourTextView.setText("yourtext"); } 
-one
source share

All Articles