Android粘头RecyclerView/节头RecyclerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24859266/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:51:37  来源:igfitidea点击:

Android sticky header RecyclerView/ Section Header RecyclerView

androidandroid-recyclerview

提问by Pramod

Hello All I have just created a demo to work with new android L widget RecyclerView.I have also implemented Pull-To-Refresh using SwipeRefreshLayout but problem for me now is how can I implement sticky header here,Because when i try to set

大家好,我刚刚创建了一个演示来使用新的 android L 小部件 RecyclerView。我还使用 SwipeRefreshLayout 实现了下拉刷新,但现在对我来说问题是如何在这里实现粘性标题,因为当我尝试设置时

mRecyclerView.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrolled(int arg0, int arg1) {

    }

    @Override
    public void onScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
});

I get only these 2 methods so dont have any idea how can I handle this...

我只得到这两种方法所以不知道我该如何处理这个......

Please help..

请帮忙..

采纳答案by Davideas

Since the previous answers don't provide a reliable solution, I propose my FlexibleAdapterlibrary for RecyclerView, that is able to handle all following functionalities at once:

由于之前的答案没有提供可靠的解决方案,我为 RecyclerView提出了我的FlexibleAdapter库,它能够同时处理以下所有功能:

  • Sticky functionality for headers with Sections, works with all 3 LayoutManagers and with ViewPager too.
  • Selection Modes.
  • Multiple item types with auto-mapping.
  • Predefined ViewHolders.
  • Expandable items with Selection Coherence.
  • Draggable and Swipe-To-Dismiss.
  • Animated Async Filter with spannable text.
  • Scrolling Animations.
  • EndlessScroll with Adapter binding.
  • UndoHelper & ActionMode Helper.
  • FastScroller.
  • ...and more.
  • 带有部分的标题的粘性功能,适用于所有 3 LayoutManagers 和 ViewPager。
  • 选择模式。
  • 具有自动映射的多种项目类型。
  • 预定义的 ViewHolders。
  • 具有选择一致性的可扩展项目。
  • 可拖动和滑动即可关闭。
  • 带有可跨文本的动画异步过滤器。
  • 滚动动画。
  • EndlessScroll 与 Adapter 绑定。
  • UndoHelper 和 ActionMode 助手。
  • 快速滚动器。
  • ...和更多。

The idea behind is to avoid to create from scratch over again a custom Adapter for every project and to have more functionalities in one library, instead of relying on different libraries that support only 1 or 3 of them and that you cannot merge.

背后的想法是避免为每个项目从头开始重新创建自定义适配器,并在一个库中拥有更多功能,而不是依赖于仅支持其中 1 或 3 个且无法合并的不同库。

回答by Budius

public void onScrolled(int dx, int dy)

public void onScrolled(int dx, int dy)

those are the two arguments that you're receiving onScrolled, that means, the number of pixels that the RecyclerView changed on the X and Y axis... so probably all you want to do is:

这些是您收到的两个参数onScrolled,这意味着 RecyclerView 在 X 和 Y 轴上更改的像素数......所以您可能只想做的是:

@Override
public void onScrolled(int dx, int dy) {
   if(dx < 0) // going up
      showSitckyHeader();
}

you can probably further improve this implementation by adding a minimum scroll amount. Something like:

您可以通过添加最小滚动量来进一步改进此实现。就像是:

int totalScrolled = 0;
@Override
public void onScrolled(int dx, int dy) {
   totalScrolled += dx;
   if(totalScrolled < MIN_SCROLL)
      showSitckyHeader();
   if(dx > 0)
      totalScrolled = 0;
}

@Override
public void onScrollStateChanged(int newState) {
   if(newState == SCROLL_STATE_IDLE || newState = SCROLL_STATE_SETTLING)
       totalScrolled = 0;
}

or even go further and implement speed, counting time, but those types of implementation are more tricky, and you have to test it yourself.

甚至更进一步,实现速度,计算时间,但那些类型的实现更棘手,你必须自己测试。