多层ExpandableListView

时间:2011-11-28 09:02:43

标签: android expandablelistview expandablelistadapter

我目前正在开展一个项目,我需要以下内容:

- MainGroup 1 (Expandable)
  - SubGroup 1 (Expandable)
    - SubSubGroup 1 (Expandable)
      - Child View
      - Child View
      - ...
    - SubSubGroup 2 (Expandable)
      - Child View
      - Child View
      - ...
  - SubGroup 2 (Expandable)
    - SubSubGroup 3 (Expandable)
      - Child View
      - ...
    - SubSubGroup 4 (Expandable)
      - Child View
      - ...
  - SubGroup 3 (Expandable)
    - Child View
    - ...
- MainGroup 2 (Expandable)
  - ...

ExpandableListView内的ExpandableListView内最多只有ExpandableListView - 所以ExpandableListView有3层:

  • MainGroup只能容纳其他ExpandableListView
  • SubGroup会保留其他ExpandableListView,但最后两个除外,它们总是只有ChildViews。 (我认为这两个可以用SubSubGroup
  • 代替
  • SubSubGroup将永远持有ChildViews

我的问题是,我认为我无法理解ExpandableListView如何布置它的孩子的基本原则。我看过像this这样的例子,但无法理解这个功能。

我尝试过简单地将ExpandableListView作为一个孩子添加到另一个中 - 但是有些东西不起作用,因为如果展开其中一个外部组,只有内部ExpandableListView的第一项可见。但是,我可以手动设置外部组容器的高度,使其大到足以显示内部ExpandableListView中的所有项目。为了达到最佳效果,高度的计算当然应该在运行中进行。

所以,要得到一些更加坚实的问题:

  • 有人可以给我一个ExpandableListView“生命周期”的解释(我的意思是即时,重复使用视图,扩展/崩溃听众),包括:
    • ExpandableListView如何以及何时通知儿童已展开/折叠?
    • ExpandableListView如何知道让所有孩子都能容纳多少个空间?
  • 使用ExpandableListView创建上述内容有多大好处,而只使用一些LinearLayouts和一些OnClickListeners将我自己的解决方案混合在一起?

修改

对于我的上一个问题,我可能会注意到可能有1到20+ MainGroups

3 个答案:

答案 0 :(得分:10)

首先,让我向您推荐GrepCode网站。它有Android SDK类的来源。感谢他们,我已经找到了AdapterViews的基本原则(我给你一个指向ExpandableListView的链接,但是如果你不仅研究它,而且它的类父母也是如此)它会更好。

现在,您的问题和我的答案:

  

如何以及何时通知ExpandableListView是否有孩子   展开/折叠?

参见方法handleItemClick(View v, int position, long id)

  

ExpandableListView如何知道为所有人腾出多少空间   孩子们是否适合集体容器?

我没有很好地研究这个问题,但据我所知,它是由requestLayout()方法完成的。我们还发现一个可滚动的View无法嵌入到另一个可滚动的View中。 (这是众所周知的错误:将ListView放入ScrollView或ListView到ExpandableListView中。)

  

使用ExpandableListView创建有多少好处   以上,与使用一些混合我自己的解决方案相比   LinearLayouts和一些OnClickListeners?

AdapterViews更快。但是,即使对于ExpandableListView,您的构造也太复杂了。您可以使用2种解决方案。

  1. Guru-solution:如果你专业设计自己的 从头开始查看/查看组(我的意思是,您了解方法 requestLayout(),dispatchDraw()等),然后编写自己的 具有3个级别的GrandExpandableListAdapter。
  2. 中等解决方案:使用ExpandableListAdapter获得前2个级别,并使用LinearLayouts 第3级。

答案 1 :(得分:1)

您可能想要检查this项目。我没试过,但我想这值得一试。默认的ExpanadableListView非常有限,最初设计为仅支持2个级别。

,你可以用它来破解它以支持更多级别,但它会变得混乱。

答案 2 :(得分:1)

即使这不是完整的解决方案,它仍然是一个3层可扩展列表。所以它留给你的是你的风格。

这是班级

package com.custom.nagee;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

public class CustomemExpandible extends ExpandableListActivity{
    LayoutInflater inflator;
    boolean flag = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflator = LayoutInflater.from(getApplicationContext());
        setListAdapter(new MyAdapter());
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        if(flag){
            v.findViewById(childPosition).setVisibility(View.VISIBLE);
            flag = false;
            return true;
        }
        v.findViewById(childPosition).setVisibility(View.GONE);
        flag = true;
        return true;
    }
    class MyAdapter extends BaseExpandableListAdapter{

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            LinearLayout linearLayout = (LinearLayout)inflator.inflate(R.layout.group, null);
            linearLayout.getChildAt(1).setId(childPosition);
            return linearLayout;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return 2;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public int getGroupCount() {
            return 2;
        }

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

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            return ((LinearLayout)inflator.inflate(R.layout.group, null));
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }
}

这里是xml文件

<强> group.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/back"
    android:layout_marginLeft="30dip" 
    android:text="DONE" >
</TextView>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="30dip" 
    android:visibility="gone" >

    <TextView
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="DONE">
    </TextView>

    <TextView
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DONE" />

    <TextView
        android:id="@+id/editText4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="DONE" />
</LinearLayout>

这是在颜色文件夹下更改文字颜色,它离开了你,你甚至可以改变背景以获得更好的外观。

<强> back.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#808080"/>
    <item android:state_window_focused="false" android:color="#808080"/>
    <item android:state_pressed="true" android:color="#808080"/>
    <item android:state_selected="true" android:color="#000000"/>
    <item android:color="#FF0000"/> <!-- not selected -->
</selector>

希望它有效......