如何在android的父级的可扩展列表中添加图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1353101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add image in expandable List in parent in android?
提问by emmby
And is it possible to customize the child in expandable list?
是否可以在可扩展列表中自定义子项?
回答by emmby
Working with SimpleExpandableListAdapter
is anything but simple. Here's some code that should get you started, assuming that you're using ExpandableListActivity
.
使用SimpleExpandableListAdapter
它绝非易事。假设您使用的是ExpandableListActivity
.
In this example, we use the standard android.R.layout.simple_expandable_list_item_1
for our group header view, but we use our own custom layout for the children.
在这个例子中,我们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);
}
}
);
And inside your custom child layout, named expandable_list_item_with_image.xml
:
在您的自定义子布局中,名为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>
回答by Tine M.
You can try making your own list adapter that extends BaseExpandableListAdapter just like described in documentation.
您可以尝试制作自己的列表适配器来扩展 BaseExpandableListAdapter ,就像文档中描述的那样。
Then override getGroupView(..)
(for parent item, or getChildView for child item) function and in this function you can inflate your own layout xml.
然后覆盖getGroupView(..)
(对于父项,或 getChildView 对于子项)函数,在此函数中,您可以扩充自己的布局 xml。
something like this:
像这样:
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent)
{
View v = convertView;
if (v == null) {
//sender is activity from where you call this adapter. Set it with construktor.
LayoutInflater vi = (LayoutInflater)sender.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
//children = arraylists of Child
Child c = children.get(childPosition);
if (c != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
if (tt != null) {
tt.setText(c.text1); }
if(bt != null){
bt.setText(c.text2);
}
if (icon != null)
{
icon.setImageResource(R.drawable.rowicon);
}
}
return v;
}
layout xml :
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/rowicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
回答by penguru
After you override getGroupView, you can use setCompoundDrawablesWithIntrinsicBounds
function to add image.
覆盖 getGroupView 后,您可以使用setCompoundDrawablesWithIntrinsicBounds
函数添加图像。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
TextView tv = (TextView) v.findViewById(R.id.textViewId);
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.imageToAdd, 0, 0, 0);
return v;
}
回答by penguru
I just found a better way, you can simply add
我刚刚找到了一个更好的方法,你可以简单地添加
android:drawableRight="@drawable/icon"
line to layout xml file of list group.
android:drawableRight="@drawable/icon"
用于布局列表组的 xml 文件的行。