请问我是使用Xamarin android进行android开发的新手,我按照一个教程(https://gist.github.com/AlejandroRuiz/c1338bb0166754c6da3d)了解了如何创建一个viewpager,我做的一切都很正确并且运行良好。这是页面适配器的代码
public class CardsPagerAdapter:Android.Support.V13.App.FragmentStatePagerAdapter { 公共类EventPage { //此树图像的图像ID: 公共字符串日期;
// Caption text for this image:
public string myEvent;
// Returns the ID of the image:
public string Date { get { return date; } }
// Returns the caption text for the image:
public string Events { get { return myEvent; } }
}
static EventPage[] mCards = {
new EventPage {date = "12 October",
myEvent = "1938 - Production began on \"The Wizard of Oz.\""},
new EventPage { date = "13 October",
myEvent = "1893 The melody for Happy Birthday To You was copyright registered. Happy Birthday was originally published as Good Morning To All in a book called Song Stories for the Kindergarten written by Mildred and Patty Hill." },
new EventPage { date = "14 October",
myEvent = "1835 Henry Blair received a patent in for an improved corn planter." },
new EventPage { date = "15 October",
myEvent = "1991 Pizza Hut was trademark registered." },
new EventPage { date = "16 October",
myEvent = "1900 Frank Sprague was granted a patent for a multi-control for electric trains." },
new EventPage { date = "17 October",
myEvent = "1961 \"HOT ROCKS\" Candy was trademark registered." },
new EventPage { date = "18 October",
myEvent = "1958 - The first computer-arranged marriage took place on Art Linkletter's show." },
new EventPage { date = "19 October",
myEvent = "1986 - Foremost journalist, Dele Giwa, assassinated by a parcel bomb which is often blamed on the Babangida regime" },
};
private List<Fragment> mCardsFragments { get; set; }
public CardsPagerAdapter(FragmentManager fm) : base(fm)
{
mCardsFragments = new List<Fragment>{
new PagerFragment(mCards[0].date, mCards[0].myEvent),
new PagerFragment(mCards[1].date, mCards[1].myEvent),
new PagerFragment(mCards[2].date, mCards[2].myEvent),
new PagerFragment(mCards[3].date, mCards[3].myEvent),
new PagerFragment(mCards[4].date, mCards[4].myEvent),
new PagerFragment(mCards[5].date, mCards[5].myEvent),
new PagerFragment(mCards[6].date, mCards[6].myEvent),
new PagerFragment(mCards[7].date, mCards[7].myEvent),
};
}
#region implemented abstract members of PagerAdapter
public override int Count
{
get
{
return mCardsFragments.Count;
}
}
#endregion
#region implemented abstract members of FragmentStatePagerAdapter
public override Fragment GetItem(int position)
{
return mCardsFragments[position];
}
public override Java.Lang.ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(mCards[position].date);
}
#endregion
}
我现在面临的问题是如何使用循环动态地将值输入到PagerFragment中,因为我从数据库中获取数据。
我需要循环的代码
mCardsFragments =新列表{
//希望循环在这里
new PagerFragment(mCards[0].date, mCards[0].myEvent),
new PagerFragment(mCards[1].date, mCards[1].myEvent),
new PagerFragment(mCards[2].date, mCards[2].myEvent),
new PagerFragment(mCards[3].date, mCards[3].myEvent),
new PagerFragment(mCards[4].date, mCards[4].myEvent),
new PagerFragment(mCards[5].date, mCards[5].myEvent),
new PagerFragment(mCards[6].date, mCards[6].myEvent),
new PagerFragment(mCards[7].date, mCards[7].myEvent),
};
答案 0 :(得分:1)
mCardsFragments = new List<Fragment>();
for (int i = 0; i < mCards.Count; i++)
{
mCardsFragments.Add(new PagerFragment(mCards[i].date, mCards[i].myEvent));
}