2

我想在我的可扩展列表中对标题(即第一级项目)应用不同的颜色。我怎样才能做到这一点?

我在我的 Android 应用程序中使用以下类作为我的可扩展列表。

public class ExpandableListAdapter extends BaseExpandableListAdapter {...}

在其中,这是对 getGroupView() 函数的调用

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

我定义了我的 XML 布局,称为 list_group,如下所示。

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

如您所见,我将文本颜色指定为#ff9900。

但是,我想在列表视图中有一些标题为#ff9900 颜色,而其他标题为不同的颜色(例如红色)。

我如何使用布尔变量根据我想要的项目颜色加载不同的 XML 布局?

非常感谢。

4

1 回答 1

2

仅考虑文本颜色,以下方式可能适用:

直接更改文本颜色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;
    }

如果您需要完全不同的布局,那么您可以使用getGroupTypeCount()getGroupType()来为组定义两种(或更多)不同的布局类型,如下所示:

    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;
    }
于 2013-09-13T05:46:40.407 回答