Horizontal Image Scroller for Android

HorizontalImageScroller-Android is a UI widget library for Android that will let your users scroll through a horizontal list of images, and let you implement it without breaking a sweat.

We wanted to include a horizontally-scrollable list of images so that many of them could be shown without much expense in smartphone screen real estate. Other information might need to be presented on the same screen as well, so our scroller would need to be hosted inside a vertical scroll view, and would have to play nice with its motion event handling. We also knew the images would need to be retrieved from a URL, and sometimes there would be many of them, so they would have to be lazy-loaded and cached locally.

Horizontal Image Scroller in Room 77 app

HorizontalImageScroller-Android solves these problems, and makes it easy to implement in your own app. Imagine a scenario where you have a list of objects that contain URLs that correspond to photos, and you’d like to show those in a horizontally-scrollable list.

In the layout xml for your activity (or fragment, if that’s how you roll), just add the element:

1
2
3
4
<com.twotoasters.android.horizontalimagescroller.widget.HorizontalImageScroller
    android:id="@+id/my_horizontal_image_scroller"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Then, in your activity’s onCreate() (or your fragment’s onCreateView()), just set up the scroller’s adapter with a list of ImageToLoadUrl objects.

1
2
3
4
5
6
ArrayList<ImageToLoad> images = new ArrayList<ImageToLoad>();
for (YourObjectWithSomeImageUrlProperty myObject : myListOfObjectsWithSomeImageUrlProperty) {
    images.add(new ImageToLoadUrl(myObject.getTheUrlToTheImage())); // adapt as necessary
}
HorizontalImageScroller scroller = (HorizontalImageScroller) findViewById(R.id.my_horizontal_image_scroller);
scroller.setAdapter(new HorizontalImageScrollerAdapter(images));

For more detail, see the readme included with the HorizontalImageScroller-Android library. For even more detail, see the included sample app. The above should be enough to get you started though.

Horizontal Image Scroller Sample app


Comments