[英]Click through a RecyclerView list item
我有一個帶有LinearLayoutManager
和Adapter
的RecyclerView
:
@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"
如何讓列表中的第一個項目透明(允許觸摸事件到它后面的任何東西),但讓它仍然占據空間?
您可以將所有“漏洞”的OnClickListener
/ OnTouchListener
設置為RecyclerView
后面的視圖的父級,並將任何MotionEvent
和touch事件委托給該父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
}
});
XML中沒有什么特別需要的,只是普通的視圖(問題中沒有屬性)。 v.getParent()
是RecyclerView
,該long方法阻止它在將手指放在“洞”上時開始滾動手勢。
列表中的“洞”視圖也具有半透明背景,但這並不重要,因為我們手動傳遞觸摸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.