Eddie K
Eddie K

Reputation: 513

How to populate ViewPager with strings from ArrayList?

I have a list of items that each has their own ArrayList<String>. When an item is clicked, the ViewPager activity is opened. 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.

  1. Is ViewPager the best way to go about doing this?
  2. If so, how can I populate it with the contents of the ArrayList<String>?

Upvotes: 1

Views: 2371

Answers (1)

Greg Ennis
Greg Ennis

Reputation: 15379

You should probably use a ViewPager. To use it you need to supply a 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. Something like this

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

Upvotes: 1

Related Questions