2016-10-29 109 views
3

我想實現的抽屜式導航菜單共享元素過渡ANDROID:抽屜式導航菜單項共享元素過渡

<item 
     android:id="@+id/nav_studentprofile" 
     android:icon="@drawable/studentprofile" 
     android:title="@string/student_profile" 
     /> 

我有一個ImageViewTextView另一項活動,

<ImageView 
    android:layout_width="81dp" 
    android:layout_height="68dp" 
    app:srcCompat="@drawable/studentprofile" 
    android:id="@+id/imageview_studentprofile" /> 

<TextView 
    android:text="@string/student_profile" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview_studentprofile" 
    android:layout_weight="1" 
    android:textSize="30sp"/> 

但對於共享元素過渡,我必須通過元素的視圖, 我無法將菜單項轉換爲View

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_studentprofile) { 

    } else if (id == R.id.nav_classeshistory) { 

    } else if (id == R.id.nav_studentattendance) { 

    } else if (id == R.id.nav_datetracking) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

我要轉換的MenuItemsview然後使用共享元素過渡這一觀點

我的第一個活動

enter image description here

我的第二個活動

enter image description here

回答

1

單擊某個項目時,可以使用item.getActionView();獲取菜單項視圖,但只有在您的抽屜項目上添加了自定義視圖時纔有效。

因此,創建用於導航抽屜項的虛擬自定義視圖:layout name:dummy_drawer_item並設置android:transitionName="@string/transition_string"

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    <!-- Add this line --> 
    android:transitionName="@string/transition_string"> 

</LinearLayout> 

然後在菜單/ drawer_items,添加自定義視圖app:actionLayout="@layout/dummy_drawer_item"的foreach項目。例如:

<item 
      android:id="@+id/nav_camera" 
      android:icon="@drawable/ic_menu_camera" 
      android:title="Import" 
      <!-- Add this line --> 
      app:actionLayout="@layout/dummy_drawer_item"/> 
     ... 

最後,被點擊的項目:

public boolean onNavigationItemSelected(MenuItem item) { 

... 
//Get the view of menuitem 
    View v = item.getActionView(); 
//Call your other activity 
// Get the transition name from the string 
    String transitionName = getString(R.string.transition_string); 

    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, transitionName); 

    //Start the Intent 
    ActivityCompat.startActivity(this, new Intent(this, YourOtherActivity.class), options.toBundle()); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

進出口使用片段交易......是有可能做到共享元素的過渡,而這樣做使用導航抽屜片段交易... –