简体   繁体   English

如何在 ExpandableListView 中获取粘性/固定标题?

[英]How to get sticky / Pinned Headers in an 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?有没有人有任何运气调整PinnedHeaderListView以便它可以与ExpandableListView一起使用,而不仅仅是带有索引部分的简单 ListView? 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.我基本上想要一个ExpandableListView ,其中每个组项视图都固定在顶部,直到它被下一个组视图向上推。

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 .我研究了代码,试图弄清楚PinnedHeaderListView是如何工作的,似乎很难适应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. PinnedHeaderListView使用SectionIndexer来跟踪部分位置。 As it draws each item with getView() it checks if the item is the beginning of a new section.当它使用getView()绘制每个项目时,它会检查项目是否是新部分的开始。 If the item is the beginning of a new section it makes a section header visible within the item's list_item view.如果该项目是新部分的开头,则它会使 header 部分项目的list_item视图中可见。 An ExpandableListAdapter has a getChildView() and a getGroupView() to draw the items and sections separately as different list items. ExpandableListAdapter有一个getChildView()和一个getGroupView()来将项目和部分分别绘制为不同的列表项。

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.我确信必须有某种方法使用PinnedHeaderListView中的方法来在ExpandableListView中获得类似的行为,但我不知道从哪里开始。

I was able to get pinned headers working on the ExpandableList1 APIdemo .我能够在ExpandableList1 APIdemo上获得固定的标题。

The hardest problem I faced was figuring out how to get the SectionIndexer to play nicely with expanding lists.我面临的最困难的问题是弄清楚如何让 SectionIndexer 与扩展列表很好地配合。 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.在我最初解决此问题的尝试中,我在 MyExpandableAdapter 中创建了一个 SectionIndexer object 并将其映射到我的数据,类似于在联系人应用程序和彼得的示例中完成的方式。 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.在可展开的列表视图中,平面列表位置会随着组的展开和展开而变化。

So, the solution is to not to map the section indexer to the data, but to the instance of your ExpandableListView.因此,解决方案不是 map 的节索引器到数据,而是到 ExpandableListView 的实例。 With this solution, you don't even need a SectionIndexer object as used in the examples.使用此解决方案,您甚至不需要示例中使用的 SectionIndexer object。 You just need to wrap the SectionIndexer implementation methods around the ExpandableListView methods like this:您只需将 SectionIndexer 实现方法包装在 ExpandableListView 方法周围,如下所示:

    @Override
    public int getPositionForSection(int section) {
        return mView.getFlatListPosition(ExpandableListView
                .getPackedPositionForGroup(section));
    }

    @Override
    public int getSectionForPosition(int position) {
        return ExpandableListView.getPackedPositionGroup(mView
                .getExpandableListPosition(position));
    }

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.我会将完整代码发布到谷歌代码或 github 并很快从这里链接。 ExpandableLists with pinned headers look great!带有固定标题的 ExpandableLists 看起来很棒!

Well I have done in just 3 steps:好吧,我只做了3 个步骤:

1. Add compile 'com.diegocarloslima:fgelv:0.1.+@aar' in build.gradle 1.在build.gradle中添加compile 'com.diegocarloslima:fgelv:0.1.+@aar' '

2. Add ExpandableListView in xml. 2.在xml中添加ExpandableListView

<com.diegocarloslima.fgelv.lib.FloatingGroupExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:groupIndicator="@null" />

3. Java code: 3. Java码:

FloatingGroupExpandableListView expandableListView = (FloatingGroupExpandableListView) findViewById(R.id.expandableListView);
MyexpandableListAdapter adapter = new MyexpandableListAdapter(context, mList);
WrapperExpandableListAdapter wrapperAdapter = new WrapperExpandableListAdapter(adapter);
expandableListView.setAdapter(wrapperAdapter);

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM