gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:19:12 25 4
gpt4 key购买 nike

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

最佳答案

使用 SimpleExpandableListAdapter 绝非简单。假设您使用的是 ExpandableListActivity,下面是一些可以帮助您入门的代码。

在这个例子中,我们使用标准的 android.R.layout.simple_expandable_list_item_1 作为我们的组头 View ,但我们使用我们自己的自定义布局给 children 。

    // 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>

关于android - 如何在android的父级可扩展列表中添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1353101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com