Android horizontal listview

Source: Internet
Author: User
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?
  1. Package com. devsmart. Android. test;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. View. layoutinflater;
  5. Import Android. View. view;
  6. Import Android. View. viewgroup;
  7. Import Android. widget. baseadapter;
  8. Import Android. widget. textview;
  9. Import com. devsmart. Android. UI. horizontiallistview;
  10. Public class horizontallistviewdemo extends activity {
  11. @ Override
  12. Protected void oncreate (bundle savedinstancestate ){
  13. Super. oncreate (savedinstancestate );
  14. Setcontentview (R. layout. listviewdemo );
  15. Horizontiallistview listview = (horizontiallistview) findviewbyid (R. Id. listview );
  16. Listview. setadapter (madapter );
  17. }
  18. Private Static string [] dataobjects = new string [] {"text #1 ",
  19. "Text #2 ",
  20. "Text #3 "};
  21. Private baseadapter madapter = new baseadapter (){
  22. @ Override
  23. Public int getcount (){
  24. Return dataobjects. length;
  25. }
  26. @ Override
  27. Public object getitem (INT position ){
  28. Return NULL;
  29. }
  30. @ Override
  31. Public long getitemid (INT position ){
  32. Return 0;
  33. }
  34. @ Override
  35. Public View getview (INT position, view convertview, viewgroup parent ){
  36. View retval = layoutinflater. From (parent. getcontext (). Inflate (R. layout. viewitem, null );
  37. Textview Title = (textview) retval. findviewbyid (R. Id. Title );
  38. Title. settext (dataobjects [position]);
  39. Return retval;
  40. }
  41. };
  42. }

Res/layout/listviewdemo. xml:

View plaincopy to clipboardprint?
  1. <! --? XML version = "1.0" encoding = "UTF-8 "? -->
  2. <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">
  3. <COM. devsmart. android. UI. horizontiallistview Android: Id = "@ + ID/listview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Background = "# DDD">
  4. </COM. devsmart. Android. UI. horizontiallistview> </linearlayout>

Res/layout/listitem. xml:

View plaincopy to clipboardprint?
  1. <! --? XML version = "1.0" encoding = "UTF-8 "? -->
  2. <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">
  3. <Imageview Android: Id = "@ + ID/image" Android: layout_width = "150dip" Android: layout_height = "150dip" Android: scaletype = "centercrop" Android: src = "@ drawable/icon">
  4. <Textview Android: Id = "@ + ID/Title" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: textcolor = "#000" Android: gravity = "center_horizontal">
  5. </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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.