这是我的 expandablelistadapter :
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但是我想为不同的组设置不同的图标。如何更改适配器。
答案 0 :(得分:0)
您可以在ListView上设置的组指示符将始终应用于所有元素。因此,要获得所需的行为,您必须自定义列表项的布局以复制效果。将ImageView添加到列表项,并以编程方式为每个列表项设置图像。
答案 1 :(得分:0)
首先定义可扩展列表视图
<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)