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

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

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

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

Android Studio

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

Kotlin

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

Q&A

1回答

626閲覧

Databindingを使ったListViewにitemのタッチイベントが取れない

ShoutaInoue

総合スコア28

Android

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

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2018/07/30 04:16

編集2022/01/12 10:55

お世話になっております。
Android開発初心者のです。
現在DatabindingをつかったListViewにてitemのタッチイベントの取得ができません。
handlerのinterfaceをMainActivityにて実装して
layoutにてonItemClickListenerを設定するというやり方を
したのですが上手くいきません。
ご教授頂けましたら幸いです。
宜しくお願い致します。

####参考URL
Android DataBinding 〜ListViewで使う〜

####MainActivity

kotlin

1class MainActivity : AppCompatActivity(), BindingListEventHandler { 2 var viewModel = TestViewModel() 3 4 override fun onCreate(savedInstanceState: Bundle?) { 5 super.onCreate(savedInstanceState) 6 val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) 7 binding.viewModel = this.viewModel 8 } 9 10 override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { 11 Log.d("DEBUG", "CellTap") 12 val list = parent as BindingListView 13 var msg = (list.getItemAtPosition(position) as BindingTestValue).name 14 Toast.makeText(this, msg, Toast.LENGTH_LONG) 15 } 16}

####BindingListEventHandler

Kotlin

1import android.view.View 2import android.widget.AdapterView 3import com.example.shotainoue.listviewdatabindingtest.Components.Adapters.BindingTestAadapter 4 5interface BindingListEventHandler { 6 fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) 7}

####actitity_main.xml

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<layout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 xmlns:app="http://schemas.android.com/apk/res-auto"> 5 <data> 6 <variable name="viewModel" type="com.example.shotainoue.listviewdatabindingtest.ViewModels.TestViewModel" /> 7 <variable 8 name="handlers" 9 type="com.example.shotainoue.listviewdatabindingtest.Components.EventHandlers.BindingListEventHandler"/> 10 </data> 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:orientation="vertical" 15 android:background="@drawable/default_back" 16 tools:context=".UserInterface.Activity.ShiftPatternListActivity"> 17 <!-- ヘッダーエリア--> 18 <RelativeLayout 19 android:layout_width="match_parent" 20 android:layout_height="0dp" 21 android:layout_weight="10"> 22 <Button 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_centerVertical="true" 26 android:layout_marginLeft="5dp" 27 android:layout_marginStart="5dp" 28 android:background="@drawable/rounded_btn_default" 29 android:includeFontPadding="false" 30 android:minHeight="0dp" 31 android:minWidth="0dp" 32 android:paddingBottom="15dp" 33 android:paddingEnd="10dp" 34 android:paddingStart="10dp" 35 android:paddingTop="15dp" 36 android:text="@string/backButton" 37 android:textColor="@color/colorWhite" 38 android:textSize="14sp"/> 39 <TextView 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:layout_centerInParent="true" 43 android:text="@string/shiftPatternSetting" 44 android:textColor="@color/colorTitleText" 45 android:textSize="17sp" 46 android:textStyle="bold" 47 tools:ignore="RelativeOverlap" /> 48 <Button 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="@string/sendButton" 52 android:textColor="@color/colorWhite" 53 android:textSize="14sp" 54 android:minWidth="0dp" 55 android:minHeight="0dp" 56 android:includeFontPadding="false" 57 android:layout_centerVertical="true" 58 android:layout_alignParentRight="true" 59 android:layout_alignParentEnd="true" 60 android:layout_marginRight="10dp" 61 android:layout_marginEnd="10dp" 62 android:paddingStart="10dp" 63 android:paddingEnd="10dp" 64 android:paddingTop="15dp" 65 android:paddingBottom="15dp" 66 android:background="@drawable/rounded_btn_default" 67 tools:ignore="RelativeOverlap"/> 68 </RelativeLayout> 69 <RelativeLayout 70 android:layout_width="match_parent" 71 android:layout_height="0dp" 72 android:layout_weight="90"> 73 <com.example.shotainoue.listviewdatabindingtest.Components.BindingListView 74 android:id="@+id/list" 75 android:layout_width="match_parent" 76 android:layout_height="match_parent" 77 android:headerDividersEnabled="true" 78 android:footerDividersEnabled="true" 79 android:divider="@color/colorGray215" 80 android:dividerHeight="1dp" 81 android:scrollbars="none" 82 android:onItemClickListener="@{handlers.onItemClick}" 83 app:list="@{viewModel.list}"/> 84 </RelativeLayout> 85 </LinearLayout> 86</layout>

####list_item.xml

kotlin

1<?xml version="1.0" encoding="utf-8"?> 2<layout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:descendantFocusability="blocksDescendants"> 4 <LinearLayout 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content" 7 android:background="@color/colorWhite" 8 android:paddingStart="10dp" 9 android:paddingEnd="10dp" 10 android:orientation="horizontal"> 11 <TextView 12 android:id="@+id/name" 13 android:layout_width="0dp" 14 android:layout_height="wrap_content" 15 android:layout_weight="20" 16 android:text="aaaa" 17 android:textSize="14sp" 18 android:textColor="@color/colorWhite" 19 android:layout_margin="10dp" 20 android:paddingLeft="10dp" 21 android:paddingRight="10dp" 22 android:paddingTop="2dp" 23 android:paddingBottom="2dp" 24 android:gravity="center" 25 android:background="@color/colorBlack" 26 android:focusable="false"/> 27 <TextView 28 android:id="@+id/term" 29 android:layout_width="0dp" 30 android:layout_height="match_parent" 31 android:layout_weight="50" 32 android:text="testだよ" 33 android:textSize="14sp" 34 android:focusable="false" 35 android:gravity="center_horizontal|center_vertical" 36 android:textColor="@color/colorBlack"/> 37 <Switch 38 android:id="@+id/isEnable" 39 android:layout_width="0dp" 40 android:layout_height="match_parent" 41 android:layout_weight="15" 42 android:checked="false" 43 android:focusableInTouchMode="false" 44 android:focusable="false" 45 android:gravity="center_horizontal|center_vertical" /> 46 <ImageView 47 android:layout_width="0dp" 48 android:layout_height="match_parent" 49 android:layout_weight="5" 50 android:src="@drawable/arrow3_r" 51 android:scaleType="centerInside" 52 android:layout_gravity="end|center_vertical" 53 android:contentDescription="@string/image_right_arrow" /> 54 </LinearLayout> 55</layout>

####対応したこと
layoutに下記を設定しましたが上手くいきませんでした。
android:descendantFocusability="blocksDescendants"

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

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

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

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

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

guest

回答1

0

その記事では説明が省略されてますが、 binding.handlers のセットをしていないからではないでしょうか?

kotlin

1val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) 2binding.viewModel = this.viewModel 3binding.handlers = this // これを追加

投稿2018/08/01 09:53

kakajika

総合スコア3131

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問