Frage

I would like to apply different colours to the header (i.e. the 1st level items) in my Expandable List. How can I do that?

I am using the following class for my expandable list in my Android app.

public class ExpandableListAdapter extends BaseExpandableListAdapter {...}

And inside it, this is the call to getGroupView() function

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

I defined my XML layout, called list_group, as follows.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">


<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="17dp"
    android:textColor="#ff9900" />

 </LinearLayout>

As you can see, I specified text color to #ff9900.

However, I would like to have SOME headers in the list view in #ff9900 color, and other headers in a different colour (e.g. red).

How could I use a boolean variable to load a different XML layout based on which colour I want for my item?

Thanks a lot.

War es hilfreich?

Lösung

Considering only text colour, the following way might be applicable:

change text colour straight in getGroupView():

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        final String headerTitle = (String) getGroup(groupPosition);
        View view = convertView;

        if (convertView == null) {
            final LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = infalInflater.inflate(R.layout.list_group, null);
        }

        final TextView lblListHeader = (TextView) view
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        if (/* Check Your flag here*/ (groupPosition & 1) == 1) {
            lblListHeader.setTextColor(mContext.getResources().getColor(android.R.color.holo_red_light));
        } else {
            lblListHeader.setTextColor(mContext.getResources().getColor(android.R.color.holo_green_light));
        }

        return view;
    }

If You need completely different layout, then You might use getGroupTypeCount() and getGroupType() in order to define two (or more) different layout types for groups, like the following:

    final static int GROUP_TYPE_COUNT = 2;
    final static int GROUP_TYPE_FIRST = 0;
    final static int GROUP_TYPE_SECOND = 1;

    @Override
    public int getGroupTypeCount() {
        return GROUP_TYPE_COUNT;
    }

    @Override
    public int getGroupType(final int groupPosition) {
        return ((groupPosition & 1) == 1) ? GROUP_TYPE_FIRST : GROUP_TYPE_SECOND;
    }

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
        final String headerTitle = (String) getGroup(groupPosition);
        final int type = getGroupType(groupPosition);
        View view = convertView;

        if (convertView == null) {
            final LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            switch (type) {
                case GROUP_TYPE_FIRST:
                    view = infalInflater.inflate(R.layout.list_group, null);
                    break;

                case GROUP_TYPE_SECOND:
                default:
                    view = infalInflater.inflate(R.layout.list_group_1, null);
                    break;
            }
        }

        final TextView lblListHeader = (TextView) view
                .findViewById(R.id.lblListHeader);

        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return view;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top