是否可以在运行时为 ListView 添加不同的视图?

发布于 2024-12-23 12:17:10 字数 1875 浏览 1 评论 0原文

我遇到了一种情况,我需要使 ListView 的每个项目都有不同的视图。例如, 第一个项目可能有一个 TextView 和一个 ImageView,第二个项目可能有一个 Button 和一个 TextView .
我最初的尝试是使用嵌套在项目适配器内的表格布局,但它不起作用。

public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.user_activity_item, null);
            holder = new ItemViewHolder();
            holder.textViewText = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_name);
            holder.textViewText2 = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_where);
            holder.table = (TableLayout) convertView.findViewById(R.id.user_activity_item_xml_tablelayout_activity_table);
            convertView.setTag(holder);
        }
        else {
            holder = (ItemViewHolder) convertView.getTag();
        }

        holder.textViewText.setText(items.get(position));
        holder.textViewText2.setText(items.get(position));

        // add a new row
        TableRow row = new TableRow(context);
        row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
        ImageView i = new ImageView(context);
        TextView t = new TextView(context);
        t.setText("Some texts");
        row.addView(i);
        row.addView(t);
        holder.table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        return convertView;
    }

每次加载列表时,表都会相应地修改。所以物品的数量不断增加。所以我的问题是,有没有办法处理这种情况?我可以做什么来制作一个动态 ListView ,在其中我可以在运行时为每个项目添加另一个小部件?任何建议将不胜感激。

I ran into a situation where I need to make each item of a ListView have different views. For example,
the first item might have a TextView and an ImageView, where the second item might have a Button and a TextView.
My initial attempt was using a table layout nested inside an item adapter, but it doesn't work.

public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.user_activity_item, null);
            holder = new ItemViewHolder();
            holder.textViewText = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_name);
            holder.textViewText2 = (TextView) convertView.findViewById(R.id.user_activity_item_xml_textview_user_where);
            holder.table = (TableLayout) convertView.findViewById(R.id.user_activity_item_xml_tablelayout_activity_table);
            convertView.setTag(holder);
        }
        else {
            holder = (ItemViewHolder) convertView.getTag();
        }

        holder.textViewText.setText(items.get(position));
        holder.textViewText2.setText(items.get(position));

        // add a new row
        TableRow row = new TableRow(context);
        row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
        ImageView i = new ImageView(context);
        TextView t = new TextView(context);
        t.setText("Some texts");
        row.addView(i);
        row.addView(t);
        holder.table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        return convertView;
    }

Each time the list got loaded, the table got modified accordingly. So the number of items keeps increasing. So my question is, is there a way to handle this situation? What can I do to make a dynamic ListView in which I can add another widget at runtime for each item? Any suggestion would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-12-30 12:17:10

在方法开始时设置convertView = null。它会起作用的。

Set convertView = null at the start of method. it will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文