Android ApiDemos示例解析(173):Views->Lists->6. ListAdapter Collapsed

来源:互联网 发布:nginx不支持中文路径 编辑:程序博客网 时间:2024/05/08 01:48

本例使用自定义的ListAdapter 实现了类似Expandable Lists 的功能 Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter

使用的自定义View SpeechView ,功能和例子Android ApiDemos示例解析(171):Views->Lists->4. ListAdapter类似,这个View由两个TextView构成,一个TextView显示Title,一个View显示内容,所不同是,它增加一个方法setExpanded ,可以控制显示内容的TextView的可见性:

public void setExpanded(boolean expanded) { mDialogue.setVisibility(expanded ? VISIBLE : GONE);}

类SpeechListAdapter 增加一个方法,用于改变列表项的可见性,类似于MVC改变了Model 的值,此时需要调用notifyDataSetChanged()通知View数据源发生了变化以更新UI。

/** * Make a SpeechView to hold each row. * @see android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup) */public View getView(int position, View convertView, ViewGroup parent) { SpeechView sv; if (convertView == null) { sv = new SpeechView(mContext, mTitles[position], mDialogue[position], mExpanded[position]); } else { sv = (SpeechView)convertView; sv.setTitle(mTitles[position]); sv.setDialogue(mDialogue[position]); sv.setExpanded(mExpanded[position]); } return sv;}public void toggle(int position) { mExpanded[position] = !mExpanded[position]; notifyDataSetChanged();}

从这个例子也可以看到ListActivity非常灵活,列表项可以使用不同的View,理论上不同的列表项可以选择任意的View来显示。

原创粉丝点击