java - 如何回收 LinearLayout 中的 View ,以避免多次调用 findViewById()?

标签 java android android-layout optimization

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

我会使用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());

    }

参数quoteNodeList是一个org.w3c.dom.NodeListXmlUtilities 是我自己编写的一个帮助器类,但方法应该是不言自明的。

非常感谢任何帮助。

最佳答案

This involves calling findViewById numerous times for each item, which I have been told is very expensive.

远没有通货膨胀本身那么昂贵。如果您有 10,000 条评论,则会膨胀 10,000 行,从而使您面临堆空间耗尽和/或 UI 卡住时间过长的风险。

How can I recycle my views to avoid this?

考虑到你的结构,你不能。

I would use a ListView, except each item can have any number of content and comment elements within it and I have been told in Google I/O 2010 - The world of ListView that ListViews should not be overcomplicated.

由于您提出的解决方案将明显更加“过于复杂”(两到三个数量级),因此请使用 ListView

关于java - 如何回收 LinearLayout 中的 View ,以避免多次调用 findViewById()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14941058/

相关文章:

java - 使用从外部类到主类的字符串

android - EditText 如何在没有任何操作栏的情况下激活复制/粘贴弹出窗口?

android - 相对于屏幕宽度在android中缩放图像

java - com.google.android.maps.MapView ClassNotFoundException

java - Android:在ViewAnimator的进出动画之间添加延迟

android - 如何在 Android 中将 UI 小部件放置在多个 Z 排序的表面 View 之上

android - 自定义用户界面不工作

java - 使用 Gradle 在 JUnit 测试中从主程序访问资源

java - 从用 XSD 生成的类生成有效的 XML

java - Sonar runner (ubuntu) 执行失败 : com. mysql.jdbc.Driver