在ExpandableListView中显示图片需要以下几个步骤:
首先,确保你已经将所需的图片添加到项目的资源文件夹(res/drawable)中。
创建一个自定义的布局文件,例如list_item_image.xml,用于定义列表项的布局。在这个布局文件中,添加ImageView和其他需要的控件。例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/your_image" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="Your Text" />
</LinearLayout>
请确保将your_image
替换为实际的图片资源名称。
public class ImageExpandableListAdapter extends BaseAdapter {
private Context context;
private List<String> groupHeaders;
private List<List<String>> childItems;
private int resource;
public ImageExpandableListAdapter(Context context, List<String> groupHeaders, List<List<String>> childItems, int resource) {
this.context = context;
this.groupHeaders = groupHeaders;
this.childItems = childItems;
this.resource = resource;
}
@Override
public int getGroupCount() {
return groupHeaders.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childItems.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupHeaders.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childItems.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resource, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView);
textView.setText(headerTitle);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resource, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.your_image); // 设置图片资源
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
请确保将your_image
替换为实际的图片资源名称。
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
List<String> groupHeaders = new ArrayList<>();
List<List<String>> childItems = new ArrayList<>();
// 添加分组数据和子项数据
// ...
ImageExpandableListAdapter adapter = new ImageExpandableListAdapter(this, groupHeaders, childItems, R.layout.list_item_image);
expandableListView.setAdapter(adapter);
现在,你应该可以在ExpandableListView中看到图片了。如果需要根据数据动态更改图片,可以在适配器类的getView()方法中设置ImageView的图片资源。