0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Layoutタップ時にRecyclerViewもタップ対象とする方法

Last updated at Posted at 2020-09-07

親のLayoutに android:clickable="true" してもRecyclerView領域タップに反応してくれない。そんなときの対応方法。

setAdapterの後に suppressLayout(true) をするだけでOK。
setLayoutFrozen(boolean) は Deprecated になりました。
https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView#suppresslayout

タッチイベントやスクロールが無効になるので、当然RecyclerViewのアイテムがClickListener等のイベントを持っていても反応しなくなります。

setAdapterの後なのは、setAdapter内でfalseに設定されているためです。

// recyclerview-1.1.0-sources.jar
public void setAdapter(@Nullable Adapter adapter) {
    // bail out if layout is frozen
    setLayoutFrozen(false);
    setAdapterInternal(adapter, false, true);
    processDataSetCompletelyChanged(false);
    requestLayout();
}

@Deprecated
public void setLayoutFrozen(boolean frozen) {
    suppressLayout(frozen);
}

suppressLayout(true) の1行を追加するだけで良いですが、ソースコード上で行う必要があるためレイアウトXML上からは分からなくなってしまいます。なので私が実装する際にはCustomViewを作成しています。

package com.example.myapp

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.RecyclerView

class SuppressRecyclerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    @SuppressLint("PrivateResource") defStyleAttr: Int = R.attr.recyclerViewStyle
) : RecyclerView(context, attrs, defStyleAttr) {

    override fun setAdapter(adapter: Adapter<*>?) {
        super.setAdapter(adapter)
        suppressLayout(true)
    }
}
<com.example.myapp.SuppressRecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    tools:itemCount="3"
    tools:listitem="@layout/list_card" />

以上

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?