質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

1回答

2640閲覧

【Android】RecyclerView内のClickイベントが発生しない

t.k.s-kun

総合スコア0

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/12/05 01:43

前提・実現したいこと

閲覧ありがとうございます。
AndroidStudioにてAndroidアプリの開発中ですが、RecyclerView内のViewにクリックイベントが適用できず困っています。
言語はKotlinです。

発生している問題・エラーメッセージ

下記のソースのようにしてRecyclerAdapter内でViewへOnClickListenerを設定しているのですが、一切反応を起こしません。エラー等も出るわけではなく、Logも出力されません。

該当のソースコード

RecyclerViewのItemレイアウト(rec_item.xml)

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content" 7 android:background="@android:color/transparent" 8 android:clickable="true" 9 android:focusable="auto"> 10 11 <androidx.cardview.widget.CardView 12 android:id="@+id/card_com" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:clickable="true" 16 app:cardCornerRadius="6dp" 17 app:cardElevation="4dp"> 18 19 <LinearLayout 20 android:layout_width="match_parent" 21 android:layout_height="match_parent" 22 android:clickable="true" 23 android:orientation="vertical"> 24 25 <LinearLayout 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:layout_marginStart="4dp" 29 android:clickable="true" 30 android:gravity="center_vertical" 31 android:orientation="horizontal"> 32 33 <TextView 34 android:id="@+id/txt_title" 35 android:layout_width="0dp" 36 android:layout_height="wrap_content" 37 android:layout_weight="5" 38 android:text="TextView" 39 android:textColor="@color/black" 40 android:textSize="24sp" /> 41 42 <Button 43 android:id="@+id/btn_open_detail" 44 android:layout_width="0dp" 45 android:layout_height="wrap_content" 46 android:layout_weight="1" 47 android:clickable="true" 48 android:focusable="auto" /> 49 </LinearLayout> 50 51 <View 52 android:id="@+id/line_color" 53 android:layout_width="match_parent" 54 android:layout_height="4dp" 55 android:background="@color/colorAccent" /> 56 57 <androidx.constraintlayout.widget.ConstraintLayout 58 android:id="@+id/container_detail" 59 android:layout_width="match_parent" 60 android:layout_height="wrap_content" 61 android:clickable="true" 62 android:visibility="gone"> 63 64 <TextView 65 android:id="@+id/txt_detail" 66 android:layout_width="0dp" 67 android:layout_height="wrap_content" 68 android:text="detail" 69 app:layout_constraintStart_toStartOf="parent" 70 app:layout_constraintEnd_toEndOf="parent" 71 app:layout_constraintTop_toTopOf="parent" /> 72 73 </androidx.constraintlayout.widget.ConstraintLayout> 74 </LinearLayout> 75 </androidx.cardview.widget.CardView> 76</LinearLayout>

Fragmentのレイアウト(my_fragment)

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/my_parent" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".views.MyFragment" > 9 10 <androidx.appcompat.widget.Toolbar 11 android:id="@+id/toolbar" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:background="@color/colorPrimary" 15 android:minHeight="?attr/actionBarSize" 16 android:theme="?attr/actionBarTheme" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20 21 <androidx.recyclerview.widget.RecyclerView 22 android:id="@+id/recycler" 23 android:layout_width="0dp" 24 android:layout_height="0dp" 25 app:layout_constraintBottom_toBottomOf="parent" 26 app:layout_constraintEnd_toEndOf="parent" 27 app:layout_constraintStart_toStartOf="parent" 28 app:layout_constraintTop_toBottomOf="@+id/toolbar" /> 29 30</androidx.constraintlayout.widget.ConstraintLayout>

リサイクラーのアダプタ(MyRecyclerAdapter.kt)

kotlin

1class MyRecyclerAdapter(private val fragment: Fragment, private val data: MutableList<String>) : RecyclerView.Adapter<MyRecyclerAdapter.MyRecyclerViewHolder>() { 2 3 class MyRecyclerViewHolder(val view: View) : RecyclerView.ViewHolder(view) { 4 val titleText: TextView = view.txt_title 5 val detailText: TextView = view.txt_detail 6 val openDetailButton: Button = view.btn_open_detail 7 val detailContainer: View = view.container_detail 8 } 9 10 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyRecyclerViewHolder { 11 return MyRecyclerViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.rec_item, parent, false)) 12 } 13 14 override fun onBindViewHolder(holder: MyRecyclerViewHolder, position: Int) { 15 val item = data[position] 16 holder.apply { 17 titleText.text = position.toString() 18 detailText.text = item 19 //詳細オープンボタンのセット 20 //クリックが反応しない 21 openDetailButton.setOnClickListener { 22 Log.d("mytest", "openDetailButton is clicked.") //出力されていないのでまずこのブロックに入ってない 23 when(detailContainer.visibility){ 24 View.VISIBLE -> detailContainer.visibility = View.GONE 25 View.GONE -> detailContainer.visibility = View.VISIBLE 26 } 27 } 28 } 29 } 30 31 override fun getItemCount(): Int = data.size

フラグメント(MyFragment.kt)

kotlin

1class MyFragment : Fragment() { 2 3 private lateinit var mAdapter: MyRecyclerAdapter 4 5 override fun onCreate(savedInstanceState: Bundle?) { 6 super.onCreate(savedInstanceState) 7 arguments?.let {} 8 } 9 10 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { 11 // Inflate the layout for this fragment 12 return inflater.inflate(R.layout.my_fragment, container, false) 13 } 14 15 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 16 super.onViewCreated(view, savedInstanceState) 17 18 mAdapter = MyRecyclerAdapter(this, listOf("A","B","C")) 19 recycler.apply { 20 layoutManager = LinearLayoutManager(activity) 21 adapter = mAdapter 22 } 23 } 24 25 companion object { 26 fun newInstance() = 27 CommunityFragment().apply { 28 arguments = Bundle().apply { } 29 } 30 } 31}

試したこと

xmlファイル内でclickable属性をつけたり外したり、onClickListenerをフラグメントから渡したりしましたが、いまいち何が原因なのかがわからないため困っています。
原因がわかる方いらっしゃいましたら、よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

気になった点は
MyFragmentで行っている

listOf("A","B","C")

です。

mutableListOf("A","B","C")

にしないとビルドが通らないかと。

mutableListOfに変更して提供されていたソースコードをコピペして手元で動かしたところ、
ちゃんと「openDetailButton is clicked.」のログは出てました。画面右端にある灰色のボタンを押すとログが出力されています。

確認ですが、mutableListOfにして画面右端にある灰色のボタンを押してもログが出力されてない状況でしょうか?となると考えられるのは期待通り動いているが何故かログが出力されないとかでしょうか。

例えば

Log.d("mytest", "openDetailButton is clicked.")

の横にブレークポイントを貼ってボタンをクリックするとブレークポイントで止まりませんでしょうか?止まるのであればやはり期待通り動いていて、ログが何故か出力されてなさそうです。

投稿2020/12/09 11:16

Sagamaru

総合スコア70

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問