如何在 RecyclerView Header Decor 中设置监听器? [英] How can I set a listener inside a RecyclerView Header Decor?

查看:36
本文介绍了如何在 RecyclerView Header Decor 中设置监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 RecyclerView 中使用 这个库

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)

它忽略了我,点击了后面的项目(recyclerview列表的背景选择器).

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

onBindHeaderViewHolder 正在工作,因为我在那里有 setText.我想知道一种方法可以使 OnClickListener 持续存在并且视图接收它.(如果可能,不回收).

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

推荐答案

2016 年 3 月 29 日的 现在可以使用 ViewHolder 的相同侦听器点击粘滞标题,不再使用 物品装饰.而是使用 FrameLayout.

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.

答案:我曾在 header-decor 上工作过在 sticky-headers-recyclerview 上,我的结论是,ItemDecoration 不应该是实现真正好的粘性标题的方法,因为我们所拥有的只是在顶部绘制的东西,不是视图(!),所以我们不能在上面附加任何监听器东西,我们不能与那个东西交互.

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.

但是...我能够在我的 FlexibleAdapter 项目中, 来拦截 sticky 头部的点击,现在它成为了这个功能的高级解决方案.

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.

在这里查看我的评论:

另一个非常重要的注意事项:

我在顶部提到的 2 个项目,大量使用循环 for 语句:它们在顶部多次绘制,作为装饰"项目(使用 onDrawOver),当前显示的所有标题.不仅,他们将所有视图和对位置的引用没有回收(!)它们.

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.

你可以想象每次滚动列表的每个像素时,内存增加了多少,手机做了多少工作,电池消耗巨大……嗯……我已经在新版本中删除了所有这些东西StickyHeaderHelper.

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.

这篇关于如何在 RecyclerView Header Decor 中设置监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆