How can I set a listener inside a RecyclerView Header Decor?

元气小坏坏 提交于 2019-11-27 08:29:07

问题


I implemented sticky headers in my RecyclerView, using this library

I want to add a clickable view inside the custom header, but when I set the listener in the

public void onBindHeaderViewHolder(HeaderHolder viewholderHeader, int position)

It ignores me, and the item of behind is clicked (background selector of the recyclerview list).

the onBindHeaderViewHolder is working, because of the setText I have in there. I want to know a way to do that the OnClickListener persists and the view receives it. (Without recycling if it is possible).


回答1:


EDIT of 29/03/2016: Sticky Headers are now clickable using same listeners of the ViewHolder, not using anymore ItemDecoration. A FrameLayout is instead used.

Answer: I've worked on header-decor and on sticky-headers-recyclerview and my conclusion is that, ItemDecoration should not be the way to implement a real good sticky headers, because what we have is just something drawn on the top and is not a view(!) so we cannot attach any listener on that thing and we cannot interact with that thing.

But... I was able, in my FlexibleAdapter project, to intercept the tap on the sticky header, now it is become an advanced solution for this functionality.

See my comments here:

  • https://github.com/timehop/sticky-headers-recyclerview/issues/105#issuecomment-187144374
  • https://github.com/davideas/FlexibleAdapter/issues/41

Another very important note:

The 2 projects, I mentioned on the top, make heavy use of cycle for statement: they draw, as a "decoration" many times on the top of the items (with onDrawOver), all the headers currently displayed. Not only, they put in a cache ALL the views and the reference to the positions without recycling(!) them.

You can imagine how much the memory increases and how much work the phone is making every time the list is scrolled of every pixel, with a huge battery consummation... Huh... I have removed all these things in the new StickyHeaderHelper.



来源:https://stackoverflow.com/questions/33018788/how-can-i-set-a-listener-inside-a-recyclerview-header-decor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!