如何在Android ExpandableListView中为类别标题添加标题图标
这是我的可扩展列表适配器:如何在Android ExpandableListView中为类别标题添加标题图标,android,expandablelistview,Android,Expandablelistview,这是我的可扩展列表适配器: public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context; private List<String> _listDataHeader; // header titles // child data in format of header title, child title private HashMap<Strin
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private HashMap<String, List<String>> _listDataChildID;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData,HashMap<String, List<String>> listChildIDData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this._listDataChildID = listChildIDData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
public Object getChildrenID(int groupPosition, int childPosititon) {
return this._listDataChildID.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
它实际上为所有组设置了相同的groupindicator,但是我希望不同的组有不同的图标。如何更改适配器。您可以在ListView上设置的组指示器将始终应用于所有元素。因此,要获得您想要的行为,您必须自定义列表项的布局以复制效果。将ImageView添加到列表项中,并以编程方式为每个列表项设置图像。首先定义可展开的listview
<ExpandableListView
android:id="@+id/left_drawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#e5e5e5"
android:choiceMode="singleChoice"
android:divider="#000"
android:dividerHeight="1dp" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity"
>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/imgicon"
/>
<TextView
android:id="@+id/textViewsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:text="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imgicon"
>
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
}
现在,我们只需要相应地使用这个类
使用此类初始化组标题和图标
然后在getGroupView()
方法中执行以下操作:
final groups group = (groups) getGroup(groupPosition);
textviewsub.setText(group.string);
icon_img.setImageBitmap(group.bitmap)在上述代码中,您应该通过编程设置需要在每个组中显示的图像图标是的,但是您可以演示如何执行此操作,因为标题是从getGroupView获取参数groupPosition,这意味着我必须再次向适配器中的每个函数添加参数,还是有更好的方法执行此操作:(我仍然不知道如何修复它,因为我将在jsonformat中拥有来自Web服务的图像设置标题很简单,因为这是字符串我如何设置位图您是否保存从Web服务接收的图像?
public class Group {
public String string;
public Bitmap bitmap;
public final List<String> children = new ArrayList<String>();
public Group(String string,Bitmap bitmap){
this.string = string;
this.bitmap = bitmap;
}
final groups group = (groups) getGroup(groupPosition);