Android ApiDemos示例解析(173):Views->Lists->6. ListAdapter Collapsed
这个View由两个TextView构成,一个TextView显示Title,一个View显示内容,所不同是,它增加一个方法setExpanded ,可以控制显示内容的TextView的可见性:
[java]
public void setExpanded(boolean expanded) {
mDialogue.setVisibility(expanded ? VISIBLE : GONE);
}
public void setExpanded(boolean expanded) {
mDialogue.setVisibility(expanded ? VISIBLE : GONE);
}
类SpeechListAdapter 增加一个方法,用于改变列表项的可见性,类似于MVC改变了Model 的值,此时需要调用notifyDataSetChanged()通知View数据源发生了变化以更新UI。
[java]
/**
* 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();
}
/**
* 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来显示。
补充:移动开发 , Android ,