日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android App列表之游标ListView(索引ListView)

發(fā)布時間:2024/4/17 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android App列表之游标ListView(索引ListView) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原帖地址:http://www.apkbus.com/android-14717-1-1.html
游標ListView,提供索引標簽,使用戶能夠快速定位列表項。
? ?? ?也可以叫索引ListView,有的人稱也為Tweaked ListView,可能更形象些吧。
? ?? ?一看圖啥都懂了: 1.游標(Fast scroll thumb)
? ?? ?就是右邊的那個拖動的方塊,這個非常的簡單:
  • <ListView
  • ? ? android:id="@+id/tweaked_list"
  • ? ? android:layout_width="fill_parent"?
  • ? ? android:layout_height="wrap_content"?
  • ? ? android:fastScrollEnabled="true"/>
  • 復制代碼 也可以用在java后臺書寫:
  • tweakedListView.setFastScrollEnabled(true);
  • 復制代碼 在數(shù)據(jù)量有一定大的時候,滑動列表,就會出現(xiàn)右邊的所謂的"游標"了。
    ? ?? ?簡單,這也是我為什么私下里喜歡自己寫控件,但是工作中卻喜歡用通用控件。
    ? ?? ?我們看下源代碼,其實就是啟用FastScroller對象:?
  • //啟用FastScroller對象
  • public void setFastScrollEnabled(boolean enabled) {
  • ? ? mFastScrollEnabled = enabled;
  • ? ? if (enabled) {
  • ? ?? ???if (mFastScroller == null) {
  • ? ?? ?? ?? ?mFastScroller = new FastScroller(getContext(), this);
  • ? ?? ???}
  • ? ? } else {
  • ? ?? ???if (mFastScroller != null) {
  • ? ?? ?? ?? ?mFastScroller.stop();
  • ? ?? ?? ?? ?mFastScroller = null;
  • ? ?? ???}
  • ? ? }
  • }
  • 復制代碼 2.字母索引
    ? ???在 Android學習系列(10)--App列表之拖拽ListView(上)中 我們使用了一種WindowManager在ListView中添加一些自定義影像,這種方法我覺得一定是可行的。
    ? ?但是,android系統(tǒng)給我們提供了一個更簡單的方法:使用AlphabetIndexer。
    ? ?AlphabetIndexer,實現(xiàn)了SectionIndexer接口,是adapter的一個輔助類,輔助實現(xiàn)在快滑時,顯示索引字母。
    ? ?使用字母索引的話,必須保證數(shù)據(jù)列表是按字母順序排序,以便AlphabetIndexerh采用二分查找法快速定位。
  • /**
  • * Cursor表示數(shù)據(jù)游標
  • * sortedColumnIndex數(shù)據(jù)集合中的第幾列
  • * alphabet字母列表,用的最多的是"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  • **/
  • public AlphabetIndexer(Cursor cursor, int sortedColumnIndex, CharSequence alphabet) {}
  • 復制代碼 用到3個方法:
  • //這三個方法,實現(xiàn)了索引數(shù)據(jù)和列表數(shù)據(jù)的對應和定位
  • public int getPositionForSection(int section) {}
  • public int getSectionForPosition(int position) {}
  • public Object[] getSections() {}
  • 復制代碼 3.游標Cursor的實現(xiàn)
    ? ???Cursor接口的實現(xiàn),有兩種選擇:
    ? ???(1).直接使用數(shù)據(jù)庫查詢返回的cursor
    ? ???(2).自定義實現(xiàn)Cursor接口的新類
    ? ???第一種方式很簡單,查詢一下數(shù)據(jù)庫返回Cursor即可。
    ? ???這里我們以第二種方式實踐,偽裝一個Cursor,主要是實現(xiàn)3個方法:
    ? ???(1).getCount()
    ? ???(2). moveToPosition()
    ? ???(3). getString()
  • /**
  • ? ? * 偽裝一個Cursor供AlphabetIndexer作數(shù)據(jù)索引源
  • ? ? */
  • ? ?private class IndexCursor implements Cursor{
  • ? ?? ??
  • ? ?? ? private ListAdapter adapter;
  • ? ?? ? private int position;
  • ? ?? ? private Map<String, String> map;
  • ? ?? ??
  • ? ?? ? public IndexCursor(ListAdapter adapter){
  • ? ?? ?? ???this.adapter = adapter;
  • ? ?? ? }

  • ? ?? ? @Override
  • ? ?? ? public int getCount() {return this.adapter.getCount();}
  • ? ?? ??
  • ? ?? ? /**
  • ? ?? ???* 取得索引字母,這個方法非常重要,根據(jù)實際情況具體處理
  • ? ?? ???*/
  • ? ?? ? @SuppressWarnings("unchecked")
  • ? ?? ? @Override
  • ? ?? ? public String getString(int columnIndex) {
  • ? ?? ?? ???map = (HashMap<String, String>)adapter.getItem(position);
  • ? ?? ?? ???return map.get(key).substring(0,1);
  • ? ?? ? }
  • ? ?? ??
  • ? ?? ? @Override
  • ? ?? ? public boolean moveToPosition(int position) {
  • ? ?? ?? ???if(position<-1||position>getCount()){
  • ? ?? ?? ?? ?? ?return false;
  • ? ?? ?? ???}
  • ? ?? ?? ???
  • ? ?? ?? ???this.position = position;
  • ? ?? ?? ???//如果不滿意位置有點向上偏的話,下面這幾行代碼是修復定位索引值為頂部項值的問題
  • ? ?? ?? ???//if(position+2>getCount()){? ?? ?? ?? ?? ??
  • ? ?? ?? ???//? ? this.position = position;
  • ? ?? ?? ???//}else{
  • ? ?? ?? ???//? ?this.position = position + 2;
  • ? ?? ?? ???//}
  • ? ?? ?? ???return true;
  • ? ?? ? }
  • ? ?? ??
  • ? ?? ? @Override
  • ? ?? ? public void close() {}
  • ? ?? ? @Override
  • ? ?? ? public void copyStringToBuffer(int arg0, CharArrayBuffer arg1) {}
  • ? ?? ? @Override
  • ? ?? ? public void deactivate() {}
  • ? ?? ? @Override
  • ? ?? ? public byte[] getBlob(int arg0) {return null;}
  • ? ?? ? @Override
  • ? ?? ? public int getColumnCount() {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public int getColumnIndex(String columnName) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public String getColumnName(int columnIndex) {return null;}
  • ? ?? ? @Override
  • ? ?? ? public String[] getColumnNames() {return null;}
  • ? ?? ? @Override
  • ? ?? ? public double getDouble(int columnIndex) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public Bundle getExtras() {return null;}
  • ? ?? ? @Override
  • ? ?? ? public float getFloat(int columnIndex) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public int getInt(int columnIndex) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public long getLong(int columnIndex) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public int getPosition() {return position;}
  • ? ?? ? @Override
  • ? ?? ? public short getShort(int columnIndex) {return 0;}
  • ? ?? ? @Override
  • ? ?? ? public boolean getWantsAllOnMoveCalls() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isAfterLast() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isBeforeFirst() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isClosed() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isFirst() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isLast() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean isNull(int columnIndex) {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean move(int offset) {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean moveToFirst() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean moveToLast() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean moveToNext() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public boolean moveToPrevious() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public void registerContentObserver(ContentObserver observer) {}
  • ? ?? ? @Override
  • ? ?? ? public void registerDataSetObserver(DataSetObserver observer) {}
  • ? ?? ? @Override
  • ? ?? ? public boolean requery() {return false;}
  • ? ?? ? @Override
  • ? ?? ? public Bundle respond(Bundle extras) {return null;}
  • ? ?? ? @Override
  • ? ?? ? public void setNotificationUri(ContentResolver cr, Uri uri) {}
  • ? ?? ? @Override
  • ? ?? ? public void unregisterContentObserver(ContentObserver observer) {}
  • ? ?? ? @Override
  • ? ?? ? public void unregisterDataSetObserver(DataSetObserver observer) {}
  • ? ?? ??
  • ? ?}
  • 復制代碼 這個類的實例就可作為AlphaIndexer的構(gòu)造函數(shù)第一個參數(shù)數(shù)據(jù)游標。 4.自定義Adapter的實現(xiàn)
    ? ?? ?使用前面介紹的東西,我們來實現(xiàn)最終的IndexAdapter:
  • class IndexAdapter extends SimpleAdapter implements SectionIndexer{
  • ? ??
  • ? ? private AlphabetIndexer alphabetIndexer;
  • ? ??
  • ? ? public IndexAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {
  • ? ?? ???super(context, data, resource, from, to);
  • ? ?? ???//設(shè)置數(shù)據(jù)游標
  • ? ?? ???//設(shè)置索引字母列表
  • ? ?? ???alphabetIndexer = new AlphabetIndexer(new IndexCursor(this), 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  • ? ? }

  • ? ? @Override
  • ? ? public Object[] getSections() {
  • ? ?? ???return alphabetIndexer.getSections();
  • ? ? }

  • ? ? @Override
  • ? ? public int getPositionForSection(int section) {
  • ? ?? ???return alphabetIndexer.getPositionForSection(section);
  • ? ? }

  • ? ? @Override
  • ? ? public int getSectionForPosition(int position) {
  • ? ?? ???return alphabetIndexer.getSectionForPosition(position);
  • ? ? }
  • }
  • 復制代碼 5.跑起來
    ? ???提供樣本數(shù)據(jù)如下:
  • public List<Map<String, String>> getData(){
  • ? ? List<Map<String, String>> itemList = new ArrayList<Map<String, String>>();
  • ? ? String alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  • ? ??
  • ? ? Map<String, String> map = null;
  • ? ? for(char c:alphas.toCharArray()){
  • ? ?? ???for(int i=0; i<10; i++){? ?? ?? ?? ?? ??
  • ? ?? ?? ?? ?map = new HashMap<String, String>();
  • ? ?? ?? ?? ?map.put("itemText", ""+c+i);
  • ? ?? ?? ?? ?itemList.add(map);
  • ? ?? ???}
  • ? ? }

  • ? ? return itemList;
  • }
  • 復制代碼  子項的布局文件:
  • <?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="50dip"
  • ? ? android:gravity="center_vertical"
  • ? ? >
  • ? ? <TextView?
  • ? ?? ???android:id="@+id/tweaked_item_text"
  • ? ?? ???android:layout_width="fill_parent"?
  • ? ?? ???android:layout_height="wrap_content" />
  • </LinearLayout>
  • 復制代碼   使用并運行:
  • protected void onCreate(Bundle savedInstanceState) {
  • ? ? super.onCreate(savedInstanceState);
  • ? ? setContentView(R.layout.tweake_list);
  • ? ??
  • ? ? tweakedListView = (ListView)findViewById(R.id.tweaked_list);
  • ? ??
  • ? ? //獲取數(shù)據(jù)
  • ? ? List<Map<String, String>> itemList = getData();
  • ? ? ListAdapter adapter = new IndexAdapter(this, itemList, R.layout.tweake_list_item, new String[]{"itemText"}, new int[]{R.id.tweaked_item_text});
  • ? ? tweakedListView.setAdapter(adapter);
  • }
  • 復制代碼 效果如下: 6.小結(jié)
    ? ?? ?這種索引效果,在大數(shù)據(jù)量列表顯示中非常的實用,是Android開發(fā)必備常識。
    ? ?? ?本文只是一個簡單的sample,實際工作中肯定會需要進一步擴展定義:
    ? ?? ?(1).對于復雜類型的處理,可根據(jù)Map<String,?>擴展自定義實體類,再通過adapter轉(zhuǎn)換使用即可。
    ? ?? ?(2).對于索引字母列表,可動態(tài)設(shè)置,舉個例子,你的列表只有ABCD四個字母,如果索引字母列表還是設(shè)置“ABCDEFGHIJKLMNOPQRSTUVWXYZ”就不合適了,會有個索引偏位的問題。
    ? ?? ?(3).對于復雜界面的顯示,可重寫adapter的getView方法自定義視圖。 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Android App列表之游标ListView(索引ListView)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。