在ExpandableListView中添加圖片,您需要自定義一個(gè)適配器,該適配器繼承自BaseExpandableListAdapter
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// ...
}
在適配器類中,實(shí)現(xiàn)必要的方法,如getGroupCount(), getChildrenCount(), getGroup(), getChild(), getGroupId(), getChildId(), hasStableIds()和getViewTypeCount()等。
重寫getGroupView()和getChildView()方法,以便在這些方法中設(shè)置圖片。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// 初始化group的布局
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
// 獲取ImageView并設(shè)置圖片
ImageView imageView = (ImageView) convertView.findViewById(R.id.group_image);
imageView.setImageResource(groupImages[groupPosition]);
// 其他UI元素(如TextView)的設(shè)置
// ...
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
// 初始化child的布局
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
// 獲取ImageView并設(shè)置圖片
ImageView imageView = (ImageView) convertView.findViewById(R.id.child_image);
imageView.setImageResource(childImages[groupPosition][childPosition]);
// 其他UI元素(如TextView)的設(shè)置
// ...
return convertView;
}
ExpandableListView expandableListView = findViewById(R.id.expandable_list_view);
MyExpandableListAdapter adapter = new MyExpandableListAdapter(this, groupData, childData);
expandableListView.setAdapter(adapter);
注意:這里的示例代碼僅作為參考,您可能需要根據(jù)自己的需求進(jìn)行調(diào)整。