android嵌套的listview

有可能/build议有一个嵌套的列表视图?

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

一个例子就是我的主要列表显示博客post的地方,然后在每一行中,你将有另一个列表视图来显示每个post的评论(这是可以折叠的)

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

我有一个ListView,一个CustomAdapter,并在getAdapter的getAdapter,我有这样的事情:

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在行内创build一个列表,并用代码填充它。

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

这听起来像你在找什么? 如果你没有,或者如果这不起作用,我会build议有两个列表视图:其中之一,比如说博客post,第二个评论,以及对博客文章项目的操作,将你带到第二个视图,填写相关评论。

你可以这样做:

在父级listview行的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); } 

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

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