问题描述
任何人都可以适应 PinnedHeaderListView ,以便它可以与 ExpandableListView 一起使用一个带有索引节的简单ListView?我基本上想要一个ExpandableListView
,其中每个组项目视图保持固定在顶部,直到被下一个组视图向上推.
Has anyone had any luck adapting PinnedHeaderListView so that it can be used with an ExpandableListView instead of just a simple ListView with indexed sections? I basically want an ExpandableListView
where each group item view stays pinned to the top until it is pushed up by the next group view.
我已经研究了代码以尝试弄清PinnedHeaderListView
的工作方式,并且似乎很难适应ExpandableListView
.主要问题似乎在于使用不同类型的适配器和绘图方法. PinnedHeaderListView
使用SectionIndexer
来跟踪节的位置.用getView()
绘制每个项目时,它会检查该项目是否是新节的开始.如果该项目是新节的开始,则会使该节的标题在该项目的list_item
视图中在之内可见. ExpandableListAdapter
具有getChildView()
和getGroupView()
可以将项目和部分分别绘制为不同的列表项目.
I've studied the code to try and figure out how PinnedHeaderListView
works and it seems like it will be difficult to adapt to an ExpandableListView
. The main problem seems to be in the use of a different kind of adapter and drawing methodology. A PinnedHeaderListView
makes use of SectionIndexer
to keep track of section positions. As it draws each item with getView()
it checks if the item is the beginning of a new section. If the item is the beginning of a new section it makes a section header visible within the item's list_item
view. An ExpandableListAdapter
has a getChildView()
and a getGroupView()
to draw the items and sections separately as different list items.
我确信必须有某种方法可以使用PinnedHeaderListView
中的方法在ExpandableListView
中获得类似的行为,但是我不确定从哪里开始.
I am sure there must be some way of using the methodology in PinnedHeaderListView
to get similar behavior in an ExpandableListView
, but I am not sure where to begin.
推荐答案
我能够在 ExpandableList1 APIdemo .
我面临的最困难的问题是弄清楚如何使SectionIndexer在扩展列表中很好地发挥作用.正如我的问题所证明的那样,我在想这一切都是错误的.在解决该问题的最初尝试中,我在MyExpandableAdapter中创建了一个SectionIndexer对象,并将其映射到我的数据,类似于在Contacts应用程序和Peter的示例中的操作.这在Contacts应用程序中有效,因为平面列表的位置与数据集静态匹配.在可扩展列表视图中,平面图组的位置随组的扩展和扩展而变化.
The hardest problem I faced was figuring out how to get the SectionIndexer to play nicely with expanding lists. As was evidenced in my question, I was thinking of this all wrong. In my original attempt to solve this problem, I created a SectionIndexer object within MyExpandableAdapter and mapped it to my data, similar to how it is done in the Contacts app and Peter's example. This works in the Contacts app because the flat list positions statically match the data set. In an expandable list view the flat list positions change as groups are expanded in and out.
因此,解决方案是不将节索引器映射到数据,而是映射到ExpandableListView的实例.使用此解决方案,您甚至不需要示例中使用的SectionIndexer对象.您只需要像这样在ExpandableListView方法周围包装SectionIndexer实现方法:
So, the solution is to not to map the section indexer to the data, but to the instance of your ExpandableListView. With this solution, you don't even need a SectionIndexer object as used in the examples. You just need to wrap the SectionIndexer implementation methods around the ExpandableListView methods like this:
@Override
public int getPositionForSection(int section) {
return mView.getFlatListPosition(ExpandableListView
.getPackedPositionForGroup(section));
}
@Override
public int getSectionForPosition(int position) {
return ExpandableListView.getPackedPositionGroup(mView
.getExpandableListPosition(position));
}
当然,您还需要进行其他更改才能使所有这些正常工作,但是上述方法才是关键.我将完整的代码发布到Google代码或github上,并从此处链接.带有固定标头的ExpandableList看起来很棒!
There are of course other changes you have to make to get this all working, but the above methods are the key. I'll post the full code to google code or github and link from here soon. ExpandableLists with pinned headers look great!
这篇关于如何在ExpandableListView中获得粘性/固定标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!