我正在尝试在两个活动之间创建共享元素转换。这似乎可以在两个布局之间找到,每个布局包含一个ImageView。但是,我遇到了使用ListView进行此转换的问题。
问题是当用户点击第一行时,转换将按预期工作。但是,当点击第二行时,转换的原点似乎也是第一行的图像!
我创建了一个带有自定义行布局的简单列表视图:
row_detailed.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Daft Punk"
android:id="@+id/primary_textview"
android:layout_gravity="center_horizontal"
android:layout_weight="0.07"
android:layout_alignTop="@+id/primary_imageview"
android:layout_toRightOf="@+id/primary_imageview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Random Access Memories"
android:id="@+id/textView4"
android:layout_gravity="center_horizontal"
android:layout_weight="0.07"
android:layout_alignBottom="@+id/primary_imageview"
android:layout_toRightOf="@+id/primary_imageview" />
<ImageView
android:layout_width="84dp"
android:layout_height="84dp"
android:id="@+id/primary_imageview"
android:src="@drawable/ram"
android:layout_weight="0.07" />
</RelativeLayout>
辅助活动的布局 activity_transition_secondary.xml :
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="legiocode.com.tapstrprototype.activity.TransitionSecondaryActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secondary_imageview"
android:src="@drawable/ram"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
主要活动 TransitionPrimaryActivity.java :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_primary);
ListView listView = (ListView)findViewById(R.id.listView2);
ArrayAdapter<String> adapter = new ArrayAdapter(this,
R.layout.row_detailed,
R.id.primary_textview,
items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String transitionName = getString(R.string.transition_image) + String.format("_%d", i);
ImageView imageView = (ImageView)findViewById(R.id.primary_imageview);
imageView.setTransitionName(transitionName);
Intent intent = new Intent(TransitionPrimaryActivity.this, TransitionSecondaryActivity.class);
intent.putExtra(TransitionSecondaryActivity.TRANSITION_NAME, transitionName);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(TransitionPrimaryActivity.this,
imageView, // The view which starts the transition
transitionName // The transitionName of the view we’re transitioning to
);
ActivityCompat.startActivity(TransitionPrimaryActivity.this, intent, options.toBundle());
}
});
}
辅助活动 TransitionSecondaryActivity.java :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_secondary);
String transitionName = getIntent().getStringExtra(TRANSITION_NAME);
ImageView imageView = (ImageView)findViewById(R.id.secondary_imageview);
imageView.setTransitionName(transitionName);
}
我试图动态地实现转换名称,但这似乎没有多大区别。我是否正确设置过渡?
答案 0 :(得分:2)
您必须在
中访问实际点击的视图(在本例中为参数视图)onItemClick(AdapterView<?> adapterView, View view, int i, long l)
使用
ImageView imageView = (ImageView)findViewById(R.id.primary_imageview);
您始终可以访问列表中的第一个元素。
PS:将转换名称放在xml中。使其更容易阅读。