在ExpandableListView中顯示圖片需要以下幾個步驟:
首先,確保你已經(jīng)將所需的圖片添加到項目的資源文件夾(res/drawable)中。
創(chuàng)建一個自定義的布局文件,例如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<>();
// 添加分組數(shù)據(jù)和子項數(shù)據(jù)
// ...
ImageExpandableListAdapter adapter = new ImageExpandableListAdapter(this, groupHeaders, childItems, R.layout.list_item_image);
expandableListView.setAdapter(adapter);
現(xiàn)在,你應該可以在ExpandableListView中看到圖片了。如果需要根據(jù)數(shù)據(jù)動態(tài)更改圖片,可以在適配器類的getView()方法中設置ImageView的圖片資源。