Android 多层可扩展列表视图

Android 多层可扩展列表视图,android,expandablelistview,expandablelistadapter,Android,Expandablelistview,Expandablelistadapter,我目前正在从事一个项目,其中我需要以下内容: - MainGroup 1 (Expandable) - SubGroup 1 (Expandable) - SubSubGroup 1 (Expandable) - Child View - Child View - ... - SubSubGroup 2 (Expandable) - Child View - Child View - ... - Su

我目前正在从事一个项目,其中我需要以下内容:

- 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

  • main组
    将只保存其他
    ExpandableListView
  • 子组
    将保存其他
    可扩展列表视图
    ,但最后两个除外,这两个视图始终是
    子视图
    。(我想这两个可以用一个来代替
    子组
  • 子分组
    将始终保持
    childview
我的问题是,我想我没有理解
ExpandableListView
如何布局它的子对象的基本原则。我看过这样的例子,但无法理解这些功能

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

因此,要回答一些更为具体的问题:

  • 有人能给我解释一下
    ExpandableListView
    的“生命周期”(我指的是实例化、视图重用、展开/折叠侦听器)包括:
    • ExpandableListView
      如何以及何时通知子级已展开/折叠
    • ExpandableListView
      如何知道为所有子项留出多少空间来容纳组容器
  • 与使用一些
    线性布局
    和一些
    OnClickListeners
    将我自己的解决方案混合在一起相比,使用
    可扩展列表视图
    创建上述内容有多大好处

编辑


对于我的最后一个问题,我可能会注意到,可能有1到20个以上的主组

您可能想要签出项目。我还没试过,但我想值得一试。默认的ExpanadableListView非常有限,最初设计为仅支持2个级别。你可以随意使用它,让它支持更多的级别,但它会变得混乱。

即使这不是一个完整的解决方案,它仍然是一个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"?>
<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>


这是在“颜色”文件夹下用于更改文本颜色,它留给您,您甚至可以更改背景以获得更好的外观

back.xml

<?xml version="1.0" encoding="utf-8"?>
<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>


希望它能工作…

首先,让我向你推荐这个网站。它有Android SDK类的源代码。感谢他们,我找到了AdapterViews的基本原理(我给了您一个到ExpandableListView的链接,但如果您不仅研究它,而且还研究它的类父级,那就更好了)

现在,你的问题和我的回答:

ExpandableListView如何以及何时通知子级 扩展/崩溃

见方法

ExpandableListView如何知道要为所有用户留出多少空间 要将儿童放入组容器中

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

使用ExpandableListView创建 以上,与我自己的解决方案混合使用一些 线性布局和一些onclick听众

AdapterView速度更快。但即使对于ExpandableListView,您的构造也太复杂了。您可以使用两种解决方案

  • 大师解决方案:如果你是专业的设计自己的 从头开始的视图/视图组(我的意思是,您了解方法 requestLayout()、dispatchDraw()等),然后编写自己的 具有3个级别的GrandExpandableListAdapter
  • 中等解决方案:前两个级别使用ExpandableListAdapter,前两个级别使用LinearLayouts 第三级

  • 使用它会如何影响我的许可?我是为一家公司开发的,所以我不能真正做那些要求我的源代码是开源的事情。我不担心BSD许可证要求你保持项目的开源。至少我在这里找不到任何声明:。但我可能大错特错了。撇开许可证不谈——它会满足我的目的,所以+1。我确实发现,仅仅运行源代码中提供的简单示例是相当滞后的,因此我仍然在寻找我问题的答案。您的解决方案已经解决了..?在我被转移到另一个项目时,我从未真正解决过这个问题,但QuickNick的答案肯定是正确的。