android - 在 Android 上中断动画导航会导致空白屏幕
问题描述
我在片段之间的动画方面遇到了麻烦。在片段过渡期间快速按下后退按钮时,我可以实现NavHostFragment
活动布局内部显示为空的状态。
这是一个演示该问题的视频:
正如您在视频中看到的那样,在转换完成后按下后退按钮会弹出第二个片段,如预期的那样。但是,当动画仍在进行中时,有时按下后退按钮会清除整个动画NavHostFragment
,我会看到一个空视图。
动画越长,我越有可能获得空白屏幕。也许我没有正确应用动画,所以下面是这个例子的所有相关代码。
MainActivity
:
public class MainActivity extends androidx.appcompat.app.AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_test);
}
}
FirstFragment
:
public class FirstFragment extends androidx.fragment.app.Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first, container, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
view.findViewById(R.id.to_second_fragment).setOnClickListener(v -> androidx.navigation.Navigation.findNavController(view).navigate(R.id.action_first_fragment_to_second_fragment));
}
}
activity_navigation_test
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation_test" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation_test
:
<?xml version="1.0" encoding="utf-8"?>
<navigation
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/navigation_test"
app:startDestination="@id/first_fragment">
<fragment
android:id="@+id/first_fragment"
android:name="com.test.navigation.FirstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" >
<action
android:id="@+id/action_first_fragment_to_second_fragment"
app:destination="@id/second_fragment"
app:enterAnim="@anim/slide_in_from_right"
app:exitAnim="@anim/slide_a_little_out_to_left" />
</fragment>
<fragment
android:id="@+id/second_fragment"
android:name="com.test.navigation.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
</navigation>
slide_in_from_right
(slide_a_little_out_to_left
除了 fromX 和 toX 值之外是相同的):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="666"
android:shareInterpolator="false">
<translate android:fromXDelta="100%"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator" />
</set>
我正在使用以下库版本:
androidx.appcompat:appcompat:1.3.0-alpha02
androidx.fragment:fragment:1.2.5
androidx.navigation:navigation-fragment:2.3.1
androidx.navigation:navigation-ui:2.3.1
解决方案
推荐阅读
- r - 如何从 R 中的单行中提取数据?
- javascript - Flutter 客户端无法连接到 Node.js 服务器(socket.io)
- swift - UIView layoutSubviews 通知
- macos - 如何安装ffmpeg-static?m1/os x
- javascript - JS | 滚动时未显示我的粘性菜单
- java - XmlElement 在 SOAP 中显示为小写
- android - 在技术上是否可以阻止用户更改原始启动器?
- wcf - SoapUI 对序列模式断言是否过于严格?
- python - 如何在 Python 中访问我的 JSON 中的元素?
- c++ - 自动展开 qtreeview 中委托的 qcombobox