【问题标题】:How to dynamic add image/imagebutton in android listview or gridview如何在 android listview 或 gridview 中动态添加图像/图像按钮
【发布时间】:2012-07-27 04:01:12
【问题描述】:

我有一个情感图标列表(超过 100 个图标)。
我想在一个视图中显示人们可以点击它并为他点击的内容敬酒。
现在,我有一些令人困惑的地方:
1) 我应该使用哪些视图来包含所有这些图标(GridView 或 ListView)?
2)如何动态添加它们或以编程方式添加它们?如果我一一声明,我认为是不行的。但是还有其他想法吗?
所有图像图标都在可绘制文件夹中。 谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    GridView 在这种情况下使用PopupWindow 会更好。获取一个 HashMap 并将笑脸的名称存储为字符串,并将其图像存储为资源 ID。

    例如:-

    HashMap<String, Integer> map = new  HashMap<String, Integer>();
    map.put("smile", R.drawable.smile);
    .... 
    

    现在,遍历 HashMap 的所有值(图像)并从 HashMap 值(图像)填充您的 GridView。

    而且,在 Adapter 类中,您可以使用 map.get(key); 向 ImageView 显示图像并设置资源。

    更新:

    我刚刚为它创建了一个小演示,你可以从here查看它。

    【讨论】:

      【解决方案2】:

      您告诉您需要的视图,网格视图将是更好的选择,并且 您可以在其中添加动态值

      private ArrayList<Photo> mPhotos;
      private BaseAdapter mAdapter;
      private GridView mGridView;
      
      onCreate:
      
      /* other things here */
      
      mAdapter = new MyAdapter(mPhotos);
      mGridView.setAdapter(mAdapter);
      

      // 将新图像添加到列表中

      mPhotos.add(photo);
      mAdapter.notifyDataSetChanged();
      

      看到这个答案会帮助你

      Populating a GridView with ImageViews dynamically/programmatically using a ImageAdapter

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多