簡體   English   中英

單擊RecyclerView列表項

[英]Click through a RecyclerView list item

我有一個帶有LinearLayoutManagerAdapterRecyclerView

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM