Я пытаюсь реализовать пользовательский фрагмент с загрузкой анимации. Тот же код отлично работает с активностью, но не работает с фрагментом.Android: запуск анимации при загрузке фрагмента
Вот мой фрагмент кода макета:
<FrameLayout 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"
tools:context="pw.osin.musicexpert.fragments.BusyFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/fragment_animation_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:background="@drawable/vinyl" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/fragment_animation_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:gravity="center_horizontal"
android:text="@string/message_data_is_loading"
android:textColor="@color/colorPrimaryYellow"
android:textSize="20dp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
Мои методы, которые я вызывает в методе OnCreate в фрагменте
private fun setAnimation() {
Log.i(TAG, "Установка анимации")
val drawable = AnimationUtils.loadAnimation(this.context, R.anim.rotate_anim)
mAnimationDescription?.setText(R.string.message_data_is_loading);
mAnimationImage?.startAnimation(drawable)
}
private fun initViewItems(view: View?) {
Log.i(TAG, "Поиск элементов View")
mAnimationDescription = view?.findView<TextView>(R.id.animation_description);
mAnimationImage = view?.findView<ImageView>(R.id.animation_logo);
}
Мой поворот код анимации:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:startOffset="0"
android:toDegrees="360" />
</set>
Он re Я присоединяю свой фрагмент анимации к активности в методе OnCreate
private var mTransaction: FragmentTransaction? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
attachFragment()
}
private fun attachFragment() {
mTransaction = supportFragmentManager.beginTransaction()
mTransaction?.add(R.id.main_activity_container, BusyFragment.newInstance("Получаем дату"))
mTransaction?.commit()
}
Где мне нужно вызвать мой метод начала анимации?
Спасибо.
Что вы видите? Ваш фрагмент показывает, но не оживляет? Получает ли ваш текст правильный текст? – CaseyB
Привет, да. Мой фрагмент показывает, но не оживляет. –