Android Horizontal ListView

Due popularity, I have decided to create a GitHub repo for this project. Please checkout the code at: https://github.com/dinocore1/DevsmartLib-Android Pull requests are welcome! 😉

Licensed under MIT License Copyright (c) 2011 Paul Soucy, (paul+blog@dev-smart.com)

The Android API seems to be lacking a Horizontal ListView widget. Basically, what I needed was something exactly like the Gallery widget but without the center-locking feature. After much googling (and finding dead ends like this or this), I eventually came to the conclusion that a Horizontal ListView simply did not exist. So I built my own…

My Android Horizontal ListView implementation has the following features:

  • Subclass AdapterView so I can make use of adapters
  • Fast – make use of recycled views when possible
  • Items are clickable – (accepts AdapterView.OnItemClickListener)
  • Scrollable
  • No center-locking
  • Simple – is that so much to ask?

If you find this helpful, let me know, I would love to hear your feedback.

How to use:
Horizontal ListView is ment to be a drop-in replacement for a standard ListView. Here is some quick demo code to get you started:

package com.devsmart.android.test; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.devsmart.android.ui.HorizontialListView; public class HorizontalListViewDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listviewdemo); HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview); listview.setAdapter(mAdapter); } private static String[] dataObjects = new String[]{ "Text #1", "Text #2", "Text #3" }; private BaseAdapter mAdapter = new BaseAdapter() { @Override public int getCount() { return dataObjects.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null); TextView title = (TextView) retval.findViewById(R.id.title); title.setText(dataObjects[position]); return retval; } }; }

res/layout/listviewdemo.xml:

res/layout/listitem.xml:

Download code from GitHub

old download link: [download id=”1″]

Changelist
1.5:
adapter.notifyDataSetChanged() now saves position in list instead of starting at begining

1.4:
Added code to respond to adapter.notifyDataSetChanged()

1.3:
added mScroller.forceFinished(true); to the onDown function of mOnGesture so the user con stop the scroll on tap.

Paul Soucy

Read more posts by this author.