[英]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: 我尝试过以下方法:
recyclerView.setClickable(false);
recyclerView.setFocusable(false);
RecyclerView
and make onTouchEvent(MotionEvent)
return false
. RecyclerView
并使onTouchEvent(MotionEvent)
返回false
。 itemView.setClickable(false);
itemView.setClickable(false);
in the RecyclerView
Adapter. RecyclerView
适配器中。 This works, the click is sent to the parent. RecyclerView
is not scrollable anymore. RecyclerView
不再可滚动了。 clickable="false"
, focusable="false"
, focusableInTouchMode="false"
in inflated list item XML. clickable="false"
, focusable="false"
, focusableInTouchMode="false"
。 (See comment @Ibrahim ) recyclerView#setLayoutFrozen(true)
and itemView.setClickable(false);
recyclerView#setLayoutFrozen(true)
和itemView.setClickable(false);
. 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. 所以不幸的是,这不是一个解决方案。
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.