In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how multi-layout adapters enable ListView to present multiple layouts. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
After using SimpleAdapter, ListView can only present one kind of Layout, which is simple but sometimes does not meet the requirements. So, I downloaded the source code of SDK and rewrote SimpleAdapter, and you can see that those JavaDoc are still the JavaDoc of the previous SimpleAdapter.
After you download it, you can use it as SimpleAdapter.
The main changes are as follows:
1. The constructor no longer accepts a single Layout Resource, but can accept an Resource array.
2. Resource can be selected according to ItemViewType, so subclasses should override getItemViewType and getViewTypeCount (optional).
The / * * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "ASIS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * / package android.widget; import java.util.ArrayList; import java.util.List; import java.util.Map; import android.content.Context; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Checkable; import android.widget.Filter; import android.widget.Filterable; import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView / * this is a simple adapter that maps static data to the view defined in the XML file. * you can specify the ArrayList of Maps as the data for the list. Each item in the ArrayList corresponds to a row in the list. * Maps contains data for one row. You can also specify the XML file, which defines the view used to display lines and maps to the specified view through the keyword * Map. * binding data to the view is divided into two phases. First, if {@ link android.widget.SimpleAdapter.ViewBinder} is valid, * the {@ link ViewBinder#setViewValue (android.view.View, Object, String)} method is called. * if the return value is true, bind is performed. If the return value is false, the views are bound in the following order: * * implements the view of Checkable (for example, CheckBox), and the expected bound value is of Boolean type. * TextView, expecting the binding value to be a string type, bound by calling {@ link # setViewText (TextView, String)}. * ImageView. The expected binding value is a resource ID or a string, which is bound by calling * {@ link # setViewImage (ImageView, int)} or {@ link # setViewImage (ImageView, String)}. * * if no appropriate binding occurs, a {@ link IllegalStateException} exception will be thrown. * @ author translate by Drode * @ author convert by cnmahj * / public class MultiLayoutSimpleAdapter extends BaseAdapter implements Filterable {private int [] mTo; private String [] mFrom; private ViewBinder mViewBinder; protected Listextends Map private int [] mResources; private int [] mDropDownResources; private LayoutInflater mInflater; private SimpleFilter mFilter; private ArrayList / * constructor * * @ param context the context of the running view associated with the SimpleAdapter. List of * @ param data Map. Each entry in the list corresponds to a line. The Maps contains all the data specified in the from. * @ param resource defines the resource ID for the view layout of the list project. The layout file should contain at least the name defined in the to. * @ param from list of column names associated with items in Map. * @ param to is used to display a view list of columns in parameters in from. These views should all be of type TextView. * the Nth view in the list displays the value obtained from the N column of the parameter from. * / public MultiLayoutSimpleAdapter (Context context, Listextends Map int [] resources, String [] from, int [] to) {mData = data; mResources = mDropDownResources = resources; mFrom = from; mTo = to; mInflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);} @ Override public int getViewTypeCount () {return mResources.length } / * * @ see android.widget.Adapter#getCount () * / public int getCount () {return mData.size ();} / * @ see android.widget.Adapter#getItem (int) * / public Object getItem (int position) {return mData.get (position) } / * @ see android.widget.Adapter#getItemId (int) * / public long getItemId (int position) {return position;} / * @ see android.widget.Adapter#getView (int, View, ViewGroup) * / public View getView (int position, View convertView, ViewGroup parent) {return createViewFromResource (position, convertView, parent, mResources [getItemViewType (position)]) } private View createViewFromResource (int position, View convertView, ViewGroup parent, int resource) {View v; if (convertView = = null) {v = mInflater.inflate (resource, parent, false);} else {v = convertView;} bindView (position, v); return v } / * set the layout resource ID that creates the drop-down list view. * * @ param resource defines the layout resource ID. * @ see # getDropDownView (int, android.view.View, android.view.ViewGroup) * / public void setDropDownViewResource (int [] resources) {this.mDropDownResources = resources } @ Override public View getDropDownView (int position, View convertView, ViewGroup parent) {return createViewFromResource (position, convertView, parent, mResources [getItemViewType (position)]);} private void bindView (int position, View view) {final Map if (dataSet = = null) {return;} final ViewBinder binder = mViewBinder; final String [] from = mFrom Final int [] to = mTo; final int count = to.length; for (int I = 0; I
< count; i++) { final View v = view.findViewById(to[i]); if (v != null) { final Object data = dataSet.get(from[i]); String text = data == null ? "" : data.toString(); if (text == null) { text = ""; } boolean bound = false; if (binder != null) { bound = binder.setViewValue(v, data, text); } if (!bound) { if (v instanceof Checkable) { if (data instanceof Boolean) { ((Checkable) v).setChecked((Boolean) data); } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text); } else { throw new IllegalStateException(v.getClass().getName() + " should be bound to a Boolean, not a " + (data == null ? " : data.getClass())); } } else if (v instanceof TextView) { // Note: keep the instanceof TextView check at the bottom of these // ifs since a lot of views are TextViews (e.g. CheckBoxes). setViewText((TextView) v, text); } else if (v instanceof ImageView) { if (data instanceof Integer) { setViewImage((ImageView) v, (Integer) data); } else { setViewImage((ImageView) v, text); } } else if (v instanceof Spinner) { if (data instanceof Integer) { ((Spinner)v).setSelection((Integer) data); } else { continue; } } else { throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleAdapter"); } } } } } /** * 返回用于将数据绑定到视图的 {@link ViewBinder}. * * @return ViewBinder,如果绑定器不存在则返回 null. * * @see #setViewBinder(android.widget.SimpleAdapter.ViewBinder) */ public ViewBinder getViewBinder() { return mViewBinder; } /** * 设置用于将数据绑定到视图的绑定器. * * @param viewBinder 用于将数据绑定到视图的绑定器.设置为 null,可以删除既存的绑定器. * * @see #getViewBinder() */ public void setViewBinder(ViewBinder viewBinder) { mViewBinder = viewBinder; } /** * 由 bindView() 方法调用,用于为 ImageView 设置图像.只在 * ViewBinder 不存在或者既存的 ViewBinder 无法处理 ImageView 的绑定时才调用. * * 如果调用 {@link #setViewImage(ImageView, String)} 方法时提供的 * value 参数可以转换为整数类型,则会自动调用本方法. * * @param v 接收图像的 ImageView. * @param value 从数据集获取到的值 * * @see #setViewImage(ImageView, String) */ public void setViewImage(ImageView v, int value) { v.setImageResource(value); } /** * 由 bindView() 方法调用,用于为 ImageView 设置图像.只在 * ViewBinder 不存在或者既存的 ViewBinder 无法处理 ImageView 的绑定时才调用. * * 本方法默认将 value 作为图像资源 ID 来对待;当 value * 无法变换为整数类型时,才作为图像的 Uri 来使用. * * @param v 接收图像的 ImageView. * @param value 从数据集获取到的值. * * @see #setViewImage(ImageView, int) */ public void setViewImage(ImageView v, String value) { try { v.setImageResource(Integer.parseInt(value)); } catch (NumberFormatException nfe) { v.setImageURI(Uri.parse(value)); } } /** * 由 bindView() 方法调用,用于为 TextView 设置文本.只在 * ViewBinder 不存在或者既存的 ViewBinder 无法处理 TextView 的绑定时才调用. * * @param v 接收文本的 TextView. * @param text 设置到 TextView 的文本. */ public void setViewText(TextView v, String text) { v.setText(text); } public Filter getFilter() { if (mFilter == null) { mFilter = new SimpleFilter(); } return mFilter; } /** * 该类用于 SimpleAdapter 的外部客户将适配器的值绑定到视图. * * 你可以使用此类将 SimpleAdapter 不支持的值绑定到视图,或者改变 SimpleAdapter * 支持的视图的绑定方式. * * @see MultiLayoutSimpleAdapter#setViewImage(ImageView, int) * @see MultiLayoutSimpleAdapter#setViewImage(ImageView, String) * @see MultiLayoutSimpleAdapter#setViewText(TextView, String) */ public static interface ViewBinder { /** * 绑定指定的数据到指定的视图. * * 当使用 ViewBinder 绑定了数据时,必须返回真.如果该方法返回假, * SimpleAdapter 会用自己的方式去绑定数据. * * @param view 要绑定数据的视图 * @param data 绑定用的数据 * @param textRepresentation 代表所提供的数据的安全字符串: * 或者是 data.toString(),或者是空串,不能为空. * * @return 如果将数据绑定到了视图,返回真;否则返回假. */ boolean setViewValue(View view, Object data, String textRepresentation); } /** * An array filters constrains the content of the array adapter with * a prefix. Each item that does not start with the supplied prefix * is removed from the list. */ private class SimpleFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence prefix) { FilterResults results = new FilterResults(); if (mUnfilteredData == null) { mUnfilteredData = new ArrayList } if (prefix == null || prefix.length() == 0) { ArrayList results.values = list; results.count = list.size(); } else { String prefixString = prefix.toString().toLowerCase(); ArrayList int count = unfilteredValues.size(); ArrayList for (int i = 0; i < count; i++) { Map if (h != null) { int len = mTo.length; for (int j=0; j String str = (String)h.get(mFrom[j]); String[] words = str.split(" "); int wordCount = words.length; for (int k = 0; k < wordCount; k++) { String word = words[k]; if (word.toLowerCase().startsWith(prefixString)) { newValues.add(h); break; } } } } } results.values = newValues; results.count = newValues.size(); } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { //noinspection unchecked mData = (List if (results.count >0) {notifyDataSetChanged ();} else {notifyDataSetInvalidated ();} above is how multiple layout adapters enable ListView to render multiple layouts. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.