Android ApiDemos樣本解析(173):Views->Lists->6. Lis

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.