0

这是我的可扩展列表适配器

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;
}

}

当我添加活动时

    **// get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);
    Drawable d = getResources().getDrawable(R.drawable.header_icon);
    expListView.setGroupIndicator(d);** 

它实际上为所有组设置了相同的 groupindicator,但是我想要为不同的组设置不同的图标。我该如何更改适配器。

4

2 回答 2

1

首先定义可展开的列表视图

    <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" />

然后是一个 xml 来定义您的可扩展列表视图组

<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()方法中执行此操作 编写以下代码

ImageView imgicon = (ImageView) convertView.findViewById(R.id.imgicon); imgicon.setImageBitmap(bitmap);

希望这能解决你的问题

New Added Code

制作一个以组标题和组图标为属性的自定义类

groups.java

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;
}

}

现在只需相应地使用这个类

使用这个类来初始化你的组标题和图标

并在getGroupView()方法中执行以下操作:

final groups group = (groups) getGroup(groupPosition);

textviewsub.setText(group.string); icon_img.setImageBitmap(group.bitmap)

于 2013-10-28T14:48:04.670 回答
0

您可以在 ListView 上设置的组指示符将始终应用于所有元素。因此,要获得您想要的行为,您必须自定义列表项的布局以复制效果。将 ImageView 添加到列表项,并以编程方式为每个列表项设置图像。

于 2013-10-28T14:44:31.230 回答