android嵌套列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3135112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:51:47  来源:igfitidea点击:

android nested listview

androidlistviewnested

提问by Ben

is it possible/advisable to have a nested listview?

是否可以/建议使用嵌套的列表视图?

i.e. a listView that's contained within a row of another listview?

即包含在另一个列表视图的一行中的列表视图?

an example would be where my main list is displaying blog posts, and then in each row, you'd have another list view for the comments for each post (that would be collapsible)

一个例子是我的主列表显示博客文章,然后在每一行中,你会有另一个列表视图来显示每篇文章的评论(这将是可折叠的)

回答by Paulo Cesar

I had the same problem today, so this is what I did to solve it:

我今天遇到了同样的问题,所以这就是我为解决它所做的:

I have a ListView, with a CustomAdapter, and on the getView of the customAdapter, I have something like this:

我有一个带有 CustomAdapter 的 ListView,在 customAdapter 的 getView 上,我有这样的东西:

LinearLayout list = (LinearLayout) myView.findViewById(R.id.list_musics);
list.removeAllViews();

for (Music music : albums.get(position).musics) {
    View line = li.inflate(R.layout.inside_row, null);

    /* nested list's stuff */

    list.addView(line);
}

So, resuming, It's not possible to nest to ListViews, but you can create a list inside a row using LinearLayout and populating it with code.

因此,继续说,不可能嵌套到 ListViews,但您可以使用 LinearLayout 在一行内创建一个列表并用代码填充它。

回答by kiswa

Is what you're looking for the ExpandableListView? Of course, that's limited to only two levels of listings (but that sounds like it would work for your needs).

您正在寻找ExpandableListView吗?当然,这仅限于两个级别的列表(但这听起来可以满足您的需求)。

回答by Kris

Thissound like what you're looking for? If you're not, or if this doesn't work, I would suggest having two list views: one of, say, blog posts, and the second of comments, and an action on a blog post item takes you to the second view, populated with the relevant comments.

听起来像您要找的吗?如果你不是,或者如果这不起作用,我建议有两个列表视图:一个是博客文章,另一个是评论,对博客文章项目的操作会将你带到第二个视图,填充了相关评论。

回答by Ahmed Esameddeen

you can do it like this :

你可以这样做:

inside the parent listview row xml layout add the following table layout

在父列表视图行 xml 布局中添加以下表格布局

   <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/table_show"
        android:background="#beb4b4">
    </TableLayout>

then you have to make a layout for the child list with name reply_row.xml

那么你必须为名为reply_row.xml的子列表做一个布局

<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_reply_row"
        android:textColor="#000"/>
</TableRow>

in your parent listview adapter getview method add the following code :

在您的父列表视图适配器 getview 方法中添加以下代码:

TableLayout replyContainer = (TableLayout) 
// vi is your parent listview inflated view
vi.findViewById(R.id.table_show); 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


       //child listview contents list
        String [] replys = {"a","b","c","d"};

        for (int i=0;i<replys.length;i++)
        {
        final View comments = inflater.inflate(R.layout.reply_row, null);
        TextView reply_row = (TextView) comments.findViewById(R.id.tv_reply_row) ;
        reply_row.setText(replys[i]);

//for changing your tablelayout parameters 
        TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
                      (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

        int leftMargin=3;
        int topMargin=2;
        int rightMargin=3;
        int bottomMargin=2;
        tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        comments.setLayoutParams(tableRowParams);
            TableRow tr = (TableRow) comments;
            replyContainer.addView(tr);
          }

回答by user3510029

You'd better use one ListView, not nested. Nesting ListView is an inefficient way. Your ListView may not scroll smoothly and take up more memory.

你最好使用一个 ListView,而不是嵌套。嵌套 ListView 是一种低效的方式。您的 ListView 可能无法平滑滚动并占用更多内存。

You could organize your data structure to show nested data in one ListView. Or you can use this project PreOrderTreeAdapter. It is convenient to show nested data in ListView or RecyclerView. It can be used to make ListView or RecyclerView collapsible, just change the way you provide your data than notify the adapter.

您可以组织数据结构以在一个 ListView 中显示嵌套数据。或者你可以使用这个项目PreOrderTreeAdapter。在 ListView 或 RecyclerView 中显示嵌套数据很方便。它可用于使 ListView 或 RecyclerView 可折叠,只需更改提供数据的方式而不是通知适配器。