LoginSignup
0
2

More than 5 years have passed since last update.

setCustomAnimationsがViewPagerの中のFragmentに効かない問題

Last updated at Posted at 2017-02-22

直面した問題

Fragment#replaceを呼ぶときに setCustomAnimations でenterとexitのアニメーションを指定しているのに、ViewPagerの中のFragmentがパッと消える問題に直面した。
elevation関係で重ねてる他のViewの後ろに回るとかしてるのかと思っていろいろ調べて、 https://code.google.com/p/android/issues/detail?id=163384 に行き着いたけど、どうやらこれは関係なかった。

原因

setCustomAnimations はViewPagerの様なNestedなFragmentには適用されず、パッと消えるとのこと。たしかによくよく考えたらフェードアウトのアニメーションが親と子で2重に掛かったりしたら大変だ。

対策

いろいろ調べた結果、Stack Overflowの↓に行き着いた。

Fragment#onCreateAnimationメソッドはFragmentの追加時/削除時のアニメーションの生成を担っているので、該当する子フラグメント(ViewPagerの中身)を親フラグメント(ViewPagerの持ち主)がisRemoving()==trueのときはアルファ値を1に固定するなどしてあげると、パッと消えるような見た目にはならない。

2017/07/14追記

Support Libraryの25.x.x系で内部の変数の持ち方が変わったので、↑の方法をそのまま適用すると動きません。変数名を変えてあげると動くようになります。

0
2
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
0
2