简体   繁体   English

将ListView Stick的标题视图(不是节标题)放在顶部

[英]Make Header View (not Section Header) of ListView Stick on Top

I have a ListView with a HeaderView. 我有一个带有HeaderView的ListView。

I want one of the views in the HeaderView to stick on top. 我希望HeaderView中的一个视图能够粘在上面。

I've seen a lot of examples for sticky Section Headers. 我已经看到很多关于粘性节标题的例子。

I also looked at StickyScrollViewItems but since I'm using a ListView , I cannot use a ScrollView . 我也查看了StickyScrollViewItems,但由于我使用的是ListView ,我无法使用ScrollView

Is there a library available for this or should I just override the OnScrollListener of the ListView ? 是否有可用的库,或者我应该覆盖ListViewOnScrollListener

Thanks. 谢谢。

I don't know of any library that lets you do this but what I do for a header is to use a RelativeLayout for my xml that will have the ListView . 我不知道有任何库允许你这样做,但我为标题做的是使用RelativeLayout为我的xml将具有ListView Then create your header view however you want and give it the property 然后根据需要创建标题视图并为其提供属性

android:layout_alignParentTop="true"

then give your ListView 然后给你的ListView

android:layout_below="@id/idOfHeaderView

This is the easiest way I know of to dock a header view at the top. 这是我所知道的将标题视图停靠在顶部的最简单方法。 It has worked for me every time. 它每次都对我有用。 I hope this helps. 我希望这有帮助。

I have just written a load of code that does this that I cant share for contractoral reasons. 我刚刚编写了大量代码来执行此操作,但由于合同原因我无法共享。 Basically follow the approach outlined here and apply to a listview rather than a scroll view. 基本上遵循此处概述的方法并应用于列表视图而不是滚动视图。

Main points are 要点是

  1. Create a wrapper view that contains your floating/sticky header and your listview as siblings 创建一个包装器视图,其中包含浮动/粘性标头和listview作为兄弟
  2. Use a proxy method when adding headers with an isSticky boolean - if is sticky then add a fake blank header to the listview of the same size and your intended header view to the floating header wrapper (use a relative layout here) 使用isSticky布尔值添加标头时使用代理方法 - 如果是粘滞的,则将相同大小的列表视图中的假空白标题添加到浮动标题包装器的预期标题视图中(在此处使用相对布局)
  3. Set a scroll listener of the listview that tracks the top px position of the dummy header view in the list and setting this as a top margin of the floating header that sits inside a relative layout 设置listview的滚动侦听器,该侦听器跟踪列表中虚拟标题视图的顶部px位置,并将其设置为位于相对布局内的浮动标题的上边距
  4. Handle all the annoying edge cases / OEM overscroll crap to get it to work in all situtions (like this for samsung) 处理所有恼人的边缘情况/ OEM反弹时废话得到它的所有工作situtions(像这样的三星)

Remember to set the initial position of the floating/pinned header after the listview has been layed out. 在列出视图布局后,请记住设置浮动/固定标题的初始位置。

I feel its a little bit involved and takes some tweaking to get right - this is a time where i envy iOS and any iOS devs will think your slacking as it takes a while to implement :D 我觉得它有点牵扯并需要一些调整才能做到正确 - 这是我羡慕iOS的时候,任何iOS开发人员都会认为你需要一段时间才能实现松弛:D

All the Open libs out there at time of writing are using scroll views or list view headers. 在编写本文时,所有Open libs都使用滚动视图或列表视图标题。 This issue with these approaches are that list views recycle views (so unlike scroll views you cant just keep a ref to the dummy view) and also the current libs seem duplicate the sticky views using adapter getView methods and so on - which is no good for dynamic list view header views as they are not recycled and can only have one parent, so reparenting would be annoying (and in my case has a lot of functionality so I defo dont want to create two views of the same type and shoehorn the current libs to fit my solutions) 这些方法的问题在于列表视图循环视图(因此不像滚动视图那样只能保留对虚拟视图的引用),并且当前的库似乎使用适配器getView方法等重复粘贴视图 - 这对于动态列表视图标题视图,因为它们不被回收,只能有一个父级,所以重新定位会很烦人(在我的情况下有很多功能,所以我不想创建两个相同类型的视图,并且当前的libs)适合我的解决方案)

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

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