自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使得ListView能夠呈現(xiàn)多種布局的多布局適配器

移動(dòng)開發(fā) Android
ListView用了SimpleAdapter之后就只能呈現(xiàn)一種Layout,這樣雖然簡單但是有時(shí)不能滿足需求。這里重寫SimpleAdapter,可以接受Resource數(shù)組,能夠適配多種布局。

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

各位下載了之后能將它當(dāng)成SimpleAdapter使用。

主要修改的地方是:

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

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

感謝各位的使用與支持!

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

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

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

    An array filters constrains the content of the array adapter with 

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

     

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

 

責(zé)任編輯:徐川 來源: OSChina
相關(guān)推薦

2012-09-19 15:29:26

Worklight適配器

2015-08-07 10:05:37

recyclervie超省寫法

2018-10-11 10:38:31

前端JavaScript編程語言

2022-02-18 17:21:29

適配器模式客戶端

2020-10-25 08:56:21

適配器模式

2022-02-13 23:33:24

設(shè)計(jì)模式Java

2021-02-16 08:16:09

適配器模式MybatisJava

2021-08-06 06:51:16

適配器配置Spring

2011-04-28 09:54:50

jQuery

2013-11-26 16:39:21

Android設(shè)計(jì)模式

2021-02-18 08:39:28

設(shè)計(jì)模式場景

2012-05-16 17:22:11

Java設(shè)計(jì)模式

2009-11-18 18:08:20

PHP適配器模式

2009-12-21 10:26:09

Oracle適配器

2010-05-05 22:04:08

萬兆以太網(wǎng)融合網(wǎng)絡(luò)Brocade

2012-08-02 10:46:34

JavaAdapter模式

2010-07-09 12:53:30

HART協(xié)議

2014-12-17 09:57:01

AndroidAdapteViewHolder

2013-02-26 10:55:47

C#適配器設(shè)計(jì)模式

2012-12-10 10:53:04

IBMdW
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號