android:列表视图中的列表视图 [英] android: listview in listview

查看:37
本文介绍了android:列表视图中的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个列表视图放入一个列表视图项中.内部列表视图不应该是可滚动的,而是采用显示所有行所需的所有大小.有没有更好的方法来解决这个问题?桌子,网格,...?我现在面临的问题是内部列表视图没有占用它需要的空间,所以它在第一个列表项的末尾被切断.如果我尝试滚动,只有外部列表视图正在滚动,这正是我想要的.

i'm trying to place a listview inside a listviewitem. the inner listview should not be scrollable but take all size it needs to display all it's rows. is there a better way to to this? table, grid, ...? the problem i'm facing right now is that the inner listview doesn't take the space it needs, so it's cut at about the end of the first listitem. if i try to scroll, just the outer listview is scrolling which is exactly what i want.

谢谢,我的最终解决方案是

thanks, my final solution is

LinearLayout layout = (LinearLayout) row.findViewById(R.id.LLBroadcasts);
layout.removeAllViews();

for (Item b : bs.getItems()) {

  View child = _inflater.inflate(R.layout.item_row, null);

  TextView tvTitle = (TextView) child.findViewById(R.id.TVItemTitle);
  tvTitle.setText(b.getTitle());

  TextView tvDesc = (TextView) child.findViewById(R.id.TVItemDescription);
  tvDesc.setText(b.getDescription());
  layout.addView(child);
}

推荐答案

来自 Android 文档 - Listview:ListView 是一个显示可滚动项目列表的视图组

From the Android documentation - Listview: ListView is a view group that displays a list of scrollable items

您并不是真的想滚动内部列表视图,而是想要滚动外部列表视图.但是我认为内部列表视图可能会因它包含的元素数量而异.

You do not really want to scroll that inner list view, you want to scroll the outer listview. However I asume that the inner listview may vary on the amount of elements it contains.

代替内部列表视图,您可以使用

Instead of the inner list view you could use a

  • linear layout, see this tutorial or look at Adding content to a linear layout dynamically?
  • table layout

对于线性布局(一些示例代码):

For the linear layout (some sample code):

// access your linear layout
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
// load the xml structure of your row
View child = getLayoutInflater().inflate(R.layout.row);
// now fill the row as you would do with listview
//e.g. (TextView) child.findViewById(...
...
// and than add it
layout.addView(child);

您应该在视图持有者中保存线性布局(请参阅视图持有者模式).我认为 removeAllViews() 仅当当前行的内部行少于重用的行时才需要,所以我也会保存视图持有者中的行数.

You should save the linear layout in a view holder (see View Holder pattern). I think the removeAllViews() is only necessary when the current row has lesser inner rows than the reused one, so I would also save the number of rows in the view holder.

如果内部行的最大数量不是很高,您还可以考虑将它们缓存在视图持有者中以避免膨胀和 findByViewId(比如在 ArrayList 中).

If the maximum number of inner rows is not to high you could also think about caching them in the view holder to avoid the inflate and findByViewId (lets say in an ArrayList).

这篇关于android:列表视图中的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆