LoginSignup
13
11

More than 5 years have passed since last update.

RecyclerView内のアイテムに対して2点以上の同時クリックができてしまう・・・

Posted at

自由過ぎてたまに困ってしまうRecyclerView(内で取り扱うView)に対してあれこれClickListenerを実装すると、2点以上の同時クリックでクリック分のコールバックが走ってしまいます。

例えば同時クリックイベントでダイアログ表示、Fragment管理、Activity遷移などの処理が走るとレッツパーリィになってしまうので、RecyclerViewのsplitMotionEventsを無効にして、RecyclerView内のViewに対する複数のタッチイベント処理を制御した方が良いです。

参考 ViewGroup/attr_android:spilitMotionEvents
http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:splitMotionEvents
※RecyclerViewクラスはViewGroupを継承しています

xml内での実装例

<android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:splitMotionEvents="false"
        />
javaでの実装例
  mRecyclerView.setMotionEventSplittingEnabled(false);

補足1

ViewGroupのspilitMotionEventsのdefaultはfalseだよと公式リファレンス内に記載がありますが、RecyclerView#isMotionEventSplittingEnabledで値をみるとデフォルトはtrueになってました。

補足2

spilitMotionEvents関連のAPIは3系(API11)から。
2系のRecyclerView関連の動作は調べてない。

13
11
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
13
11