获取 ViewPager 片段中的当前索引

Get current index in fragment of ViewPager

我想使用滑动 ViewPager 创建文章列表。在移动过程中,我想动态更改操作栏中的标题。标题存储在字符串列表中,每个列表的标题是当前片段的位置。因此,例如,给定此列表:

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

我想在观看片段 2(索引 1)时显示标题

Second title

片段中操作栏的标题我更改了这段代码:

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

方法GetItem()中适配器自带的变量'position':

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

我有什么问题?好吧,操作栏的标题总是比它应该的大一个索引。所以对于片段 1(索引 0),标题是

Second title

并不如我所愿

First title

但是,在最后一个片段中,标题没有改变,只与前一个片段保持一致。

有人可以帮助我吗?

@edit

我添加了适配器的全部内容和一个片段:

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

您可以在页面更改时使用 http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageSelected(int) 更新 activity 中的标题