2016-01-13 33 views
4

我正在嘗試使用共享元素轉換在兩個屏幕之間進行動畫處理。下面是我的代碼的相關部分(可能更多不相關),但我已經刪除了一些以節省空間。共享元素轉換遍歷導航欄

activity_main.xml中:

<android.support.v4.widget.DrawerLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.AppBarLayout ... > 
      ... 
     </android.support.design.widget.AppBarLayout> 

     <fragment 
      android:id="@+id/fragment" 
      android:name=".MainFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      tools:layout="@layout/fragment_main" /> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView ... /> 

</android.support.v4.widget.DrawerLayout> 

fragment_main.xml:

<android.support.v7.widget.RecyclerView 
    android:id="@+id/my_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="8dp" 
    android:scrollbars="vertical" 
    tools:context=".MainFragment" 
    tools:showIn="@layout/activity_main" /> 

在RecyclerView每個條目包含一個CardView作爲根視圖:

card_item.xml:

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <com.google.android.gms.maps.MapView 
      android:id="@+id/map" 
      android:layout_width="match_parent" 
      android:layout_height="180dp" 
      map:liteMode="true" /> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/map" 
      android:transitionName="@string/transition_title" /> 

     <TextView 
      android:id="@+id/subtitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/job_title" 
      android:transitionName="@string/transition_rate" /> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/job_rate" 
      android:transitionName="@string/transition_description" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

在RecyclerView的ViewHolder,我設置onClickListener:

mCardView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent intent = new Intent(mContext, SecondActivity.class); 

     View title = mCardView.findViewById(R.id.title); 
     View rate = mCardView.findViewById(R.id.rate); 
     View description = mCardView.findViewById(R.id.description); 

     // Use fancy animations for API 21+ 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
       mContext.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(mActivity, 
         Pair.create(title, mContext.getString(R.string.transition_title)), 
         Pair.create(rate, mContext.getString(R.string.transition_rate)), 
         Pair.create(description, mContext.getString(R.string.transition_description))).toBundle() 
       ); 
      } else { 
       mContext.startActivity(intent); 
      } 
     } 
    }); 

的問題是,如果我一張卡片,其中TextViews熄滅有所點擊屏幕上,他們通過動畫/導航欄上面:

text overlapping navigation bar

我該如何防止這種情況發生,以便TextViews在導航欄下的動畫'下'?

回答

10

您可以添加的導航欄爲共享元素,也

View navigationBar = mContext.findViewById(android.R.id.navigationBarBackground); 

而且隨着Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME創建對,

Pair.create(navigationBar, Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME) 
+0

那是......果然不出我所期待的解決方案!我必須這麼做似乎很愚蠢,但它很有用,所以謝謝。 – Ellis