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

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

问题描述

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

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

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

ItemDecoration布局的结构为

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

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

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

我正在用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天全站免登陆