【发布时间】:2010-06-28 18:31:17
【问题描述】:
是否有可能/可取的嵌套列表视图?
即包含在另一个列表视图的一行中的列表视图?
一个例子是我的主列表显示博客帖子,然后在每一行中,您将有另一个列表视图,用于每个帖子的 cmets(可折叠)
【问题讨论】:
是否有可能/可取的嵌套列表视图?
即包含在另一个列表视图的一行中的列表视图?
一个例子是我的主列表显示博客帖子,然后在每一行中,您将有另一个列表视图,用于每个帖子的 cmets(可折叠)
【问题讨论】:
我今天遇到了同样的问题,所以我就是这样做来解决它的:
我有一个 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 在行内创建一个列表并使用代码填充它。
【讨论】:
您正在寻找的是ExpandableListView 吗?当然,这仅限于两个级别的列表(但这听起来可以满足您的需求)。
【讨论】:
This 听起来像您要找的东西?如果你不是,或者如果这不起作用,我建议有两个列表视图:一个,比如说,博客文章,第二个是 cmets,对博客文章项目的操作会将你带到第二个视图, 填充了相关的 cmets。
【讨论】:
你可以这样做:
在父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>
在您的父列表视图适配器 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 可折叠,只需更改您提供数据的方式而不是通知适配器。
【讨论】: