0

I am new the Android development (3 weeks).

I'd like to create an Activity that allows the user to scroll through a list of items. I'd like only one item to occupy the width/height of the screen at any given time. At the same time, I'd like for the items to be able to scroll smoothly (up/down), similarly to facebook/instagram. During run-time, items will be pushed on top of the stack/list (like a news feed in FB).

What is the best way to accomplish this? What are the pros/cons between using a ListView, LinearLayout (Vertical) items, a List of Buttons added on top of one another? Or should I use Fragments that display on top of one another? How would I implement the ability to display only one item at a given moment?

code
  • 5,294
  • 16
  • 62
  • 113
  • 1
    Sounds like you want a [ViewPager](http://developer.android.com/reference/android/support/v4/view/ViewPager.html). By default, its motion is horizontal, but there are examples on-site that show how to make it vertical; e.g., [this post](http://stackoverflow.com/questions/13477820/android-vertical-viewpager). – Mike M. Oct 14 '14 at 00:31
  • Thank you so much Mike! This is exactly what I was looking for. Are ListViews used for smaller items? And is it unconventional to use a LinearLayout with a large number of vertical items? – code Oct 14 '14 at 00:47
  • 1
    One benefit of ListViews is that they handle their child Views efficiently, so if you have a sizeable collection, smaller items or not, they're less likely to suffer from scrolling lag or OOM errors. If you have a rather small number of items, a scrollable LinearLayout might work just fine, but in that case, even items not visible on-screen are resident in memory, which can lead to the aforementioned problems. A ViewPager is pretty much for the use case you've described; i.e., it scrolls, but snaps to a single, visible View at a time. ListViews are for smooth scrolling, flinging through a list – Mike M. Oct 14 '14 at 01:00

1 Answers1

1

The answer is to use a ViewPager: http://developer.android.com/reference/android/support/v4/view/ViewPager.html

By default, the motion is horizontal, but there are examples that show how to make it vertical.

code
  • 5,294
  • 16
  • 62
  • 113