gpt4 book ai didi

java - 在 setContentId() 之后将占位符返回到其 'empty' 状态

转载 作者:行者123 更新时间:2023-11-29 18:44:25 24 4
gpt4 key购买 nike

我有一个 ImageView,一旦我应用了 placeholder.setContentID(ImageViewID),它就会在屏幕上移动到占位符。

我的问题是如何将 ImageView 重置到其原始位置?理想情况下,这应该将占位符“重置”为其空状态。

我已经尝试了 placeholder.setVisibility(View.GONE) 和 placeholder.setVisibility(View.INVISIBLE),但显示没有 react 。我还尝试了 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image"
android:onClick="transform"
android:layout_width="24dp"
android:layout_height="24dp"
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_width="24dp"
android:layout_height="24dp"
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>

最佳答案

不幸的是,无法将 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image"
android:layout_width="24dp"
android:layout_height="24dp"
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_width="24dp"
android:layout_height="24dp"
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_width="24dp"
android:layout_height="24dp"
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);
}
}
}

关于java - 在 setContentId() 之后将占位符返回到其 'empty' 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52300915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com