Android 可扩展列表为单个组设置背景颜色

标签 android android-listview

如何在可扩展 View 中为各个组名称设置不同的背景颜色?

我有使用组名创建组的代码 - 组 1 到组 10。每个组都有 child - child 1 到 child 3。

我想根据组名为每个组及其子组赋予不同的背景颜色。不满足条件默认为黑色。
示例:
第 1 组 - 白色背景
第 2 组 - 红色背景
第 3 组 - 灰色背景
第 4 组 - 黄色背景

ma​​in.XML:可扩展列表布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

        <ExpandableListView 
            android:id="@+id/expandable_list"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />

         <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:choiceMode="multipleChoice" />

</LinearLayout>

grouprow.xml:分组文本布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/rowname"
         android:paddingLeft="50px"
         android:textSize="30px"
         android:textColor="@drawable/blue"
         android:textStyle="normal"
         android:layout_width="fill_parent"
         android:layout_height="50px"/>

</LinearLayout>

childrow.xml:子文本布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/grpchild"
         android:paddingLeft="50px"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="normal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

</LinearLayout>

父组类:创建每个父项的类

public class CategoryGroup {
       private String mTitle;
        private ArrayList<String> mArrayChildren;

        public String getTitle() {
            return mTitle;
        }

        public void setTitle(String mTitle) {
            this.mTitle = mTitle;
        }

        public ArrayList<String> getArrayChildren() {
            return mArrayChildren;
        }

        public void setArrayChildren(ArrayList<String> mArrayChildren) {
            this.mArrayChildren = new ArrayList(mArrayChildren);
        }
}

可扩展列表类:

public class ExpandableListItems extends BaseExpandableListAdapter {

           private LayoutInflater inflater;
            private ArrayList<CategoryGroup> mParent;

          public ExpandableListItems(Context context, ArrayList<CategoryGroup> parent) {
                mParent = parent;
                inflater = LayoutInflater.from(context);
            }


            @Override
            //counts the number of group/parent items so the list knows how many times calls getGroupView() method
            public int getGroupCount() {
                return mParent.size();
            }

            @Override
            //counts the number of children items so the list knows how many times calls getChildView() method
            public int getChildrenCount(int i) {
                return mParent.get(i).getArrayChildren().size();
            }

            @Override
            //gets the title of each parent/group
            public Object getGroup(int i) {
                return mParent.get(i).getTitle();
            }

            @Override
            //gets the name of each item
            public Object getChild(int i, int i1) {
                return mParent.get(i).getArrayChildren().get(i1);
            }

            @Override
            public long getGroupId(int i) {
                return i;
            }

            @Override
            public long getChildId(int i, int i1) {
                return i1;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            //in this method you must set the text to see the parent/group on the list
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

                if (view == null) {
                    System.out.println("i: " + i + "; b: " + b );
                    view = inflater.inflate(R.layout.grouprow, viewGroup, false);
                }


                TextView textView = (TextView) view.findViewById(R.id.rowname);

                //"i" is the position of the parent/group in the list

                textView.setText(getGroup(i).toString());

                //return the entire view
                return view;
            }

            @Override
            //in this method you must set the text to see the children on the list
            public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = inflater.inflate(R.layout.childrow, viewGroup, false);
                }

                TextView textView = (TextView) view.findViewById(R.id.grpchild);
                //"i" is the position of the parent/group in the list and 
                //"i1" is the position of the child
                textView.setText(mParent.get(i).getArrayChildren().get(i1));

                //return the entire view
                return view;
            }

            @Override
            public boolean isChildSelectable(int i, int i1) {
                return true;
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                /* used to make the notifyDataSetChanged() method work */
                super.registerDataSetObserver(observer);
            }
}

主要 Activity 类:

public class GetResourcesListActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        private ExpandableListView mExpandableList;
        private ExpandableListItems mAdapter;
        ArrayList<CategoryGroup> arrayParentsGroups = new ArrayList<CategoryGroup>();

        mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
        arrayParents = createGroupList();

        mAdapter = new ExpandableListItems(this, arrayParentsGroups);
        mExpandableList.setAdapter(mAdapter);

        private List createGroupList() {
            ArrayList arrayParents = new ArrayList();
            for( int i = 0 ; i < 10 ; ++i ) { // 10 groups........
                CategoryGroup parent = new CategoryGroup();
                parent.setTitle("Group" + i);

                ArrayList<String> arrayChildren = new ArrayList<String>();
                for (int j = 0; j < 3; j++) {
                    arrayChildren.add("Child " + j);
                }
                parent.setArrayChildren(arrayChildren);
                arrayParents.add(parent);
            }
            return arrayParents;
        }
    }
)

最佳答案

实现此目的的简单方法如下。

为您的 CategoryGroup 对象提供另一个名为 int mBackgroundRes 的属性。同时添加相应的getter和setter方法。

在您的 createGroupList() 方法中,您可以根据您的逻辑设置后台资源:

private ArrayList<CategoryGroup> createGroupList() {
    ArrayList arrayParents = new ArrayList();
    for (int i = 0; i < 10; ++i) { // 10 groups........
        CategoryGroup parent = new CategoryGroup();
        parent.setTitle("Group" + i);

        int background = android.R.color.black;
        if (i == 0) {
            background = android.R.color.white;
        } else if (i == 1) {
            background = android.R.color.holo_red_light;
        } else if (i == 2) {
            background = android.R.color.darker_gray;
        }
        parent.setBackgroundRes(background);

        ArrayList<String> arrayChildren = new ArrayList<String>();
        for (int j = 0; j < 3; j++) {
            arrayChildren.add("Child " + j);
        }
        parent.setArrayChildren(arrayChildren);
        arrayParents.add(parent);
    }
    return arrayParents;
}

最后,在您的 BaseExpandableListAdapter 中更改 getGroup() 方法以返回正确的对象类型。您可以简单地将 Object 更改为 CategoryGroup:

@Override
public CategoryGroup getGroup(int i) {
    return mParent.get(i);
}

并在 getGroupView()getChildView() 方法中设置各自 View 的背景:

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

    if (view == null) {
        System.out.println("i: " + i + "; b: " + b);
        view = inflater.inflate(R.layout.grouprow, viewGroup, false);
    }

    view.setBackgroundResource(getGroup(i).getBackgroundRes());

    TextView textView = (TextView) view.findViewById(R.id.rowname);
    textView.setText(getGroup(i).getTitle());

    return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view,
        ViewGroup viewGroup) {

    if (view == null) {
        view = inflater.inflate(R.layout.childrow, viewGroup, false);
    }

    view.setBackgroundResource(getGroup(i).getBackgroundRes());

    TextView textView = (TextView) view.findViewById(R.id.grpchild);
    textView.setText(mParent.get(i).getArrayChildren().get(i1));

    return view;
}

关于Android 可扩展列表为单个组设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16763107/

相关文章:

Android - 我怎么知道 gridview 何时到达底部?

java - 从 MainActivity 启动 fragment

android - 如何在 android 布局中打开 Intent - 到特定区域,而不是全屏

android - LayoutInflater 有时(随机)以错误的背景颜色膨胀

android - 有没有办法从主要 Activity 中隐藏特定的 ListView 项目?

android - 如何制作 TextView 列表?

android - 调用 HP ePrint Android 应用程序

1490086 字节的 Android 错误 : I/dalvikvm-heap: Grow heap (frag case) to 16. 159MB

android - 将某些文件扩展名关联到我的 android 应用程序

java - ListView onScroll 添加更多项目