【问题标题】:How to get sticky / Pinned Headers in an ExpandableListView?如何在 ExpandableListView 中获取粘性/固定标题?
【发布时间】:2011-06-06 06:35:25
【问题描述】:

有没有人幸运地调整了PinnedHeaderListView,以便它可以与ExpandableListView 一起使用,而不仅仅是一个带有索引部分的简单ListView?我基本上想要一个ExpandableListView,其中每个组项目视图都固定在顶部,直到它被下一个组视图向上推。

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

我确信一定有某种方法可以使用PinnedHeaderListView 中的方法来获得ExpandableListView 中的类似行为,但我不确定从哪里开始。

【问题讨论】:

  • 既然@joecan 没有发布完整的代码,那我就做吧。 My solution

标签: android expandablelistview sticky expandablelistadapter


【解决方案1】:

我能够在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));
    }

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

【讨论】:

  • 如果你能制作一个库项目并在 github 上提供它会很好。如果您需要模型,请参阅github.com/triposo/barone
  • @joecan 我也有兴趣了解它是如何实现的。如果您能与我们分享一些代码,请告诉我们。
  • 请让我知道如何使用此方法在可扩展列表视图中添加标题。
  • 上面的代码并不完全适用于手动滚动每个fastScroll thumb。我发布了一个解决方案如何解决这个here。它使用OnScrollListener 来捕获这两种情况。
  • @joecan 很好的信息,但是如果没有在项目中实现几乎毫无用处。请上传代码。
【解决方案2】:

好吧,我只用了 3 个步骤就完成了:

1.build.gradle

中添加compile 'com.diegocarloslima:fgelv:0.1.+@aar'

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

希望这会对你有所帮助。

【讨论】:

  • 不工作,当 targetSdkVersion 是 Android 10+ 时
猜你喜欢
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多