如何生成一个ListView,其标题位于某些部分之上?
我想生成一个 ListView,它在某些条目之间有一些分隔线,就像在某些属性部分中可以看到的那样。请参阅下面的示例。我尝试生成一个 List
,其中包含一些 textviews,后跟一个解释列表下一部分的精美分隔符,然后又是一些文本视图。这怎么能做到呢?我考虑过创建不同的视图来添加到列表中?这是要走的路吗?
I want to generate a ListView
that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List
that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我得到了一个解决方案。我不知道这是否是最好的。
我使用从 ArrayAdapter 派生的自定义适配器作为列表如本教程中所述。在适配器类中,我检查 getView 方法中的位置是否是普通行,然后膨胀行布局。如果它是新组的第一行,我会膨胀标题布局,该布局是普通行加上其上方的组标题。
如果您不想将标题混合到其中一行中。考虑以下解决方案:
您可以覆盖两个方法 getItemViewType< /a> 和 getViewTypeCount。
您现在拥有一个可以显示不同行的列表。您需要在 getView 方法中检查项目的预期视图类型,并根据它来扩展不同的布局。
该列表将为您处理回收,只将正确的回收视图返回给您的 getView 方法,这意味着如果 recycleView 不为 null,则它可用于显示当前单元格。
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
您可以使用我的
SectionedAdapter
,如果 GPLv3 是可接受的(由于某些上游代码而以这种方式获得许可)。如果您需要更灵活的东西,您可以使用我的MergeAdapter
具有较少限制的许可证 (Apache 2)。You can use my
SectionedAdapter
, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use myMergeAdapter
, if you need something more flexible and with a less-limiting license (Apache 2).我想您可能正在寻找 android.widget.ExpandableListView
http://developer. android.com/reference/android/widget/ExpandableListView.html
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
我也对这个问题的答案感兴趣。必须有一种更直接的方法来做到这一点。
在查看 Adapter 时,有一个方法,Adapter.getItemViewType(intposition)。
ListView 定义一个返回值 ITEM_VIEW_TYPE_HEADER_OR_FOOTER ,指示返回的项目是页眉还是页脚。
我还没有尝试过,但我假设如果您创建自己的适配器并返回一个类型指示它是页眉或页脚的项目,则 ListView 将正确显示它。
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.