2010-06-28 140 views
27

是否有可能/建议有一个嵌套的listview?android嵌套列表视图

即包含在另一个listview的行内的listView?

一个例子就是,我的主要列表显示的博客文章,然后在每一行中,你必须为每个岗位的注释的另一列表视图(这将是可折叠)

回答

31

今天我有同样的问题,所以这是我做过什么来解决这个问题:

我有一个ListView,具有CustomAdapter,并在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); 
} 

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

+0

列表监听器不能正常工作。任何解决方案 – 2014-04-10 17:01:39

+0

谢谢队友。好主意啊。 – 2015-03-02 13:31:32

+0

我想在列表中实现列表,但我不能实现.is有解决此问题的任何替代方法吗?除了Expandable ListView – seon 2017-06-05 06:27:10

6

This听起来像什么您正在寻找?如果你不是,或者这不起作用,我会建议拥有两个列表视图:其中一个是博客帖子,另一个是评论,而对博客文章项目的操作会将你带到第二个视图,并填写相关评论。

+0

不幸的是,我们想要显示内嵌评论...所以在另一个活动中打开它们不会工作。 – Ben 2010-06-28 18:44:10

10

是你在找什么ExpandableListView?当然,这仅限于两层清单(但听起来像是可以满足您的需求)。

+0

好吧,事情是,我不希望列表是exapandible ...我只是想显示一些嵌套的数据...即原来的博客文章,然后2/3最近的评论,也许跟着一个'显示所有评论'按钮... – Ben 2010-06-28 22:42:13

+0

这听起来像一个不同的布局给我。我将它看作是某种类型的容器(LinearLayout,RelativeLayout,其他),其中包含用于博客文章的TextView,以及用于评论的ListView。只需将前几个注释加载到列表视图中,并为最后一个加载剩余注释的列表项实现一个点击监听器。无论如何,我的$ .02。 – kiswa 2010-06-28 23:19:55

+0

好,除了每篇博客文章都会有多篇博文和多篇评论。所以我尝试了一个测试,通过让我的行视图布局包含一个列表视图,然后我使用了一个简单的数组列表适配器w /一些硬编码的虚拟字符串,以查看是否可以让它呈现。它呈现,内部列表视图似乎有滚动条,但该列表不滚动。它看起来像一个黑客入侵的iframe,除了它只显示第一行,无论我做了什么。即使height = fill_parent也不行。我的下一个方法是尝试嵌套在列表中的表视图。 – Ben 2010-06-29 01:01:59

0

,你可以做这样的:

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

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

那么你已经做出了与名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> 
在你父母的ListView适配器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); 
      } 
0

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

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