我已经在资源中创建了一个过渡管理器,并在单击主布局上的按钮之一时运行了过渡。这个想法非常简单:单击登录按钮时,运行从资源开始的过渡,然后过渡到scene_login;单击注册按钮时,运行相同的过渡,然后过渡到scene_signup。
单击后退按钮后,可以平滑撤消过渡,并准备好再次进行按钮上的单击事件。
布尔变量用于跟踪过渡是否开始。
过渡运行后,按钮不再接收onClick事件。这意味着我无法运行后续的转换,这些转换将在onClick()
内部运行。以前从未遇到过。有想法吗?
R.transition.transition_mgr:
<?xml version="1.0" encoding="utf-8"?>
<transitionManager xmlns:android="http://schemas.android.com/apk/res/android">
<transition android:transition="@transition/mytransitions" android:toScene="@layout/scene_signup"/>
<transition android:transition="@transition/mytransitions" android:toScene="@layout/scene_login"/>
<transition android:transition="@transition/mytransitions" android:toScene="@layout/activity_main"/>
</transitionManager>
R.transition.mytransitions:
<?xml version="1.0" encoding="utf-8"?>
<transitionSet android:duration="3000"
xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds/>
<fade/>
</transitionSet>
R.layout.activity_main:
<?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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/signup"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Sign Up"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/login"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Log In"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp"
app:layout_constraintGuide_percent=".5" />
</android.support.constraint.ConstraintLayout>
R.layout.scene_login:
<?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/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp"
app:layout_constraintGuide_percent=".5" />
<Button
android:id="@+id/login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:text="Log In"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<Button
android:id="@+id/signup"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Sign Up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone" />
</android.support.constraint.ConstraintLayout>
R.layout.scene_signup:
<?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/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/login"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="Log In"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone" />
<Button
android:id="@+id/signup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Sign Up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone" />
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />
<EditText
android:id="@+id/confirmPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Password (again)"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<EditText
android:id="@+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/confirmPassword" />
</android.support.constraint.ConstraintLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity{
private static final String TAG = "MainActivity";
Button signupButton;
Button loginButton;
boolean transitionStarted = false;
TransitionManager transitionMgr;
Scene scene_main;
Scene scene_login;
Scene scene_signup;
ViewGroup root;
Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signupButton = findViewById(R.id.signup);
loginButton = findViewById(R.id.login);
root = findViewById(R.id.root);
transitionMgr = TransitionInflater.from(mContext).inflateTransitionManager(R.transition.transition_mgr, root);
scene_main = Scene.getSceneForLayout(root, R.layout.activity_main, mContext);
scene_login = Scene.getSceneForLayout(root, R.layout.scene_login, mContext);
scene_signup = Scene.getSceneForLayout(root, R.layout.scene_signup, mContext);
signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: transitionStarted: " + transitionStarted );
if(!transitionStarted) {
transitionMgr.transitionTo(scene_signup);
transitionStarted = true;
}
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: transitionStarted: " + transitionStarted);
if(!transitionStarted) {
transitionMgr.transitionTo(scene_login);
transitionStarted = true;
}
}
});
}
@Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed: transitionStarted: " + transitionStarted);
if(transitionStarted) {
transitionStarted = false;
transitionMgr.transitionTo(scene_main);
}
else
super.onBackPressed();
}
}
PS。似乎通过过渡到新场景,视图信息(包括所有onClick侦听器)被清除了,我们可能需要再次查找视图并设置所需的侦听器。
答案 0 :(得分:0)
请注意,过渡到新场景后,由于视图层次结构发生了变化,因此会清除诸如onClick侦听器之类的视图信息,尽管您可能会创建具有相同ID的不同视图。
为了使onClick在过渡后仍能正常工作,您需要再次查找各自的视图并在其上重置onClick侦听器。
修改后的活动代码(有效)(也在GitHub上):
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onClick(p0: View?) {
when(p0?.id) {
R.id.register -> {
if (!animationStarted) {
TransitionManager.go(sceneRegister, set);
animationStarted = true;
}
}
R.id.login -> {
if(!animationStarted) {
TransitionManager.go(sceneLogin, set);
animationStarted = true;
}
}
}
}
var animationStarted = false;
lateinit var scene: Scene
lateinit var login: Button
lateinit var register: Button
lateinit var root : ViewGroup
lateinit var sceneRegister: Scene
lateinit var sceneLogin: Scene
lateinit var set: TransitionSet
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
login = findViewById<Button>(R.id.login);
register = findViewById<Button>(R.id.register);
root = findViewById<ViewGroup>(R.id.root);
scene = Scene.getSceneForLayout(root, R.layout.main_activity, this);
sceneLogin = Scene.getSceneForLayout(root, R.layout.main_activity_login, this);
sceneRegister = Scene.getSceneForLayout(root, R.layout.main_activity_register, this);
set = TransitionSet();
set.addTransition(ChangeBounds()).addTransition(Fade()).addTransition(AutoTransition());
login.setOnClickListener (this)
register.setOnClickListener(this)
}
override fun onBackPressed() {
if(animationStarted) {
val root = findViewById<ViewGroup>(R.id.root);
TransitionManager.go(scene, ChangeBounds() );
animationStarted = false;
login = findViewById(R.id.login)
register = findViewById(R.id.register)
login.setOnClickListener(this)
register.setOnClickListener(this)
}
else {
super.onBackPressed();
}
}
}