20

2つのフラグメントListMovieFragmentDetailMovieFragmentがあります。フラグメントの共有トランジションを使用しているとき、戻り遷移が正しく機能しない

MainActivityに実装されているListMovieFragmentのインターフェイスがあります。共有要素の遷移を使用しています。 の画像ビューをクリックすると、MainActivityにはonMovieSelectedが呼び出されます。

ListMovieFragmentから移行します。
しかし、の背面のボタンをクリックすると、DetailMovieFragmentからListMovieFragmentへの移行が失敗します。

gif of what is going wrong

ここMainActivityです。私はフラグメントのトランジションを設定するための不適切な組み合わせがあると思います。

public class MainActivity extends AppCompatActivity implements ListMovieFragment.MovieSelectedListener { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if(savedInstanceState == null) { 
      ListMovieFragment listMovieFragment = new ListMovieFragment(); 

      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.add(R.id.activity_main, listMovieFragment, ListMovieFragment.TAG); 
      fragmentTransaction.commit(); 
     } 
    } 

    @Override 
    public void onMovieSelected(int movieId) { 
     DetailMovieFragment detailMovieFragment = 
       (DetailMovieFragment)getSupportFragmentManager().findFragmentByTag(DetailMovieFragment.TAG); 
     /* Create a new DetailMovieFragment if not exits */ 
     if(detailMovieFragment == null) { 
      detailMovieFragment = new DetailMovieFragment(); 
     } 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      /* Get the fragments that will be using the transition */ 
      ListMovieFragment listMovieFragment = 
        (ListMovieFragment)getSupportFragmentManager().findFragmentByTag(ListMovieFragment.TAG); 
      if(listMovieFragment == null) { 
       listMovieFragment = new ListMovieFragment(); 
      } 

      /* Inflate the transition */ 
      Transition changeTransition = TransitionInflater 
        .from(MainActivity.this) 
        .inflateTransition(R.transition.change_image_transform); 

      /* source fragment (ListMovieFragment) */ 
      listMovieFragment.setExitTransition(new Explode()); 
      listMovieFragment.setSharedElementReturnTransition(changeTransition); 

      /* Destination fragment (DetailMovieFragment) */ 
      detailMovieFragment.setSharedElementEnterTransition(changeTransition); 
      detailMovieFragment.setEnterTransition(new Explode()); 

      /* Get the shared imageview from the source fragment (MovieListFragment) */ 
      final ImageView ivSharedImage = (ImageView) findViewById(R.id.ivMoviePoster); 

      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.activity_main, detailMovieFragment, DetailMovieFragment.TAG); 
      fragmentTransaction.addToBackStack(DetailMovieFragment.TAG); 
      fragmentTransaction.addSharedElement(ivSharedImage, getResources().getString(R.string.transition_poster_image)); 
      fragmentTransaction.commit(); 
     } 
     else { 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.activity_main, detailMovieFragment, DetailMovieFragment.TAG); 
      fragmentTransaction.addToBackStack(DetailMovieFragment.TAG); 
      fragmentTransaction.commit(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
      getSupportFragmentManager().popBackStack(); 
     } 
     else { 
      super.onBackPressed(); 
     } 
    } 
} 

マイ遷移xmlファイル:

<transitionSet> 
    <changeBounds /> 
</transitionSet> 

fragment_list

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="18dp" 
    android:paddingBottom="6dp"> 

    <ImageView 
     android:id="@+id/ivMoviePoster" 
     android:layout_width="184dp" 
     android:layout_height="276dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:transitionName="@string/transition_poster_image"/> 
</LinearLayout> 

fragment_detail

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidbox.fragmenttransitions.detail.DetailMovieFragment"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="The Movie App" 
     android:textSize="28sp" 
     android:fontFamily="sans-serif-light" 
     android:textColor="@android:color/holo_blue_dark"/> 

    <ImageView 
     android:id="@+id/ivMoviePoster" 
     android:layout_width="92dp" 
     android:layout_height="138dp" 
     android:layout_marginEnd="16dp" 
     android:layout_marginTop="112dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:layout_gravity="end" 
     android:transitionName="@string/transition_poster_image"/> 
</FrameLayout> 

activity_main
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidbox.fragmenttransitions.activity.MainActivity"> 
</FrameLayout> 
+0

移行が正常に機能しているように見えますが、ポスターの最初の枠の外には表示されません。最初の画面サイズやポスターの初期サイズを変更するとどうなりますか? – cokceken

+0

activity_mainとフラグメントのレイアウトを共有できますか? –

+0

@NileshSinghレイアウトファイルを追加しました – ant2009

答えて

10

fragment_list.xmlを以下のように変更すると、android:layout_width/heightのプロパティが変更されます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingTop="18dp" 
    android:paddingBottom="6dp"> 

    <ImageView 
     android:id="@+id/ivMoviePoster" 
     android:layout_width="184dp" 
     android:layout_height="276dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:transitionName="@string/transition_poster_image"/> 
</LinearLayout> 
+0

それはそれほど簡単ではないと思います。私はトランジションに間違ったことをしていると思った。ありがとう。 – ant2009

+0

@cokceken私はコードがこの変更なしで動作すると思います。 match_parent以外の変更はありませんか? –

+0

@Nilesh Singhそれはレイアウトのサイズです。大きな画像やたくさんの画像があれば、それは役に立ちます – cokceken

関連する問題