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

发布于 2024-11-14 03:29:45 字数 965 浏览 10 评论 0原文

有没有人有幸改编 PinnedHeaderListView 这样它就可以与 ExpandableListView 一起使用,而不仅仅是一个带有索引部分的简单 ListView?我基本上想要一个 ExpandableListView,其中每个组项目视图保持固定在顶部,直到它被下一个组视图推上。

我研究了代码,试图弄清楚 PinnedHeaderListView 是如何工作的,但似乎很难适应 ExpandableListView。主要问题似乎在于使用不同类型的适配器和绘图方法。 PinnedHeaderListView 使用 SectionIndexer 来跟踪部分位置。当它使用 getView() 绘制每个项目时,它会检查该项目是否是新部分的开始。如果该项目是新部分的开头,它会使部分标题在该项目的 list_item 视图中可见。 ExpandableListAdapter 具有 getChildView()getGroupView(),用于将项目和部分分别绘制为不同的列表项目。

我确信必须有某种方法可以使用 PinnedHeaderListView 中的方法来在 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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-11-21 03:29:45

我能够在 ExpandableList1 APIdemo

我面临的最困难的问题是弄清楚如何让SectionIndexer 很好地处理扩展列表。正如我的问题所证明的那样,我的想法都是错误的。在我最初尝试解决此问题时,我在 MyExpandableAdapter 中创建了一个 SectionIndexer 对象并将其映射到我的数据,类似于在联系人应用程序和 Peter 的示例中完成的方式。这在联系人应用程序中有效,因为平面列表位置与数据集静态匹配。在可扩展列表视图中,平面列表位置随着组的扩展而变化。

因此,解决方案是不要将部分索引器映射到数据,而是映射到 ExpandableListView 的实例。使用此解决方案,您甚至不需要示例中使用的SectionIndexer 对象。您只需将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));
    }

当然,您还必须进行其他更改才能使这一切正常工作,但上述方法是关键。我很快会将完整的代码发布到 google code 或 github 并从这里链接。带有固定标题的 ExpandableList 看起来很棒!

I was able to get pinned headers working on the ExpandableList1 APIdemo.

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.

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));
    }

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!

执笔绘流年 2024-11-21 03:29:45

好吧,我只用了 3 步骤:

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

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代码:

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

希望这对您有帮助。

Well I have done in just 3 steps:

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

2. Add ExpandableListView in xml.

<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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文