【问题标题】:ANDROID - ExpandableListViewANDROID - ExpandableListView
【发布时间】:2011-08-04 10:40:38
【问题描述】:

我试图弄清楚如何构建一个包含(许多)的视图:

  • PARENT1(可检查、可扩展)
  • CHILD1(单选按钮)
  • CHILD2(单选按钮)

...

  • PARENT2(可检查、可扩展)
  • CHILD1(可检查)
  • CHILD2(可检查)

...

关键是父级必须是可检查的,并且基于该子级必须更改图标。有人能指出我正确的方向吗,因为我发现似乎没有什么对我有用。

【问题讨论】:

    标签: android checkbox radio-button expandablelistview itemrenderer


    【解决方案1】:

    我假设,您的数据以某种方式结构化,例如: 数组列表在哪里

    • 父{名称:字符串,检查:布尔值, children:ArrayList} 和
    • 子 {name:String}

    如果是这样,你只需要做两件事:

    1. 为您创建合适的布局 父项渲染器和子项 渲染器
    2. 将 BaseExpandableListAdapter 扩展为 自己建立清单。

    这是一个关于我的想法的小示例:
    layout/grouprow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:orientation="horizontal"
        android:gravity="fill" android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- PARENT -->
        <TextView android:id="@+id/parentname" android:paddingLeft="5px"
            android:paddingRight="5px" android:paddingTop="3px"
            android:paddingBottom="3px" android:textStyle="bold" android:textSize="18px"
            android:layout_gravity="fill_horizontal" android:gravity="left"
            android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <CheckBox android:id="@+id/checkbox" android:focusable="false" 
            android:layout_alignParentRight="true" android:freezesText="false"
            android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:layout_marginTop="5px" />
    </RelativeLayout>
    

    layout/childrow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:padding="0px">
        <!-- CHILD -->
        <TextView android:id="@+id/childname" android:paddingLeft="15px" 
            android:paddingRight="5px" android:focusable="false" android:textSize="14px"
            android:layout_marginLeft="10px" android:layout_marginRight="3px" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" />
    </LinearLayout>
    

    MyELAdapter 类:我已将其声明为 MyExpandableList 的内部类,因此我可以直接访问父列表,而无需将其作为参数传递。

    private class MyELAdapter extends BaseExpandableListAdapter
    {
        private LayoutInflater inflater;
    
        public MyELAdapter()
        {
            inflater = LayoutInflater.from(MyExpandableList.this);
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, 
                View convertView, ViewGroup parentView)
        {
            final Parent parent = parents.get(groupPosition);
            convertView = inflater.inflate(R.layout.grouprow, parentView, false);
            ((TextView) convertView.findViewById(R.id.parentname)).setText(parent.getName());
            CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
            checkbox.setChecked(parent.isChecked());
            checkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent));
            if (parent.isChecked())
                convertView.setBackgroundResource(R.color.red);
            else
                convertView.setBackgroundResource(R.color.blue);
            return convertView;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                View convertView, ViewGroup parentView)
        {
            final Parent parent = parents.get(groupPosition);
            final Child child = parent.getChildren().get(childPosition);
            convertView = inflater.inflate(R.layout.childrow, parentView, false);
            ((TextView) convertView.findViewById(R.id.childname)).setText(child.getName());
            return convertView;
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition)
        {
            return parents.get(groupPosition).getChildren().get(childPosition);
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }
    
        @Override
        public int getChildrenCount(int groupPosition)
        {
            return parents.get(groupPosition).getChildren().size();
        }
    
        @Override
        public Object getGroup(int groupPosition)
        {
            return parents.get(groupPosition);
        }
    
        @Override
        public int getGroupCount()
        {
            return parents.size();
        }
    
        @Override
        public long getGroupId(int groupPosition)
        {
            return groupPosition;
        }
    
        @Override
        public void notifyDataSetChanged()
        {
            super.notifyDataSetChanged();
        }
    
        @Override
        public boolean isEmpty()
        {
            return ((parents == null) || parents.isEmpty());
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    
        @Override
        public boolean hasStableIds()
        {
            return true;
        }
    
        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
    }
    

    你必须已经有一个公共类MyExpandableList extends ExpandableListActivity,它有一个成员:

    private ArrayList<Parent> parents;
    

    在你给这个成员赋值/加载父母列表之后,你还应该将你的适配器附加到这个视图:

    this.setListAdapter(new MyELAdapter());
    

    就是这样。您有一个可检查可扩展列表,并且在CheckUpdateListeneronCheckedChanged(CompoundButton buttonView, boolean isChecked) 方法中,您可以更新父对象的检查状态。

    注意,背景颜色是在 getGroupView 方法中确定的,所以你不必在任何地方更改它,如果需要,只需调用适配器的 notifyDataSetChanged() 方法即可。

    更新

    您可以从this link 下载示例源代码。它是一个 Eclipse 项目,但如果您使用的是其他开发环境,只需简单地复制必要的源文件(java + xml)即可。

    【讨论】:

    • 感谢 rekaszeru。由于我是初学者,您愿意分享一个工作样本吗?
    • 当然,我只需要几分钟来建立一个。如果我知道您从哪里获取数据(服务器或本地),这将很有用。
    • 如果可以,请构建一个非常简单的示例(只是一些带有一堆数组的测试数据)。我得到了专为紧凑框架树视图设计的数据 tru 类型的 XML 服务。我将不得不自己格式化数据,但在这个阶段我还不确定如何。现在我对消耗列表本身的结构感兴趣。复选框、单选按钮等
    • 非常感谢 rekaszeru !如果我可以再问一个问题......如果你想要我在我的问题中提出的模式,你会如何定制孩子。在一个父母下,我希望在另一个父母中对孩子进行多选(复选框),我希望对孩子进行单选(单选按钮)。尽管如此,我还是接受了您的回答,因为您将时间和知识用于帮助新手!
    • 您可以扩展适配器的getChildView方法,并检查父级是否需要单选或多选的子级。还需要创建两个childrow.xml-s,一个用于radionbuttons,一个用于复选框。在数据结构级别上实现这一点的最明确的方法可能是拥有一个抽象的AParent 类(具有当前Parent 的功能)并基于它创建一个SingleSelectParent 和一个MultiSelectParent 扩展类。一个会有一个新成员:selectedIndex:int 并且 MultiSelectParent 会有一个 selectedIndices:ArrayList...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2014-07-23
    • 2011-08-19
    相关资源
    最近更新 更多