如何在android中的父级可扩展列表中添加图像?

2023-12-10

是否可以在可扩展列表中自定义子项?


与...一起工作SimpleExpandableListAdapter一点也不简单。这里有一些代码可以帮助您入门,假设您正在使用ExpandableListActivity.

在这个例子中,我们使用标准android.R.layout.simple_expandable_list_item_1对于我们的组标题视图,但我们为子视图使用我们自己的自定义布局。

    // Construct Expandable List
    final String NAME = "name";
    final String IMAGE = "image";
    final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

    final HashMap<String, String> group1 = new HashMap<String, String>();
    group1.put(NAME, "Group 1");
    headerData.add( group1 );

    final HashMap<String, String> group2 = new HashMap<String, String>();
    group2.put(NAME, "Group 2");
    headerData.add( group2);


    final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

    final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
    childData.add(group1data);

    final ArrayList<HashMap<String, Object>> group2data = new ArrayList<HashMap<String, Object>>();
    childData.add(group2data);


    // Set up some sample data in both groups
    for( int i=0; i<10; ++i) {
        final HashMap<String, Object> map = new HashMap<String,Object>();
        map.put(NAME, "Child " + i );
        map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
        ( i%2==0 ? group1data : group2data ).add(map);
    }

    setListAdapter( new SimpleExpandableListAdapter(
            this,
            headerData,
            android.R.layout.simple_expandable_list_item_1,
            new String[] { NAME },            // the name of the field data
            new int[] { android.R.id.text1 }, // the text field to populate with the field data
            childData,
            0,
            null,
            new int[] {}
        ) {
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

                // Populate your custom view here
                ((TextView)v.findViewById(R.id.name)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME) );
                ((ImageView)v.findViewById(R.id.image)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(IMAGE) );

                return v;
            }

            @Override
            public View newChildView(boolean isLastChild, ViewGroup parent) {
                 return layoutInflater.inflate(R.layout.expandable_list_item_with_image, null, false);
            }
        }
    );

在您的自定义子布局中,名为expandable_list_item_with_image.xml:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right" />

</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中的父级可扩展列表中添加图像? 的相关文章

随机推荐

  • 匹配 STM32F0 和 zlib 中的 CRC32

    我正在研究运行 Linux 的计算机和 STM32F0 之间的通信链路 我想对我的数据包使用某种错误检测 并且由于 STM32F0 有 CRC32 硬件 并且我在 Linux 上有带有 CRC32 的 zlib 所以我认为在我的项目中使用
  • Qt/C++ QTableWidget:双击标题时执行某些操作

    我的表单中有一个 QTableWidget 我想在用户双击行或列的标题时执行某些操作 我正在使用以下连接语句 connect ui gt tblResults gt horizontalHeader SIGNAL doubleClicked
  • 错误:不兼容的类型:char 无法转换为 String - Java [重复]

    这个问题在这里已经有答案了 这只是我遇到问题故障排除的代码的一部分 static int checkConcat String number TreeSet
  • 了解 celery 任务预取

    我刚刚发现配置选项CELERYD PREFETCH MULTIPLIER docs 默认值为 4 但 我相信 我希望预取关闭或尽可能低 我现在将其设置为 1 这与我正在寻找的内容足够接近 但仍然有一些我不明白的事情 为什么这种预取是个好主意
  • 如何更改 JOptionPane 的字体大小

    import java awt ComponentOrientation import javax swing JFrame import javax swing JLabel import javax swing JList import
  • 在 C# 中连接两个具有不同数据类型的字典

    我有两本词典 即 Dictionary
  • 一个目录中可以放置多少个文件?

    我在一个目录中保存多少个文件有关系吗 如果是这样 一个目录中有多少个文件算过多 文件过多会产生什么影响 这是在 Linux 服务器上 背景 我有一个相册网站 上传的每张图片都被重命名为8位十六进制数字的id 例如a58f375c jpg 这
  • 将 NA 替换为基于 ID 属性的模式

    我有一个数据集dt我想更换NA值与mode基于 id 的每个属性如下 Before id att 1 v 1 v 1 NA 1 c 2 c 2 v 2 NA 2 c 我正在寻找的结果是 id att 1 v 1 v 1 v 1 c 2 c
  • Mongoose - find():搜索选项内的对象不起作用

    我有一个猫鼬架构 如下所示 var mySchema new mongoose Schema metadata isDeleted type Boolean default false 我想获取我的元素列表mongodb数据库应用过滤器 所
  • 如何将标题置于表单标题的中心?

    我使用中心标签将标题置于表单的中心 请注册您的详细信息 它工作得非常完美 就像我想要的那样 但它未能通过验证 还有人知道我可以用什么吗
  • 如何在android中通过intent打开前后摄像头?

    我们实现相机的目的是它在 micromax480p 版本 5 1 中工作正常 但是当我们在 Nexus7 版本 6 1 中使用时 相机已打开 但我想打开一些正面和一些时间背面是否可以根据我们的需要打开 Intent intent new I
  • 如何在Android中单击水平列表视图项目时更改背景颜色

    如何在点击 Android 中的水平列表视图项目时更改背景颜色这样用户就会知道他们点击了哪里 下面是我从某个网站获得的代码 水平ListViewActivity java public class HorizontalListViewAct
  • ios uitextfield 类似笔记

    我希望我的 uitextfield 看起来像 iPhone 的笔记 黄色 带有线条 最好的方法是什么 提前致谢 你的意思是 UItextview 对吗 它不是 UITextField 但是 请清除 UITextView 上的背景 self
  • pyodbc 删除 unicode 字符串

    我正在使用 pyodbc 连接 sqlserver 下面是我的连接字符串 一切正常 但结果作为 unicode 字符串返回 我在连接字符串中有 CHARSET UTF8 但它仍然作为 unicode 字符串返回 有什么方法可以使用连接参数本
  • Rails 3 (beta 4) 安装后出现 Sqlite3 错误

    安装 Rails 3 后 当我尝试迁移时 出现以下有关 Sqlite3 的错误 dlsym 0x1037e5f10 Init sqlite3 native 找不到符号 Library Ruby Gems 1 8 gems sqlite3 r
  • java中如何转义$?

    我正在尝试下面的代码但出现错误 String x aaa XXX bbb String replace XXX String y xy z String z y replaceAll x x replaceFirst replace z S
  • 在棘轮 WebSocket 连接中启动会话

    我们构建了一个 Ratchet websocket 服务器 为独立的客户端应用程序提供服务 服务器在端口 8080 上运行 而我们的 Symfony 应用程序在端口 80 上运行 至关重要的是 我们必须在 websocket 服务器中运行会
  • 使用回调函数报告堆栈回溯

    假设我有以下内容 typedef struct char name char binding int address Fn Symbol definition of function symbol static Fn Symbol fnSy
  • 如何让我的应用程序检查 PC 上是否安装了 Adob​​e flash 播放器?

    我的应用程序需要 Adob e Flash Player 才能正常运行 并且我需要它来检查它是否已安装 那么如何让我的应用程序检查 PC 上是否安装了 Adob e flash 播放器呢 我的程序是用C 编写的 检查此注册表项是否存在 HK
  • 如何在android中的父级可扩展列表中添加图像?

    是否可以在可扩展列表中自定义子项 与 一起工作SimpleExpandableListAdapter一点也不简单 这里有一些代码可以帮助您入门 假设您正在使用ExpandableListActivity 在这个例子中 我们使用标准andro