簡體   English   中英

如何在Android ExpandableListView中為類別標題添加標題圖標

[英]How to add a header icon for category titles in Android ExpandableListView

這是我的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);** 

它實際上為所有組設置了相同的組指示符,但是我想為不同的組使用不同的圖標。 如何更改適配器。

您可以在ListView上設置的組指示符將始終應用於所有元素。 因此,要獲得所需的行為,必須自定義列表項的布局以復制效果。 將ImageView添加到列表項,然后以編程方式為每個列表項設置圖像。

首先定義可擴展的列表視圖

    <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來定義您的可擴展listview組

<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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM