单击RecyclerView列表项

TWi*_*Rob 8 android android-layout android-listview recycler-adapter android-recyclerview

我有RecyclerView一个LinearLayoutManagerAdapter:

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

这是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"
    />
Run Code Online (Sandbox Code Playgroud)

我想要实现的是在我可以点击的地方找到一个.我尝试了很多组合以下属性无济于事:RecyclerViewRecyclerView

      android:background="@android:color/transparent"
      android:background="@null"
      android:clickable="false"
      android:focusable="false"
      android:focusableInTouchMode="false"
      android:longClickable="false"
Run Code Online (Sandbox Code Playgroud)

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

小智 10

您可以将所有"洞" 的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) {
                // http://stackoverflow.com/questions/8121491/is-it-possible-to-add-a-scrollable-textview-to-a-listview
                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
            }
        });
Run Code Online (Sandbox Code Playgroud)

XML中没有什么特别需要的,只是普通的视图(问题中没有属性). v.getParent()就是RecyclerView那种长方法阻止它在将手指放在"洞"上时开始滚动手势.

列表中的"洞"视图也具有半透明背景,但这并不重要,因为我们手动传递触摸.