Android 如何在某些部分上方生成带有标题的 ListView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2394514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a ListView with headers above some sections?
提问by Janusz
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 textviewsfollowed 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?
我想生成一个ListView
在某些条目之间有一些分隔线的,就像在某些属性部分中可以看到的那样。请参阅下面的示例。我尝试生成List
一个包含一些textviews其次是看中分隔再次解释列表的下一部分,然后一些文本画面之一。如何才能做到这一点?我想过创建不同的视图来添加到列表中吗?这是要走的路吗?
回答by Janusz
I got a solution. I don't know if it is the best one.
我得到了解决方案。不知道是不是最好的
I use a custom adapter derived from ArrayAdapterfor 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.
我使用从ArrayAdapter派生的自定义适配器作为本教程中所述的列表。在适配器类中,我检查 getView 方法中的位置是否为正常行,然后我膨胀行布局。如果它是一个新组的第一行,我会增加一个标题布局,它是一个普通行加上它上面的组标题。
If you don't want to mix the header into one of your rows. Consider the following solution:
如果您不想将标题混合到您的行之一中。考虑以下解决方案:
You can overwrite the two methods getItemViewTypeand 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.
您可以覆盖两个方法getItemViewType和getViewTypeCount。您现在有一个可以显示不同行的列表。您需要在 getView 方法中检查项目的预期视图类型,并根据它膨胀不同的布局。
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.
该列表将以一种方式为您处理回收,它只会将正确的回收视图返回给您的 getView 方法,这意味着如果 recycleView 不为空,它可用于显示您当前的单元格。
回答by CommonsWare
You can use my SectionedAdapter
, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter
, if you need something more flexible and with a less-limiting license (Apache 2).
您可以使用 my SectionedAdapter
, 如果 GPLv3 可以接受(由于某些上游代码而以这种方式获得许可)。MergeAdapter
如果您需要更灵活且限制更少的许可证(Apache 2),您可以使用 my 。
回答by CommonsWare
I think you might be looking for android.widget.ExpandableListView
我想你可能正在寻找 android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
http://developer.android.com/reference/android/widget/ExpandableListView.html
回答by David
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).
在查看适配器时,有一个方法,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.
ListView 定义了一个返回值 ITEM_VIEW_TYPE_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.
我还没有尝试过,但我假设如果您创建自己的适配器并返回一个类型指示它是页眉或页脚的项目,ListView 将适当地显示它。