LoginSignup
1
1

More than 3 years have passed since last update.

RecyclerView内のTextViewをSelectableにする方法

Last updated at Posted at 2019-10-28

AndroidでRecyclerView内のTextViewをSelectableにする方法です。
今更な感じもあると思いますが、あまり情報がなかったので。

僕の場合は複数のFragmentでRecycledViewPoolを共有しているため、Fragmentを切り替えて戻ってきた時に、再度選択ができない状態になっていました。
また、RecyclerViewのAdapterに設定する方法もありますが、Groupieを使っているためCustomViewを作る形をとりました。

SelectableTextView.kt
class SelectableTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) 
    : AppCompatTextView(context, attrs) {

    init {
        // Selectableにする
        setTextIsSelectable(true)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        // 再度アタッチされた時にSelectableにならないバグの回避
        isEnabled = false
        isEnabled = true
    }
}

参考

http://y-anz-m.blogspot.com/2012/10/androidtextview.html
http://y-anz-m.blogspot.com/2019/06/recyclerview-textview-textisselectable.html

1
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
1
1