Android 水平的ListView

來源:互聯網
上載者:User
Android Horizontal ListViewPosted 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:

view plaincopy to clipboardprint?
  1. package com.devsmart.android.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.TextView;  
  10.   
  11. import com.devsmart.android.ui.HorizontialListView;  
  12.   
  13. public class HorizontalListViewDemo extends Activity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.   
  19.         setContentView(R.layout.listviewdemo);  
  20.   
  21.         HorizontialListView listview = (HorizontialListView) findViewById(R.id.listview);  
  22.         listview.setAdapter(mAdapter);  
  23.   
  24.     }  
  25.   
  26.     private static String[] dataObjects = new String[]{ "Text #1",  
  27.         "Text #2",  
  28.         "Text #3" };   
  29.   
  30.     private BaseAdapter mAdapter = new BaseAdapter() {  
  31.   
  32.         @Override  
  33.         public int getCount() {  
  34.             return dataObjects.length;  
  35.         }  
  36.   
  37.         @Override  
  38.         public Object getItem(int position) {  
  39.             return null;  
  40.         }  
  41.   
  42.         @Override  
  43.         public long getItemId(int position) {  
  44.             return 0;  
  45.         }  
  46.   
  47.         @Override  
  48.         public View getView(int position, View convertView, ViewGroup parent) {  
  49.             View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);  
  50.             TextView title = (TextView) retval.findViewById(R.id.title);  
  51.             title.setText(dataObjects[position]);  
  52.   
  53.             return retval;  
  54.         }  
  55.   
  56.     };  
  57.   
  58. }  

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.   
  4.   <com.devsmart.android.ui.horizontiallistview android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ddd">  
  5.   
  6. </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.   
  4.   <imageview android:id="@+id/image" android:layout_width="150dip" android:layout_height="150dip" android:scaletype="centerCrop" android:src="@drawable/icon">  
  5.   
  6.     <textview android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#000" android:gravity="center_horizontal">  
  7.   
  8. </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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.