Android Horizontal ListView

Android 水平的ListView_第1张图片

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

Licensed under MIT License Copyright (c) 2011 Paul Soucy, ([email protected])

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:

view plain copy to clipboard print ?
  1. packagecom.devsmart.android.test;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.LayoutInflater;
  5. importandroid.view.View;
  6. importandroid.view.ViewGroup;
  7. importandroid.widget.BaseAdapter;
  8. importandroid.widget.TextView;
  9. importcom.devsmart.android.ui.HorizontialListView;
  10. publicclassHorizontalListViewDemoextendsActivity{
  11. @Override
  12. protectedvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.listviewdemo);
  15. HorizontialListViewlistview=(HorizontialListView)findViewById(R.id.listview);
  16. listview.setAdapter(mAdapter);
  17. }
  18. privatestaticString[]dataObjects=newString[]{"Text#1",
  19. "Text#2",
  20. "Text#3"};
  21. privateBaseAdaptermAdapter=newBaseAdapter(){
  22. @Override
  23. publicintgetCount(){
  24. returndataObjects.length;
  25. }
  26. @Override
  27. publicObjectgetItem(intposition){
  28. returnnull;
  29. }
  30. @Override
  31. publiclonggetItemId(intposition){
  32. return0;
  33. }
  34. @Override
  35. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  36. Viewretval=LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem,null);
  37. TextViewtitle=(TextView)retval.findViewById(R.id.title);
  38. title.setText(dataObjects[position]);
  39. returnretval;
  40. }
  41. };
  42. }

res/layout/listviewdemo.xml:

view plain copy to clipboard print ?
  1. <!--?xmlversion="1.0"encoding="utf-8"?-->
  2. <linearlayoutxmlns: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.horizontiallistviewandroid: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 plain copy to clipboard print ?
  1. <!--?xmlversion="1.0"encoding="utf-8"?-->
  2. <linearlayoutxmlns: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. <imageviewandroid:id="@+id/image"android:layout_width="150dip"android:layout_height="150dip"android:scaletype="centerCrop"android:src="@drawable/icon">
  4. <textviewandroid: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 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.


更多相关文章

  1. android图片压缩并转为base64字符串
  2. android > 图片旋转
  3. android 滚动的缓冲图片
  4. android:设计一个能在图片上涂鸦的程序。
  5. android NinePatchDrawable 9.png图片使用制作
  6. Android 查看保存图片到相册
  7. Android 下拉刷新 ListRefresh 从网络加载图片
  8. android 双击图片放大缩小
  9. android加载本地图片

随机推荐

  1. Android 调试错误: java.lang.SecurityEx
  2. Android room操作数据库
  3. Android通过Servlet连接MySQL 实现登陆/
  4. android webview ZoomButtonsController
  5. Android开发之Intent.Action
  6. Android(安卓)JNI编程提高篇之二
  7. JRuby on Java ME/CDC
  8. android:visibility
  9. Android作为HTTP服务器--NanoHTTPD源码分
  10. Android 播放提示音