简体   繁体   中英

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> ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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