Android(安卓)水平的ListView
Android Horizontal ListView
Posted by paul on March 7, 2011
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, (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:
- packagecom.devsmart.android.test;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.BaseAdapter;
- importandroid.widget.TextView;
- importcom.devsmart.android.ui.HorizontialListView;
- publicclassHorizontalListViewDemoextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listviewdemo);
- HorizontialListViewlistview=(HorizontialListView)findViewById(R.id.listview);
- listview.setAdapter(mAdapter);
- }
- privatestaticString[]dataObjects=newString[]{"Text#1",
- "Text#2",
- "Text#3"};
- privateBaseAdaptermAdapter=newBaseAdapter(){
- @Override
- publicintgetCount(){
- returndataObjects.length;
- }
- @Override
- publicObjectgetItem(intposition){
- returnnull;
- }
- @Override
- publiclonggetItemId(intposition){
- return0;
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
- Viewretval=LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem,null);
- TextViewtitle=(TextView)retval.findViewById(R.id.title);
- title.setText(dataObjects[position]);
- returnretval;
- }
- };
- }
res/layout/listviewdemo.xml:
view plain copy to clipboard print ?- <!--?xmlversion="1.0"encoding="utf-8"?-->
- <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">
- <com.devsmart.android.ui.horizontiallistviewandroid: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 plain copy to clipboard print ?- <!--?xmlversion="1.0"encoding="utf-8"?-->
- <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">
- <imageviewandroid:id="@+id/image"android:layout_width="150dip"android:layout_height="150dip"android:scaletype="centerCrop"android:src="@drawable/icon">
- <textviewandroid: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 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.
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer