简体   繁体   English

如何使RecyclerView无法点击并将onClick事件传递给父视图?

[英]How to make a RecyclerView not clickable and pass onClick event to parent view?

Is it possible to make a RecyclerView not clickable? 是否有可能使RecyclerView无法点击? I want this because my RecyclerView just shows some small icons, within a clickable CardView . 我想要这个,因为我的RecyclerView只是在一个可点击的 CardView显示一些小图标。 So if someone taps the icons, it should just click (and animate) the parent CardView instead. 因此,如果有人点击图标,它应该只是单击(和动画)父CardView

I have tried the following: 我尝试过以下方法:

  1. recyclerView.setClickable(false);
  2. recyclerView.setFocusable(false);
  3. Extend RecyclerView and make onTouchEvent(MotionEvent) return false . 扩展RecyclerView并使onTouchEvent(MotionEvent)返回false
  4. Use method 3 above and use itemView.setClickable(false); 使用上面的方法3并使用itemView.setClickable(false); in the RecyclerView Adapter. RecyclerView适配器中。 This works, the click is sent to the parent. 这样做,点击发送给父母。 However, now the RecyclerView is not scrollable anymore. 但是,现在RecyclerView不再可滚动了。
  5. Set clickable="false" , focusable="false" , focusableInTouchMode="false" in inflated list item XML. 在膨胀的列表项XML中设置clickable="false"focusable="false"focusableInTouchMode="false" (See comment @Ibrahim ) (见评论@Ibrahim
  6. Call recyclerView#setLayoutFrozen(true) and itemView.setClickable(false); 调用recyclerView#setLayoutFrozen(true)itemView.setClickable(false); . This works, but has the same issue as #4. 这有效,但与#4有同样的问题。

Any ideas how to disable and pass through the click events of the RecyclerView to the parent view? 有关如何禁用和传递RecyclerView的单击事件到父视图的任何想法? Note that the RecyclerView still needs to be scrollable (horizontal). 请注意, RecyclerView仍然需要可滚动(水平)。


EDIT: 编辑:
User @c.dunlap suggested to set OnClick listeners to the icons, and just "redirect" the click to the parent's click action. User @ c.dunlap建议将OnClick侦听器设置为图标,并将点击“重定向”到父级的单击操作。 This would work, but there won't be a click animation on the parent view. 这可以工作,但父视图上不会有点击动画。 And if someone clicks outside an itemView - but still inside the RecyclerView (eg ItemDecoration padding) - the click is not detected. 如果有人在itemView外部点击 - 但仍在RecyclerView内部(例如ItemDecoration填充) - 则未检测到点击。 So unfortunately this is not a solution. 所以不幸的是,这不是一个解决方案。

You should extend the parent View and intercept the click, so the recyclerview doesn't receive it. 您应该扩展父视图并拦截单击,因此recyclerview不会接收它。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}

Credits to: Solution 致记: 解决方案

My preferred way of handling things like this is to attach a listener from your adapter that will be called when each icon in your recycler view is clicked. 处理这类事情的首选方法是从适配器附加一个监听器,当您单击回收器视图中的每个图标时,该监听器将被调用。 Then your activity can respond in the appropriate way. 然后您的活动可以以适当的方式响应。 For instance: 例如:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private MyAdapterListener mListener;

    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         holder.itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 mListener.iconClicked();
             }
         });
    }

    public void setListener(MyAdapterListener listener) {
        mListener = listener;
    }

    public interface MyAdapterListener {
        void iconClicked();
    }

}

Then in your activity, you can simply create an instance of MyAdapterListener and set it as the listener for your Recycler View's adapter. 然后在您的活动中,您只需创建一个MyAdapterListener实例,并将其设置为Recycler View适配器的侦听器。 Then when iconClicked() function is triggered, execute the code that would be executed on parent click. 然后,当触发iconClicked()函数时,执行将在父单击时执行的代码。

I had same issue, after i trying much more things, i used one more layout over recyclerview. 我有同样的问题,在我尝试了更多的东西之后,我在recyclerview上再使用了一个布局。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:layoutwidth="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

after that you can visible and gone that "LinearLayout". 之后你可以看到并消失了“LinearLayout”。 This works 100%. 这100%工作。

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

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