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;
    }
}
4

2 回答 2

2

在您的 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-------
           }
于 2013-06-21T01:50:09.320 回答
-1

我假设您想从您的活动中访问您的片段并更新视图。片段是活动的一部分,它们可以访问它们的视图。片段是在视图适配器中动态创建的,没有直接简单的方法来标记它们。您可以简单地通过索引号访问片段。第一个是 0,第二个是 1,依此类推...

//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();
    }

编辑:在您的片段中创建以下方法并从那里更新您的文本视图。

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

}
于 2013-05-04T10:00:18.030 回答