我正在尝试为导航抽屉菜单
实施共享元素转换<item
android:id="@+id/nav_studentprofile"
android:icon="@drawable/studentprofile"
android:title="@string/student_profile"
/>
我的另一项活动包含ImageView
和TextView
,
<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"/>
但是对于共享元素转换,我必须传递元素的视图,
我无法将menuItem转换为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;
}
我想将MenuItems
转换为view
,然后在共享元素转换中使用该视图
我的第一个活动
我的第二个活动
答案 0 :(得分:1)
单击某个项目时,您可以使用item.getActionView();
获取menuitem视图,但仅当您在抽屉项目上添加自定义视图时才有效。
因此,为导航抽屉项创建虚拟自定义视图: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>
然后,在menu / 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;
}