使用滑动ViewPager我想创建一个文章列表。在移动过程中,我想动态更改操作栏中的标题。标题存储在字符串列表中,每个列表的标题是当前片段的位置。因此,例如,给出此列表:
List<String> leftItems = new List<String> ();
leftItems.Add ("First title");
leftItems.Add ("Second title");
leftItems.Add ("Third title");
我想在观看片段2(索引1)时显示标题
第二个标题
片段中操作栏的标题我更改了此代码:
((ActionBarActivity)Activity).SupportActionBar.Title = this.leftItems[this.position];
变量'position',它与方法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;
}
我遇到了什么问题?好吧,操作栏的标题总是一个比它应该更大的索引。因此对于片段1(索引0),标题是
第二个标题
而不是我想要的
第一个标题
但是,在最后一个片段中,标题不会改变,只会保留前一个片段。
有人可以帮助我吗?
@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;
}
}
答案 0 :(得分:2)