Jake
Jake

Reputation: 633

Get current index in fragment of ViewPager

Using a sliding ViewPager I want to create a list of articles. During the move, I want to dynamically change the title in the action bar. Titles are stored in the list of strings, and the title of each list is the position of the current fragment. So, for example, given this list:

List<String> leftItems = new List<String> ();
leftItems.Add ("First title");
leftItems.Add ("Second title");
leftItems.Add ("Third title");

I would like to while watching the fragment 2 (index 1) is displayed title

Second title

Title to the action bar in the fragment I change this code:

((ActionBarActivity)Activity).SupportActionBar.Title = this.leftItems[this.position];

The variable 'position', which comes with the adapter in the method GetItem ():

public override Android.Support.V4.App.Fragment GetItem(int position) {
            return new ArticlesFragment (this.articles [this.leftItems [position]], 
                this.leftItems, this.position);
        }

public ArticlesFragment(List<Article> articles, List<String> leftItems, int position) : base() {
            this.articles = articles;
            this.leftItems = leftItems;
            this.position = position;
        }

With what I have a problem? Well, the title of the action bar is always one index greater than it should. So for fragment 1 (index 0), the title is

Second title

and not as I want

First title

But, in the last fragment, the title does not change, only stays with the previous fragment.

Does someone can help me?

@edit

I add the entire contents of the adapter and a fragment:

ArticlesFragmentAdapter.cs

public class ArticlesFragmentAdapter : FragmentStatePagerAdapter {
    Dictionary<String, List<Article>> articles;
    List<String> leftItems;

    public ArticlesFragmentAdapter(Android.Support.V4.App.FragmentManager fm, 
        Dictionary<String, List<Article>> articles,
        List<String> leftItems) : base(fm) {
        this.articles = articles;
        this.leftItems = leftItems;
    }

    public override int Count {
        get { return this.leftItems.Count; }
    }

    public override Android.Support.V4.App.Fragment GetItem(int position) {
        return new ArticlesFragment (this.articles [this.leftItems [position]], 
            this.leftItems, position);
    }
}

ArticlesFragment.cs

public class ArticlesFragment : Android.Support.V4.App.Fragment {
    List<Article> articles;
    List<String> leftItems;
    ListView articlesListView;
    int position;
    ArticlesAdapter articlesAdapter;

    public ArticlesFragment(List<Article> articles, List<String> leftItems, int position) : base() {
        this.articles = articles;
        this.leftItems = leftItems;
        this.position = position;
    }

    public override View OnCreateView (LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate (Resource.Layout.Articles, container, false);

        this.articlesListView = view.FindViewById<ListView> (Resource.Id.articlesListView);
        ((ActionBarActivity)Activity).SupportActionBar.Title = this.leftItems[this.position - 1];

        this.articlesAdapter = new ArticlesAdapter ((ActionBarActivity)this.Activity, this.articles);

        this.articlesListView.Adapter = this.articlesAdapter;

        return view;
    }
}

Upvotes: 1

Views: 645

Answers (1)

Kuffs
Kuffs

Reputation: 35661

you could use the http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageSelected(int) to update the title in the activity when the page changes

Upvotes: 2

Related Questions