在 setContentId() 之后将占位符返回到其“空”状态
Posted
技术标签:
【中文标题】在 setContentId() 之后将占位符返回到其“空”状态【英文标题】:Return a placeholder to its 'empty' state after setContentId() 【发布时间】:2019-02-17 10:24:34 【问题描述】:应用 placeholder.setContentID(ImageViewID) 后,我有一个 ImageView 会在屏幕上移动到占位符。
我的问题是如何将 ImageView 重置为其原始位置?理想情况下,这应该将占位符“重置”为空状态。
我尝试过 placeholder.setVisibility(View.GONE) 和 placeholder.setVisibility(View.INVISIBLE),但它显示 没有 反应。我也尝试过 clearAnimation(),但没有成功。
这里是 MainActivity.java
package com.example.stefan.placeholderexperiment;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Placeholder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.TransitionManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity
private Placeholder placeholder;
private ConstraintLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placeholder = findViewById(R.id.placeholder);
layout = findViewById(R.id.layout);
public void transform(View v)
TransitionManager.beginDelayedTransition(layout);
placeholder.setContentId(v.getId());
和activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:onClick="transform"
android:layout_
android:layout_
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:src="@drawable/ic_android_black_24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Placeholder
android:id="@+id/placeholder"
android:layout_
android:layout_
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.967" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
【参考方案1】:遗憾的是,无法将 ImageView 重置为其原始位置,因为当您调用 Placeholder.setContentID(ImageViewID)
方法时,它会更改 LayoutParams
的 ImageView
并且不会存储先前状态的任何副本。
所以我只能建议你使用两个不同的占位符并轮流改变它们的内容,像这样:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_
android:layout_
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_
android:layout_
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:onClick="transform"
android:src="@drawable/unnamed"/>
<android.support.constraint.Placeholder
android:id="@+id/placeholder_top"
android:layout_
android:layout_
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.constraint.Placeholder
android:id="@+id/placeholder_bottom"
android:layout_
android:layout_
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.967"/>
</android.support.constraint.ConstraintLayout>
public class MainActivity extends AppCompatActivity
private Placeholder placeholderTop;
private Placeholder placeholderBottom;
private ConstraintLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
placeholderTop = (Placeholder) findViewById(R.id.placeholder_top);
placeholderBottom = (Placeholder) findViewById(R.id.placeholder_bottom);
layout = (ConstraintLayout) findViewById(R.id.layout);
placeholderTop.setContentId(R.id.image);
public void transform(View v)
if (placeholderTop.getContent() == null)
TransitionManager.beginDelayedTransition(layout);
placeholderTop.setContentId(v.getId());
placeholderBottom.setContentId(-1);
else if (placeholderBottom.getContent() == null)
TransitionManager.beginDelayedTransition(layout);
placeholderBottom.setContentId(v.getId());
placeholderTop.setContentId(-1);
【讨论】:
这很完美!谢谢你。我很惊讶 placeholderTop.setContentId(-1);不会造成任何问题... 一个更好的写法是View.NO_ID
。现在有意义吗?以上是关于在 setContentId() 之后将占位符返回到其“空”状态的主要内容,如果未能解决你的问题,请参考以下文章