這個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來顯示。