页面共享元素

233 阅读1分钟

最近看了下google的例子 页面共享元素 点击列表 将列表的一些元素带到详情页面比如头图。特记录一下。

关键代码如下

共享的view及内容

ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
        MainActivity.this,

        // Now we provide a list of Pair items which contain the view we can transitioning
        // from, and the name of the view it is transitioning to, in the launched activity
        new Pair<>(view.findViewById(R.id.imageview_item),
                DetailActivity.VIEW_NAME_HEADER_IMAGE),
        new Pair<>(view.findViewById(R.id.textview_name),
                DetailActivity.VIEW_NAME_HEADER_TITLE));

// Now we can start the Activity, providing the activity options as a bundle
ActivityCompat.startActivity(MainActivity.this, intent, activityOptions.toBundle());
详情页面的元素
mHeaderImageView = findViewById(R.id.imageview_header);
mHeaderTitle = findViewById(R.id.textview_title);

// BEGIN_INCLUDE(detail_set_view_name)
/*
 * Set the name of the view's which will be transition to, using the static values above.
 * This could be done in the layout XML, but exposing it via static variables allows easy
 * querying from other Activities
 */
ViewCompat.setTransitionName(mHeaderImageView, VIEW_NAME_HEADER_IMAGE);
ViewCompat.setTransitionName(mHeaderTitle, VIEW_NAME_HEADER_TITLE);
// END_INCLUDE(detail_set_view_name)

loadItem();