单击 RecyclerView 列表项 [英] Click through a RecyclerView list item
问题描述
我有一个带有 LinearLayoutManager
和 Adapter
的 RecyclerView
:
I have a RecyclerView
with a LinearLayoutManager
and an 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
后面的任何内容.我尝试了很多以下属性的组合都无济于事:
What I want to achieve is to have a hole in the RecyclerView
where I can click through to anything behind the 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"
如何让列表中的第一个项目透明(允许触摸事件到它后面的任何东西),但让它仍然占据空间?
推荐答案
您可以将所有孔"的 OnClickListener
/OnTouchListener
设置为RecyclerView
后面的视图并将任何 MotionEvent
和触摸事件委托给该父 ViewGroup
的触摸处理.
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.
由 TWiStErRob 更新:
Update by 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
}
});
XML 中没有什么特别需要的,只是简单的视图(问题中没有任何属性).v.getParent()
是 RecyclerView
长方法阻止它在将手指放在洞"上时启动滚动手势.
There's nothing special needed in the XML, just plain views (none of the attributes in the question).
v.getParent()
is the RecyclerView
and that long method stops it from starting a scroll gesture while holding your finger on the "hole".
我在列表中的洞"视图也有一个半透明的背景,但这并不重要,因为我们是手工提供触摸的.
My "hole" view in the list also has a semi-transparent background, but that doesn't matter because we're hand-delivering the touches.
这篇关于单击 RecyclerView 列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!