如何显示超过3级可展开列表视图?

21
如何显示超过3个级别的可展开列表视图?我只能找到三级可展开列表的示例。
参考链接:three-level-expandable-list
在这个示例中,他在 ParentLevel 的 BaseExpandableListAdapter 中的 getChildView 方法中添加了一个更多的可展开列表。
CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());

SecondLevelexplv.setGroupIndicator(null);   
return SecondLevelexplv;

因此,我在SecondLevelAdaptergetChildView方法中添加了另一个可扩展列表。

它能够工作,但是视图不像可扩展列表视图的三级那样显示。

我已经阅读了以下内容:

3-level-expandable-list-view-with-swipe-feature

multi-level-expandablelistview-in-android

issue-with-expanding-multi-level-expandablelistview

请指导我或者分享适用于Android多级可扩展显示的示例。

谢谢,


你说的“它能运行,但是视图显示不正确”是什么意思? - sandrstar
4
所有子视图都应该可见并且可以滚动,但只有第一级和第二级是正确可见且可滚动的。在第三级中,所有子元素都不可见,也无法滚动。 - Cropper
你能发布你的XML文件吗? - Anand Jain
请查看 https://github.com/shahbazhashmi/nested-expandable-recyclerview - Shahbaz Hashmi
4个回答

23

我找到了解决方案并上传了所有的Java类文件,请检查所有Java类文件,或者您可以查看此处

1:MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Object  obj = new Object();
    obj.children =  new ArrayList<Object>();
    for(int i = 0;i<Constant.state.length;i++)
    {
        Object root =  new Object();
        root.title = Constant.state[i];
        root.children =  new ArrayList<Object>();
        for(int j=0;j<Constant.parent[i].length;j++)
        {
             Object parent =  new Object();
             parent.title=Constant.parent[i][j];
             parent.children =  new ArrayList<Object>();
             for(int k=0;k<Constant.child[i][j].length;k++)
             {
                 Object child =  new Object();
                 child.title =Constant.child[i][j][k];
                 parent.children.add(child);
             }
             root.children.add(parent); 
        }
        obj.children.add(root);
    }

    if (!obj.children.isEmpty()) {
        final ExpandableListView elv = (ExpandableListView) findViewById(R.id.expList);

        elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {

                return true; /* or false depending on what you need */;
            }
        });


        ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                    long id) {

                return true/* or false depending on what you need */;
            }
        };


        ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
                    int childPosition, long id) {

                return true/* or false depending on what you need */;
            }
        };

        ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

            }
        };

        final RootAdapter adapter = new RootAdapter(this, obj, grpLst, childLst, grpExpLst);
        elv.setAdapter(adapter);
}


}
 }

2:Object.java

public class Object {
public String title; // use getters and setters instead
public List<Object> children; // same as above

public Object() {
    children = new ArrayList<Object>();
}
}

3: RootAdapter.java

public class RootAdapter extends BaseExpandableListAdapter {

private Object root;

private final LayoutInflater inflater;

public class Entry {
    public final CustExpListview cls;
    public final SecondLevelAdapter sadpt;

    public Entry(CustExpListview cls, SecondLevelAdapter sadpt) {
        this.cls = cls;
        this.sadpt = sadpt;
    }
}

public Entry[] lsfirst;

public RootAdapter(Context context, Object root, ExpandableListView.OnGroupClickListener grpLst,
    ExpandableListView.OnChildClickListener childLst, ExpandableListView.OnGroupExpandListener grpExpLst) {
    this.root = root;
    this.inflater = LayoutInflater.from(context);

    lsfirst = new Entry[root.children.size()];

    for (int i = 0; i < root.children.size(); i++) {
        final CustExpListview celv = new CustExpListview(context);
        SecondLevelAdapter adp = new SecondLevelAdapter(root.children.get(i),context);
        celv.setAdapter(adp);
        celv.setGroupIndicator(null);
        celv.setOnChildClickListener(childLst);
        celv.setOnGroupClickListener(grpLst);
        celv.setOnGroupExpandListener(grpExpLst);

        lsfirst[i] = new Entry(celv, adp);
    }

}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return root.children.get(groupPosition);
}

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

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
    View convertView, ViewGroup parent) {
    // second level list
    return lsfirst[groupPosition].cls;
}

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

@Override
public Object getGroup(int groupPosition) {
    return root.children.get(groupPosition);
}

@Override
public int getGroupCount() {
    return root.children.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    ViewGroup parent) {

    // first level

    View layout = convertView;
    GroupViewHolder holder;
    final Object item = (Object) getGroup(groupPosition);

    if (layout == null) {
        layout = inflater.inflate(R.layout.item_root, parent, false);
        holder = new GroupViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.itemRootTitle);
        layout.setTag(holder);
    } else {
        holder = (GroupViewHolder) layout.getTag();
    }

    holder.title.setText(item.title.trim());

    return layout;
}

private static class GroupViewHolder {
    TextView title;
}

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

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

4: SecondLevelAdapter.java

public class SecondLevelAdapter extends BaseExpandableListAdapter {

public Object child;
Context mContext;
LayoutInflater inflater;

public SecondLevelAdapter(Object child,Context context) {
    this.child = child;
    this.mContext=context;
    inflater = LayoutInflater.from(mContext);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return child.children.get(groupPosition).children.get(childPosition);
}

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

// third level
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {
    View layout = convertView;
    final Object item = (Object) getChild(groupPosition, childPosition);

    ChildViewHolder holder;

    if (layout == null) {
        layout = inflater.inflate(R.layout.item_child, parent, false);

        holder = new ChildViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.itemChildTitle);
        layout.setTag(holder);
    } else {
        holder = (ChildViewHolder) layout.getTag();
    }

    holder.title.setText(item.title.trim());

    return layout;
}

@Override
public int getChildrenCount(int groupPosition) {
    return child.children.get(groupPosition).children.size();
}

@Override
public Object getGroup(int groupPosition) {
    return child.children.get(groupPosition);
}

@Override
public int getGroupCount() {
    return child.children.size();
}

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

// Second level
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {
    View layout = convertView;
    ViewHolder holder;

    final Object item = (Object) getGroup(groupPosition);

    if (layout == null) {
        layout = inflater.inflate(R.layout.item_parent, parent, false);
        holder = new ViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.itemParentTitle);
        layout.setTag(holder);
    } else {
        holder = (ViewHolder) layout.getTag();
    }

    holder.title.setText(item.title.trim());

    return layout;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    super.registerDataSetObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    Log.d("SecondLevelAdapter", "Unregistering observer");
    if (observer != null) {
        super.unregisterDataSetObserver(observer);
    }
}

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

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

private static class ViewHolder {
    TextView title;
}

private static class ChildViewHolder {
    TextView title;
}

}

5 Constant.java

public class Constant {
static String[] state = {"A","B","C"};
static  String[][] parent = {
        {"aa","bb","cc","dd","ee"},
        {"ff","gg","hh","ii","jj"},
        {"kk","ll","mm","nn","oo"}
    };

static  String[][][] child = {
            {
                {"aaa","aab","aac","aad","aae"},
                {"bba","bbb","bbc","bbd","bbe"},
                {"cca","ccb","ccc","ccd","cce","ccf","ccg"},
                {"dda","ddb","dddc","ddd","dde","ddf"},
                {"eea","eeb","eec"}
            },
            {
                {"ffa","ffb","ffc","ffd","ffe"},
                {"gga","ggb","ggc","ggd","gge"},
                {"hha","hhb","hhc","hhd","hhe","hhf","hhg"},
                {"iia","iib","iic","iid","iie","ii"},
                {"jja","jjb","jjc","jjd"}
            },
            {
                {"kka","kkb","kkc","kkd","kke"},
                {"lla","llb","llc","lld","lle"},
                {"mma","mmb","mmc","mmd","mme","mmf","mmg"},
                {"nna","nnb","nnc","nnd","nne","nnf"},
                {"ooa","oob"}
            }
        };
}

5: item_parent.xml

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

<TextView
    android:id="@+id/itemParentTitle"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="#5ccccc"
    android:padding="2dp"
    android:textColor="#006363"
    android:textSize="20sp" />

<ImageView
    android:id="@+id/itemParentImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

如果我将第二个子视图设置为fill_parent作为宽度,那么右侧的图像就不会显示出来,请帮我解决这个问题,提前致谢。 - Samadhan Medge
找到了解决方案,问题不在XML布局中,而是在Java代码中。我将这两行代码进行了替换:widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST); heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST); 替换为 heightMeasureSpec = MeasureSpec.makeMeasureSpec(900, MeasureSpec.AT_MOST); - Samadhan Medge
1
在MainActivity.java类中,有ExpandableListView.OnGroupClickListener grpLst,这是第二层监听器,还有ExpandableListView.OnChildClickListener childLst,这是第三层监听器。 - Cropper
Cropper,干得好!我卡在如何在第一级项目上设置监听器,你能帮我吗? - hemant
1
子列表未打开。 - Anand Jain
显示剩余10条评论

2
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{
    // 999999 is a size in pixels. 
    // ExpandableListView requires a maximum height in order to do measurement calculations.
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

这样可以使长度更加合理。

只是很奇怪。我的二级标题在高度为2000像素时显示有误(它被对齐到了左侧),当高度为600像素时,情况会变得更好,但当它超过5行时,有些行会超出视图范围。但是根据你的建议,现在它可以完美地运作了。请问你知道我可以设置多少像素,以便正确处理多个级别的问题吗? - grabarz121
我找到了一些代码,但是现在找不到了。999999是最大的度量标准,因此计算是基于最大数量进行的,然后将其计算为实际大小。但是其他代码并没有按预期工作,但999999却完美地运行。谢谢。 - Alp Altunel

2
使用可扩展列表视图并创建第二个可扩展适配器是一种不推荐的方法,它会使整个过程变得复杂。有一种简单的方法是通过使用滚动视图和内部线性布局来膨胀不同的视图...

这更像是对现有答案的评论,而不是新答案。 - slfan
实际上,我正在发布一个链接,您将在其中找到制作可扩展列表视图的高效方法。第二级适配器不起作用,并且出现了一些复杂情况,一些必需的功能无法正常工作。 - Nouman Shah

0
public class CustExpListview extends ExpandableListView {

    public CustExpListview(Context context) {
        super(context);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDetachedFromWindow() {
        try {
            super.onDetachedFromWindow();
        } catch (IllegalArgumentException e) {
            // TODO: Workaround for
            // http://code.google.com/p/android/issues/detail?id=22751
        }
    }
}

是的,我已经检查了这个链接,但如果列表大小很大,那么在这个链接中也会有同样的问题。 - Cropper
问题出在onMesure()方法。 - user2702578
你能和我分享你的项目吗? - user2702578
1
问题出在 onMesure() 方法上,我已经创建了一个示例应用程序,与问题中的链接所示应用程序相同。 - Cropper
嘿,你能发布所有的XML文件吗? - Anand Jain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接