日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使得ListView能夠呈現(xiàn)多種布局的多布局適配器

ListView用了SimpleAdapter之后就只能呈現(xiàn)一種Layout,這樣雖然簡單但是有時不能滿足需求。所以,我下載SDK的源碼重寫了SimpleAdapter,你可以看出那些JavaDoc還是之前SimpleAdapter的JavaDoc。

各位下載了之后能將它當成SimpleAdapter使用。

主要修改的地方是:

1、構造方法不再接受單個Layout Resource,能接受Resource數(shù)組。

2、能夠根據(jù) ItemViewType來選擇Resource,所以子類應該要重寫 getItemViewType 和 getViewTypeCount(可選)。

感謝各位的使用與支持!

 
 
  1. /* 
  2.  * Copyright (C) 2006 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */ 
  16.  
  17. package android.widget; 
  18.  
  19. import java.util.ArrayList; 
  20. import java.util.List; 
  21. import java.util.Map; 
  22.  
  23. import android.content.Context; 
  24. import android.net.Uri; 
  25. import android.view.LayoutInflater; 
  26. import android.view.View; 
  27. import android.view.ViewGroup; 
  28. import android.widget.BaseAdapter; 
  29. import android.widget.Checkable; 
  30. import android.widget.Filter; 
  31. import android.widget.Filterable; 
  32. import android.widget.ImageView; 
  33. import android.widget.Spinner; 
  34. import android.widget.TextView; 
  35.  
  36. /** 
  37.  * 這是一個簡單的適配器,可以將靜態(tài)數(shù)據(jù)映射到XML文件中定義好的視圖. 
  38.  * 你可以將 Maps 的 ArrayList 指定為用于列表的數(shù)據(jù).ArrayList 中的每一項對應列表中的一行. 
  39.  * Maps 中包含用于一行的數(shù)據(jù).你也可以指定 XML 文件,其中定義了用于顯示行的視圖,通過 
  40.  * Map 的關鍵字映射到指定的視圖. 
  41.  * 綁定數(shù)據(jù)到視圖分兩個階段.首先,如果 {@link android.widget.SimpleAdapter.ViewBinder} 是有效的, 
  42.  * 則調用 {@link ViewBinder#setViewValue(android.view.View, Object, String)} 方法. 
  43.  * 如果返回值為真,則執(zhí)行綁定.如果返回值為假,則按以下順序綁定視圖: 
  44.  * 
       
    •  * 
    •  實現(xiàn)了 Checkable 的視圖(例如 CheckBox),期望綁定值是布爾類型. 
    •  * 
    •  TextView,期望綁定值是字符串類型,通過調用 {@link #setViewText(TextView, String)} 綁定. 
    •  * 
    •  ImageView,期望綁定值是資源 ID 或者一個字符串,通過調用 
    •  * {@link #setViewImage(ImageView, int)} 或 {@link #setViewImage(ImageView, String)}綁定. 
    •  * 
     
  45.  * 如果沒有合適的綁定發(fā)生,將會拋出 {@link IllegalStateException} 異常. 
  46.  * @author translate by 德羅德 
  47.  * @author convert by cnmahj 
  48.  */ 
  49. public class MultiLayoutSimpleAdapter extends BaseAdapter implements Filterable { 
  50.     private int[] mTo; 
  51.     private String[] mFrom; 
  52.     private ViewBinder mViewBinder; 
  53.  
  54.     protected List extends Map
  55.  
  56.     private int[] mResources; 
  57.     private int[] mDropDownResources; 
  58.     private LayoutInflater mInflater; 
  59.  
  60.     private SimpleFilter mFilter; 
  61.     private ArrayList
  62.  
  63.     /** 
  64.      * 構造函數(shù) 
  65.      * 
  66.      * @param context 與 SimpleAdapter 關聯(lián)的運行著的視圖的上下文. 
  67.      * @param data Map 的列表.列表中的每個條目對應一行.Maps 中包含所有在 from 中指定的數(shù)據(jù). 
  68.      * @param resource 定義列表項目的視圖布局的資源 ID.布局文件至少應該包含在 to 中定義了的名稱. 
  69.      * @param from 與 Map 中的項目建立關聯(lián)的列名的列表. 
  70.      * @param to 用于顯示 from 中參數(shù)中的列的視圖列表.這些視圖應該都是 TextView 類型的. 
  71.      * 該列表中的第 N 個視圖顯示從參數(shù) from 中的第 N 列獲取的值. 
  72.      */ 
  73.     public MultiLayoutSimpleAdapter(Context context, List extends Map
  74.             int[] resources, String[] from, int[] to) { 
  75.         mData = data; 
  76.         mResources = mDropDownResources = resources; 
  77.         mFrom = from; 
  78.         mTo = to; 
  79.         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  80.     } 
  81.  
  82.     @Override 
  83.     public int getViewTypeCount() { 
  84.         return mResources.length; 
  85.     } 
  86.  
  87.     /** 
  88.      * @see android.widget.Adapter#getCount() 
  89.      */ 
  90.     public int getCount() { 
  91.         return mData.size(); 
  92.     } 
  93.  
  94.     /** 
  95.      * @see android.widget.Adapter#getItem(int) 
  96.      */ 
  97.     public Object getItem(int position) { 
  98.         return mData.get(position); 
  99.     } 
  100.  
  101.     /** 
  102.      * @see android.widget.Adapter#getItemId(int) 
  103.      */ 
  104.     public long getItemId(int position) { 
  105.         return position; 
  106.     } 
  107.  
  108.     /** 
  109.      * @see android.widget.Adapter#getView(int, View, ViewGroup) 
  110.      */ 
  111.     public View getView(int position, View convertView, ViewGroup parent) { 
  112.         return createViewFromResource(position, convertView, parent, mResources[getItemViewType(position)]); 
  113.     } 
  114.      
  115.  
  116.     private View createViewFromResource(int position, View convertView, 
  117.             ViewGroup parent, int resource) { 
  118.         View v; 
  119.         if (convertView == null) { 
  120.             v = mInflater.inflate(resource, parent, false); 
  121.         } else { 
  122.             v = convertView; 
  123.         } 
  124.  
  125.         bindView(position, v); 
  126.  
  127.         return v; 
  128.     } 
  129.  
  130.     /** 
  131.      * 

    設置創(chuàng)建下拉列表視圖的布局資源 ID.

     
  132.      * 
  133.      * @param resource 定義下拉列表視圖的布局資源 ID. 
  134.      * @see #getDropDownView(int, android.view.View, android.view.ViewGroup) 
  135.      */ 
  136.     public void setDropDownViewResource(int[] resources) { 
  137.         this.mDropDownResources = resources; 
  138.     } 
  139.  
  140.     @Override 
  141.     public View getDropDownView(int position, View convertView, ViewGroup parent) { 
  142.         return createViewFromResource(position, convertView, parent, mResources[getItemViewType(position)]); 
  143.     } 
  144.  
  145.     private void bindView(int position, View view) { 
  146.         final Map
  147.         if (dataSet == null) { 
  148.             return; 
  149.         } 
  150.  
  151.         final ViewBinder binder = mViewBinder; 
  152.         final String[] from = mFrom; 
  153.         final int[] to = mTo; 
  154.         final int count = to.length; 
  155.  
  156.         for (int i = 0; i < count; i++) { 
  157.             final View v = view.findViewById(to[i]); 
  158.             if (v != null) { 
  159.                 final Object data = dataSet.get(from[i]); 
  160.                 String text = data == null ? "" : data.toString(); 
  161.                 if (text == null) { 
  162.                     text = ""; 
  163.                 } 
  164.  
  165.                 boolean bound = false; 
  166.                 if (binder != null) { 
  167.                     bound = binder.setViewValue(v, data, text); 
  168.                 } 
  169.  
  170.                 if (!bound) { 
  171.                     if (v instanceof Checkable) { 
  172.                         if (data instanceof Boolean) { 
  173.                             ((Checkable) v).setChecked((Boolean) data); 
  174.                         } else if (v instanceof TextView) { 
  175.                             // Note: keep the instanceof TextView check at the bottom of these 
  176.                             // ifs since a lot of views are TextViews (e.g. CheckBoxes). 
  177.                             setViewText((TextView) v, text); 
  178.                         } else { 
  179.                             throw new IllegalStateException(v.getClass().getName() + 
  180.                                     " should be bound to a Boolean, not a " + 
  181.                                     (data == null ? " : data.getClass())); 
  182.                         } 
  183.                     } else if (v instanceof TextView) { 
  184.                         // Note: keep the instanceof TextView check at the bottom of these 
  185.                         // ifs since a lot of views are TextViews (e.g. CheckBoxes). 
  186.                         setViewText((TextView) v, text); 
  187.                     } else if (v instanceof ImageView) { 
  188.                         if (data instanceof Integer) { 
  189.                             setViewImage((ImageView) v, (Integer) data); 
  190.                         } else { 
  191.                             setViewImage((ImageView) v, text); 
  192.                         } 
  193.                     } else if (v instanceof Spinner) { 
  194.                         if (data instanceof Integer) { 
  195.                             ((Spinner)v).setSelection((Integer) data); 
  196.                         } else { 
  197.                             continue; 
  198.                         } 
  199.                     } else { 
  200.                         throw new IllegalStateException(v.getClass().getName() + " is not a " + 
  201.                                 " view that can be bounds by this SimpleAdapter"); 
  202.                     } 
  203.                 } 
  204.             } 
  205.         } 
  206.     } 
  207.  
  208.     /** 
  209.      * 返回用于將數(shù)據(jù)綁定到視圖的 {@link ViewBinder}. 
  210.      * 
  211.      * @return ViewBinder,如果綁定器不存在則返回 null. 
  212.      * 
  213.      * @see #setViewBinder(android.widget.SimpleAdapter.ViewBinder) 
  214.      */ 
  215.     public ViewBinder getViewBinder() { 
  216.         return mViewBinder; 
  217.     } 
  218.  
  219.     /** 
  220.      * 設置用于將數(shù)據(jù)綁定到視圖的綁定器. 
  221.      * 
  222.      * @param viewBinder 用于將數(shù)據(jù)綁定到視圖的綁定器.設置為 null,可以刪除既存的綁定器. 
  223.      * 
  224.      * @see #getViewBinder() 
  225.      */ 
  226.     public void setViewBinder(ViewBinder viewBinder) { 
  227.         mViewBinder = viewBinder; 
  228.     } 
  229.  
  230.     /** 
  231.      * 由 bindView() 方法調用,用于為 ImageView 設置圖像.只在 
  232.      * ViewBinder 不存在或者既存的 ViewBinder 無法處理 ImageView 的綁定時才調用. 
  233.      * 
  234.      * 如果調用 {@link #setViewImage(ImageView, String)} 方法時提供的 
  235.      * value 參數(shù)可以轉換為整數(shù)類型,則會自動調用本方法. 
  236.      * 
  237.      * @param v 接收圖像的 ImageView. 
  238.      * @param value 從數(shù)據(jù)集獲取到的值 
  239.      * 
  240.      * @see #setViewImage(ImageView, String) 
  241.      */ 
  242.     public void setViewImage(ImageView v, int value) { 
  243.         v.setImageResource(value); 
  244.     } 
  245.  
  246.     /** 
  247.      * 由 bindView() 方法調用,用于為 ImageView 設置圖像.只在 
  248.      * ViewBinder 不存在或者既存的 ViewBinder 無法處理 ImageView 的綁定時才調用. 
  249.      * 
  250.      * 本方法默認將 value 作為圖像資源 ID 來對待;當 value 
  251.      * 無法變換為整數(shù)類型時,才作為圖像的 Uri 來使用. 
  252.      * 
  253.      * @param v 接收圖像的 ImageView. 
  254.      * @param value 從數(shù)據(jù)集獲取到的值. 
  255.      * 
  256.      * @see #setViewImage(ImageView, int) 
  257.      */ 
  258.     public void setViewImage(ImageView v, String value) { 
  259.         try { 
  260.             v.setImageResource(Integer.parseInt(value)); 
  261.         } catch (NumberFormatException nfe) { 
  262.             v.setImageURI(Uri.parse(value)); 
  263.         } 
  264.     } 
  265.  
  266.     /** 
  267.      * 由 bindView() 方法調用,用于為 TextView 設置文本.只在 
  268.      * ViewBinder 不存在或者既存的 ViewBinder 無法處理 TextView 的綁定時才調用. 
  269.      * 
  270.      * @param v 接收文本的 TextView. 
  271.      * @param text 設置到 TextView 的文本. 
  272.      */ 
  273.     public void setViewText(TextView v, String text) { 
  274.         v.setText(text); 
  275.     } 
  276.  
  277.     public Filter getFilter() { 
  278.         if (mFilter == null) { 
  279.             mFilter = new SimpleFilter(); 
  280.         } 
  281.         return mFilter; 
  282.     } 
  283.  
  284.     /** 
  285.      * 該類用于 SimpleAdapter 的外部客戶將適配器的值綁定到視圖. 
  286.      * 
  287.      * 你可以使用此類將 SimpleAdapter 不支持的值綁定到視圖,或者改變 SimpleAdapter 
  288.      * 支持的視圖的綁定方式. 
  289.      * 
  290.      * @see MultiLayoutSimpleAdapter#setViewImage(ImageView, int) 
  291.      * @see MultiLayoutSimpleAdapter#setViewImage(ImageView, String) 
  292.      * @see MultiLayoutSimpleAdapter#setViewText(TextView, String) 
  293.      */ 
  294.     public static interface ViewBinder { 
  295.         /** 
  296.          * 綁定指定的數(shù)據(jù)到指定的視圖. 
  297.          * 
  298.          * 當使用 ViewBinder 綁定了數(shù)據(jù)時,必須返回真.如果該方法返回假, 
  299.          * SimpleAdapter 會用自己的方式去綁定數(shù)據(jù). 
  300.          * 
  301.          * @param view 要綁定數(shù)據(jù)的視圖 
  302.          * @param data 綁定用的數(shù)據(jù) 
  303.          * @param textRepresentation 代表所提供的數(shù)據(jù)的安全字符串: 
  304.          *        或者是 data.toString(),或者是空串,不能為空. 
  305.          * 
  306.          * @return 如果將數(shù)據(jù)綁定到了視圖,返回真;否則返回假. 
  307.          */ 
  308.         boolean setViewValue(View view, Object data, String textRepresentation); 
  309.     } 
  310.  
  311.     /** 
  312.      * 

    An array filters constrains the content of the array adapter with 

  313.      * a prefix. Each item that does not start with the supplied prefix 
  314.      * is removed from the list.

     

     
  315.      */ 
  316.     private class SimpleFilter extends Filter { 
  317.  
  318.         @Override 
  319.         protected FilterResults performFiltering(CharSequence prefix) { 
  320.             FilterResults results = new FilterResults(); 
  321.  
  322.             if (mUnfilteredData == null) { 
  323.                 mUnfilteredData = new ArrayList
  324.             } 
  325.  
  326.             if (prefix == null || prefix.length() == 0) { 
  327.                 ArrayList
  328.                 results.values = list; 
  329.                 results.count = list.size(); 
  330.             } else { 
  331.                 String prefixString = prefix.toString().toLowerCase(); 
  332.  
  333.                 ArrayList
  334.                 int count = unfilteredValues.size(); 
  335.  
  336.                 ArrayList
  337.  
  338.                 for (int i = 0; i < count; i++) { 
  339.                     Map
  340.                     if (h != null) { 
  341.  
  342.                         int len = mTo.length; 
  343.  
  344.                         for (int j=0; j
  345.                             String str =  (String)h.get(mFrom[j]); 
  346.  
  347.                             String[] words = str.split(" "); 
  348.                             int wordCount = words.length; 
  349.  
  350.                             for (int k = 0; k < wordCount; k++) { 
  351.                                 String word = words[k]; 
  352.  
  353.                                 if (word.toLowerCase().startsWith(prefixString)) { 
  354.                                     newValues.add(h); 
  355.                                     break; 
  356.                                 } 
  357.                             } 
  358.                         } 
  359.                     } 
  360.                 } 
  361.  
  362.                 results.values = newValues; 
  363.                 results.count = newValues.size(); 
  364.             } 
  365.  
  366.             return results; 
  367.         } 
  368.  
  369.         @Override 
  370.         protected void publishResults(CharSequence constraint, FilterResults results) { 
  371.             //noinspection unchecked 
  372.             mData = (List
  373.             if (results.count > 0) { 
  374.                 notifyDataSetChanged(); 
  375.             } else { 
  376.                 notifyDataSetInvalidated(); 
  377.             } 
  378.         } 
  379.     } 

本文名稱:使得ListView能夠呈現(xiàn)多種布局的多布局適配器
地址分享:http://m.5511xx.com/article/cddpooo.html