Android horizontal listviewposted by Paul on march7, 2011
Due popularity, I have decided to create a GitHub Repo for this project. Please Checkout the code:
Http://github.com/dinocore1/Android-Horizontal-ListView 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 wowould 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:
View plaincopy to clipboardprint?
- 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:
View plaincopy to clipboardprint?
- <! --? XML version = "1.0" encoding = "UTF-8 "? -->
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Background = "# fff">
- <COM. devsmart. android. UI. horizontiallistview Android: Id = "@ + ID/listview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Background = "# DDD">
- </COM. devsmart. Android. UI. horizontiallistview> </linearlayout>
Res/layout/listitem. xml:
View plaincopy to clipboardprint?
- <! --? XML version = "1.0" encoding = "UTF-8 "? -->
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "# fff">
- <Imageview Android: Id = "@ + ID/image" Android: layout_width = "150dip" Android: layout_height = "150dip" Android: scaletype = "centercrop" Android: src = "@ drawable/icon">
- <Textview Android: Id = "@ + ID/Title" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: textcolor = "#000" Android: gravity = "center_horizontal">
- </Textview> </imageview> </linearlayout>
Download code here
Horizontal listview (1931)
Changelist
1.5:
Adapter. notifydatasetchanged () Now saves position in list instead of starting at ining
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.