如何在LinearLayout中回收视图,以避免多次调用findViewById()?

时间:2013-02-18 16:39:32

标签: java android android-layout optimization

我想动态地将布局XML中的项目列表添加到LinearLayout中的ScrollView。这涉及到每个项目多次调用findViewById,我被告知这是非常昂贵的。如何回收我的观点以避免这种情况?

我会使用ListView,但每个项目中可以包含任意数量的内容和评论元素,我在Google I/O 2010 - The world of ListView中被告知ListView不应该过于复杂。

以下是相关方法的代码:

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());

    }

参数quoteNodeListorg.w3c.dom.NodeListXmlUtilities是我自己编写的辅助类,但方法应该是自我解释的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

  

这涉及到每个项目多次调用findViewById,我被告知非常昂贵。

并不像通胀本身那么昂贵。如果你有10,000条评论,你将膨胀10,000行,这样你就有可能耗尽堆空间和/或冻结你的用户界面太长时间。

  

如何回收我的观点以避免这种情况?

鉴于你的结构,你不能。

  

我会使用ListView,除了每个项目中都可以包含任意数量的内容和注释元素,我在Google I / O 2010中被告知 - ListView的世界不应该过度复杂。

由于您提出的解决方案将更加“过于复杂” - 增加两到三个数量级 - 使用ListView