如何处理 RecyclerView.ItemDecoration 中的点击事件? [英] How to handle click-event in RecyclerView.ItemDecoration?

查看:52
本文介绍了如何处理 RecyclerView.ItemDecoration 中的点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RecyclerView(带有 LinearLayoutManager)和一个自定义的 RecyclerView.ItemDecoration.

比方说,我想在装饰视图中有按钮(出于某种原因......).

我用按钮填充布局,它可以正确绘制.但我无法使按钮可点击.如果我按下它,什么也没有发生(它保持不变,没有按下效果)并且 onClick 事件不会触发.

ItemDecoration 布局的结构是

<TextView/><按钮/></LinearLayout>

我正在尝试在装饰的 ViewHolder 中设置侦听器

class ItemDecorationHolder 扩展 RecyclerView.ViewHolder {公共 TextView 标题;公共按钮按钮;公共 HeaderHolder(查看 itemView){超级(项目视图);header = (TextView)itemView.findViewById(R.id.header);button = (Button)itemView.findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@覆盖公共无效onClick(查看视图){//.. 显示吐司等}});}}

我正在 onDrawOver 方法中绘制装饰.(实际上,我正在修改此代码库:https://github.com/edubarr/header-decor)>

有什么想法吗?可行吗?

谢谢!

解决方案

虽然真正的标题是滚出屏幕的,但可见的却是直接在画布上绘制,不像普通的交互式小部件.

你有这些选择

  1. 覆盖 RecyclerView.onInterceptTouchEvent(),虽然有一些侵入性,所以我更喜欢下一个.
  2. 利用 RecyclerView.addOnItemTouchListener(),记住运动事件参数已经转换成 RecyclerView 的坐标系.
  3. 使用真正的标题视图,但我认为这会有点远.

<块引用>

如果您选择选项 1/2,Button.setPressed(true) 并重绘标题将具有视觉按压效果.

I have a RecyclerView (with LinearLayoutManager) and a custom RecyclerView.ItemDecoration for it.

Let's say, I want to have buttons in the decoration view (for some reason..).

I inflate the layout with button, it draws properly. But I can't make the button clickable. If I press on it, nothing happening(it stays the same, no pressing effect) and onClick event is not firing.

The structure of ItemDecoration layout is

<LinearLayout>
  <TextView/>
  <Button/>
</LinearLayout>

And I'm trying to set listener in ViewHolder of the decoration

class ItemDecorationHolder extends RecyclerView.ViewHolder {
    public TextView header;
    public Button button;

    public HeaderHolder(View itemView) {
        super(itemView);

        header = (TextView)itemView.findViewById(R.id.header);
        button = (Button)itemView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //.. Show toast, etc.
            }
        });
    }
}

And i'm drawing the decoration in onDrawOver method. (actually, I'm modifying this codebase: https://github.com/edubarr/header-decor )

Any ideas? Is it doable?

Thanks!

解决方案

While the real header is scroll off the screen, the visible one is drawing on canvas directly ,not like a normal interactive widget.

You have these options

  1. Override RecyclerView.onInterceptTouchEvent(), though with some invasiveness so I prefer the next one.
  2. Make use of RecyclerView.addOnItemTouchListener(), remember the motion event argument has been translated into RecyclerView's coordinate system.
  3. Use a real header view, but that will go a little far I think.

If you take option 1/2, Button.setPressed(true) and redraw the header will have a visual press effect.

这篇关于如何处理 RecyclerView.ItemDecoration 中的点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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