单击 RecyclerView 列表项

Click through a RecyclerView list item

我有一个 RecyclerView 和一个 LinearLayoutManager 和一个 Adapter:

@Override public int getItemViewType(int position) {
    return position == 0? R.layout.header : R.layout.item;
}

这里是header.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/header"
      android:layout_width="match_parent"
      android:layout_height="@dimen/header_height"
    />

我想要实现的是在 RecyclerView 中有一个 ,我可以在其中点击进入 RecyclerView 后面的任何内容。我尝试了很多组合以下属性无济于事:

      android:background="@android:color/transparent"
      android:background="@null"
      android:clickable="false"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:longClickable="false"

如何让列表中的第一个项目透明(允许触摸事件到它后面的任何东西),但让它仍然占据space?

您可以将所有 "holes" 的 OnClickListener/OnTouchListener 设置为 RecyclerView 后面的视图的父视图,并委托任何 MotionEvent 并将触摸事件发送给父 ViewGroup 的触摸处理。

TWiStErRob 更新:

class HeaderViewHolder extends RecyclerView.ViewHolder {
    public HeaderViewHolder(View view) {
        super(view);
        view.setOnTouchListener(new OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
                // 
                v.getParent().requestDisallowInterceptTouchEvent(true); // needed for complex gestures
                // simple tap works without the above line as well
                return behindView.dispatchTouchEvent(event); // onTouchEvent won't work
            }
        });

XML 中没有什么特别需要的,只是简单的视图(none 问题中的属性)。 v.getParent()RecyclerView 并且那个长方法阻止它在将手指放在 "hole" 上时开始滚动手势。

我在列表中的 "hole" 视图也有一个半透明的背景,但这并不重要,因为我们是手工处理的。