简体   繁体   English

如何使用ArrayList中的字符串填充ViewPager?

[英]How to populate ViewPager with strings from ArrayList?

I have a list of items that each has their own ArrayList<String> . 我有一个项目列表,每个项目都有自己的ArrayList<String> When an item is clicked, the ViewPager activity is opened. 单击一个项目时,将打开ViewPager活动。 I want each string to fill up an entire view in ViewPager, so that the user can swipe through each of these strings as pages. 我希望每个字符串都填满ViewPager中的整个视图,以便用户可以将这些字符串中的每一个作为页面滑动。

  1. Is ViewPager the best way to go about doing this? ViewPager是执行此操作的最佳方法吗?
  2. If so, how can I populate it with the contents of the ArrayList<String> ? 如果是这样,我如何用ArrayList<String>的内容填充它?

You should probably use a ViewPager . 您可能应该使用ViewPager To use it you need to supply a PagerAdapter . 要使用它,您需要提供PagerAdapter In your subclass of PagerAdapter, just have your instatiateItem override create a TextView (either programmatically, or by inflating a layout with a TextView) and set the text to the String at the position in the array corresponding to the position parameter of instantiateItem. 在PagerAdapter的子类中,只需让instatiateItem重写创建TextView(以编程方式或通过使用TextView扩大布局),然后将文本设置为String中位于与InstantiateItem的position参数相对应的数组中的position Something like this 像这样

@Override
public Object instantiateItem (ViewGroup container, int position) {
    TextView tv = new TextView(getContext());
    tv.setText(mList.get(position));
    return tv;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM