使得ListView能够呈现多种布局的多布局适配器

移动开发 Android
ListView用了SimpleAdapter之后就只能呈现一种Layout,这样虽然简单但是有时不能满足需求。这里重写SimpleAdapter,可以接受Resource数组,能够适配多种布局。

ListView用了SimpleAdapter之后就只能呈现一种Layout,这样虽然简单但是有时不能满足需求。所以,我下载SDK的源码重写了SimpleAdapter,你可以看出那些JavaDoc还是之前SimpleAdapter的JavaDoc。

各位下载了之后能将它当成SimpleAdapter使用。

主要修改的地方是:

1、构造方法不再接受单个Layout Resource,能接受Resource数组。

2、能够根据 ItemViewType来选择Resource,所以子类应该要重写 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.  * 这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图. 
  39.  * 你可以将 Maps 的 ArrayList 指定为用于列表的数据.ArrayList 中的每一项对应列表中的一行. 
  40.  * Maps 中包含用于一行的数据.你也可以指定 XML 文件,其中定义了用于显示行的视图,通过 
  41.  * Map 的关键字映射到指定的视图. 
  42.  * 绑定数据到视图分两个阶段.首先,如果 {@link android.widget.SimpleAdapter.ViewBinder} 是有效的, 
  43.  * 则调用 {@link ViewBinder#setViewValue(android.view.View, Object, String)} 方法. 
  44.  * 如果返回值为真,则执行绑定.如果返回值为假,则按以下顺序绑定视图: 
  45.  * 
       
    •  * 
    •  实现了 Checkable 的视图(例如 CheckBox),期望绑定值是布尔类型. 
    •  * 
    •  TextView,期望绑定值是字符串类型,通过调用 {@link #setViewText(TextView, String)} 绑定. 
    •  * 
    •  ImageView,期望绑定值是资源 ID 或者一个字符串,通过调用 
    •  * {@link #setViewImage(ImageView, int)} 或 {@link #setViewImage(ImageView, String)}绑定. 
    •  * 
     
  46.  * 如果没有合适的绑定发生,将会抛出 {@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.      * 构造函数 
  66.      * 
  67.      * @param context 与 SimpleAdapter 关联的运行着的视图的上下文. 
  68.      * @param data Map 的列表.列表中的每个条目对应一行.Maps 中包含所有在 from 中指定的数据. 
  69.      * @param resource 定义列表项目的视图布局的资源 ID.布局文件至少应该包含在 to 中定义了的名称. 
  70.      * @param from 与 Map 中的项目建立关联的列名的列表. 
  71.      * @param to 用于显示 from 中参数中的列的视图列表.这些视图应该都是 TextView 类型的. 
  72.      * 该列表中的第 N 个视图显示从参数 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.      * 

    设置创建下拉列表视图的布局资源 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.      * 返回用于将数据绑定到视图的 {@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.      * 设置用于将数据绑定到视图的绑定器. 
  222.      * 
  223.      * @param viewBinder 用于将数据绑定到视图的绑定器.设置为 null,可以删除既存的绑定器. 
  224.      * 
  225.      * @see #getViewBinder() 
  226.      */ 
  227.     public void setViewBinder(ViewBinder viewBinder) { 
  228.         mViewBinder = viewBinder; 
  229.     } 
  230.  
  231.     /** 
  232.      * 由 bindView() 方法调用,用于为 ImageView 设置图像.只在 
  233.      * ViewBinder 不存在或者既存的 ViewBinder 无法处理 ImageView 的绑定时才调用. 
  234.      * 
  235.      * 如果调用 {@link #setViewImage(ImageView, String)} 方法时提供的 
  236.      * value 参数可以转换为整数类型,则会自动调用本方法. 
  237.      * 
  238.      * @param v 接收图像的 ImageView. 
  239.      * @param value 从数据集获取到的值 
  240.      * 
  241.      * @see #setViewImage(ImageView, String) 
  242.      */ 
  243.     public void setViewImage(ImageView v, int value) { 
  244.         v.setImageResource(value); 
  245.     } 
  246.  
  247.     /** 
  248.      * 由 bindView() 方法调用,用于为 ImageView 设置图像.只在 
  249.      * ViewBinder 不存在或者既存的 ViewBinder 无法处理 ImageView 的绑定时才调用. 
  250.      * 
  251.      * 本方法默认将 value 作为图像资源 ID 来对待;当 value 
  252.      * 无法变换为整数类型时,才作为图像的 Uri 来使用. 
  253.      * 
  254.      * @param v 接收图像的 ImageView. 
  255.      * @param value 从数据集获取到的值. 
  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() 方法调用,用于为 TextView 设置文本.只在 
  269.      * ViewBinder 不存在或者既存的 ViewBinder 无法处理 TextView 的绑定时才调用. 
  270.      * 
  271.      * @param v 接收文本的 TextView. 
  272.      * @param text 设置到 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.          * 绑定指定的数据到指定的视图. 
  298.          * 
  299.          * 当使用 ViewBinder 绑定了数据时,必须返回真.如果该方法返回假, 
  300.          * SimpleAdapter 会用自己的方式去绑定数据. 
  301.          * 
  302.          * @param view 要绑定数据的视图 
  303.          * @param data 绑定用的数据 
  304.          * @param textRepresentation 代表所提供的数据的安全字符串: 
  305.          *        或者是 data.toString(),或者是空串,不能为空. 
  306.          * 
  307.          * @return 如果将数据绑定到了视图,返回真;否则返回假. 
  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.     } 

 

责任编辑:徐川 来源: OSChina
相关推荐

2012-09-19 15:29:26

Worklight适配器

2018-10-11 10:38:31

前端JavaScript编程语言

2022-02-18 17:21:29

适配器模式客户端

2020-10-25 08:56:21

适配器模式

2015-08-07 10:05:37

recyclervie超省写法

2011-04-28 09:54:50

jQuery

2010-05-05 22:04:08

万兆以太网融合网络Brocade

2009-12-21 10:26:09

Oracle适配器

2009-11-18 18:08:20

PHP适配器模式

2012-05-16 17:22:11

Java设计模式

2022-02-13 23:33:24

设计模式Java

2021-02-16 08:16:09

适配器模式MybatisJava

2021-08-06 06:51:16

适配器配置Spring

2013-11-26 16:39:21

Android设计模式

2021-02-18 08:39:28

设计模式场景

2010-07-09 12:53:30

HART协议

2014-12-17 09:57:01

AndroidAdapteViewHolder

2013-02-26 10:55:47

C#适配器设计模式

2012-08-02 10:46:34

JavaAdapter模式

2010-09-02 17:04:52

DHCP服务器
点赞
收藏

51CTO技术栈公众号