ANDROID-ExpandableListView

问题描述

| 我试图弄清楚如何构建包含(许多)视图: PARENT1(可检查,可扩展) CHILD1(无线电按钮) CHILD2(无线电按钮) ... PARENT2(可检查,可扩展) 儿童1(可检查) 儿童2(可检查) ... 重点是父级必须是可检查的,并且基于此子级必须更改图标。有人能指出我正确的方向吗,因为从我那里发现什么似乎对我都没有用。     

解决方法

我假设您以某种方式对数据进行了结构化,例如: ArrayList其中 父项{name:String,已选中:布尔值, children:ArrayList}和 子{name:String} 如果是这样,您只需要做两件事: 为您创建合适的布局 父项渲染器和子项 渲染器 将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,ViewGroup parentView)
    {
        final Parent parent = parents.get(groupPosition);
        final Child child = parent.getChildren().get(childPosition);
        convertView = inflater.inflate(R.layout.childrow,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;
    }
}
您必须已经有一个拥有成员的公共课程“ѭ3”:
private ArrayList<Parent> parents;
在为此成员分配值/加载父母列表之后,还应该将适配器附加到此视图:
this.setListAdapter(new MyELAdapter());
就是这样。您有一个可检查的可扩展列表,并且在
CheckUpdateListener
的'7方法中,您可以更新父对象的选中状态。 请注意,背景色是由getGroupView方法确定的,因此您不必在任何地方进行更改,只需在需要时调用适配器的
notifyDataSetChanged()
方法即可。 更新资料 您可以从此链接下载示例源代码。这是一个eclipse项目,但是如果您使用其他开发人员环境,只需复制必要的源文件(java + xml)即可。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...