LoginSignup
9
7

More than 5 years have passed since last update.

RecyclerViewやListView内で、SurfaceViewを再利用して使う時に黒くなって困っている方へ

Last updated at Posted at 2015-08-11

VineやInstagramのようにListの中で動画再生したいときってありますよね?

VideoViewを使えば、実装はそこまで難しくはないとは思いますが、
何かしら問題が発生します。。。

僕が個人的にハマっていた中の一つが、これ。
たまに黒くなる
ってことです。

SurfaceViewのおかげでスクロール中に他のViewを飛び越えて画面の一部が黒くなったり、
時にはスクロールして消えたはずの動画が下の方でこっそり流れていたりしました。
(こういったバグは僕だけでしょうか.....???)

このたまに黒くなるバグですが、ならない方法を見つけたので共有したいと思います。
治らなかったらごめんなさい......

※ちなみに補足として、このバグはVideoViewでもSurfaceView×ExoPlayerの組み合わせでも出ました。あとxmlで囲っているViewGroupをwidth:match_parent/height:match_parentにしている時に起きます。

解決策はこれ

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/videoFrame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="0.1dp"
    android:layout_marginLeft="0.1dp">

    <SquareExoVideoView
        android:id="@+id/square_video_exo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal" />

    <SquareImageView
        android:id="@+id/video_thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal" />
</FrameLayout>

大小問わない枠を付けると治ります。
SurfaceViewをmatch_parent状態にすると、重なっている別Viewが上に来るのを難しくしてるのかな??? 経験が浅いため、明確な回答が出せず申し訳ないです。。。

以上!

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