我想動態添加從佈局XML擴展到LinearLayout
內ScrollView
內的項目列表。這涉及到每個項目多次撥打findViewById
,我被告知這些項目非常昂貴。我如何回收我的觀點以避免這種情況?如何在LinearLayout中回收視圖以避免多次調用findViewById()?
我會使用一個ListView
,除了每個項目可以有任何數量的內容和評論元素,我已經在Google I/O 2010 - The world of ListView告訴ListView
s不應該過於複雜。
這裏是我的相關方法的代碼:
private void addQuotes(NodeList quoteNodeList){
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i = 0; i < quoteNodeList.getLength(); i++){
Log.i(getClass().getSimpleName(), "Adding quote number " + i);
Node quoteNode = quoteNodeList.item(i);
// Inflate view to hold multiple content items, single additional content TextView, and multiple comment items
LinearLayout quote = (LinearLayout) layoutInflater.inflate(R.layout.quote, null);
LinearLayout contentList = (LinearLayout) quote.findViewById(R.id.dialog_list);
TextView additionalContent = (TextView) quote.findViewById(R.id.additional_content);
LinearLayout commentList = (LinearLayout) quote.findViewById(R.id.comment_list);
// Get data for content items and add to contentList
NodeList contentNodeList =
XmlUtilities.getChildWithTagName(quoteNode, NetworkHelper.XML_TAG_QUOTE_CONTENT).getChildNodes();
for(int contentIndex = 0; contentIndex < contentNodeList.getLength(); contentIndex++){
Log.i(getClass().getSimpleName(), "Adding content number " + contentIndex + " to quote number " + i);
Node contentItemNode = contentNodeList.item(contentIndex);
// Inflate view to hold name and dialog TextViews
LinearLayout contentItem = (LinearLayout) layoutInflater.inflate(R.layout.dialog_item, null);
TextView nameView = (TextView) contentItem.findViewById(R.id.speaker);
TextView dialogView = (TextView) contentItem.findViewById(R.id.dialog);
// Get data and insert into views
String nameString = XmlUtilities.getChildTextValue(contentItemNode, NetworkHelper.XML_TAG_QUOTE_CONTENT_ITEM_NAME);
String dialogString = XmlUtilities.getChildTextValue(contentItemNode, NetworkHelper.XML_TAG_QUOTE_CONTENT_ITEM_DIALOG);
nameView.setText(nameString + ":");
dialogView.setText("\"" + dialogString + "\"");
// Add to parent view
contentList.addView(contentItem);
}
// Get additional content data and add
String additionalContentString = XmlUtilities.getChildTextValue(
quoteNode, NetworkHelper.XML_TAG_QUOTE_ADDITIONAL_CONTENT);
Log.d(getClass().getSimpleName(), "additionalContentString: " + additionalContentString);
additionalContent.setText(additionalContentString);
// TODO: Get comment data and add
// Add everything to ScrollView
mQuoteList.addView(quote);
Log.d(getClass().getSimpleName(), "additionalContent: " + additionalContent.getText());
}
參數quoteNodeList
是org.w3c.dom.NodeList
。 XmlUtilities
是我自己寫的幫手類,但方法應該是自我解釋。
任何幫助,非常感謝。
這是一個像往常一樣的強烈答案,但它沒有解決:「除了每個項目可以包含任何數量的內容和評論元素」。從嵌套循環判斷,['ExpandableListView'](https://developer.android.com/reference/android/widget/ExpandableListView.html)是更好的選擇? – Sam 2013-02-18 17:07:56
@Sam:「但它沒有解決:」除了每個項目可以包含任何數量的內容和註釋元素「」 - 這只是爲位置使用適當的行佈局,相應地填充它,並在'Adapter'中重寫'getViewTypeCount()'和'getItemViewType()'。 「從嵌套循環來看,也許一個ExpandableListView是更好的選擇?」 - 具有相當特定的用戶界面。如果這是你想要的用戶界面,那肯定會去做。 – CommonsWare 2013-02-18 17:33:58