简体   繁体   English

单击RecyclerView列表项

[英]Click through a RecyclerView list item

I have a RecyclerView with a LinearLayoutManager and an Adapter : 我有一个带有LinearLayoutManagerAdapterRecyclerView

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

Here's header.xml : 这是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"
    />

What I want to achieve is to have a hole in the RecyclerView where I can click through to anything behind the RecyclerView . 我想要实现的是在RecyclerView中有一个 ,我可以在其中点击RecyclerView后面的任何内容。 I tried a lot of combinations the following attributes to no avail: 我尝试了很多组合以下属性无济于事:

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

How could I make the first item transparent (allowing touch events to anything behind it) in the list, but let it still occupy the space? 如何让列表中的第一个项目透明(允许触摸事件到它后面的任何东西),但让它仍然占据空间?

You could set the OnClickListener / OnTouchListener of all of your "holes" to the parent of the view behind the RecyclerView and delegate any of the MotionEvent and touch events to that parent ViewGroup 's touch handling. 您可以将所有“漏洞”的OnClickListener / OnTouchListener设置为RecyclerView后面的视图的父级,并将任何MotionEvent和touch事件委托给该父ViewGroup的触摸处理。

Update by TWiStErRob: 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
            }
        });

There's nothing special needed in the XML, just plain views (none of the attributes in the question). XML中没有什么特别需要的,只是普通的视图(问题中没有属性)。 v.getParent() is the RecyclerView and that long method stops it from starting a scroll gesture while holding your finger on the "hole". v.getParent()RecyclerView ,该long方法阻止它在将手指放在“洞”上时开始滚动手势。

My "hole" view in the list also has a semi-transparent background, but that doesn't matter because we're hand-delivering the touches. 列表中的“洞”视图也具有半透明背景,但这并不重要,因为我们手动传递触摸。

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

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