[英]How to listen for lottie animation end from activity
I have activity that contain fragment.我有包含片段的活动。 This fragment have a lottie animation with lottie_loop="false", that means, once the animation finish first loop , the animation will be and.这个片段有一个 lottie 动画,lottie_loop="false",这意味着,一旦动画完成第一个循环,动画将是和。
I want to listen for this event(animation end) in activity that contain this fragment, but some this wrong with my code, and I have white screen.我想在包含这个片段的活动中监听这个事件(动画结束),但是我的代码有一些错误,我有白屏。
I created interface for listen to even , and this is my code:我创建了用于收听 even 的界面,这是我的代码:
Fragment with lottie animation:带有lottie动画的片段:
public class EntryFragmentAnimation extends Fragment {
private View view;
private LottieAnimationView mLavTwoHearts;
private boolean isAnimationEnd = false;
private OnAnimationEndListener iOnAnimationEndListener;
public interface OnAnimationEndListener {
void onAnimationEnd(boolean isAnimationEnd);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_entry_animation, container, false);
initView(view);
initAnimation();
return view;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
iOnAnimationEndListener = (OnAnimationEndListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnAnimationEndListener");
}
}
private void initView(View view) {
mLavTwoHearts = view.findViewById(R.id.lavTwoHearts);
}
private void initAnimation() {
mLavTwoHearts.playAnimation();
mLavTwoHearts.addAnimatorListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
isAnimationEnd = true;
iOnAnimationEndListener.onAnimationEnd(isAnimationEnd);
}
});
}
}
And an activity还有一个活动
public class LoginActivity extends AppCompatActivity implements EntryFragmentAnimation.OnAnimationEndListener {
private boolean isAnimationEnd = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setEntryScreen();
listenToAnimationEnd();
}
private void setEntryScreen(){
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container_login_fl, new EntryFragmentAnimation())
.commit();
}
private void listenToAnimationEnd(){
while (!isAnimationEnd){
Log.d(TAG, "listenToAnimationEnd: Animation playing");
}
Log.d(TAG, "listenToAnimationEnd: animation end");
}
@Override
public void onAnimationEnd(boolean isAnimationEnd) {
this.isAnimationEnd = isAnimationEnd;
}
}
While running the app , only white screen appear and in logcat running endless log with Animation playing运行应用程序时,只出现白屏,并且在运行动画播放的 logcat 中运行无穷无尽的日志
Instead of a listener I would suggest you to better use a ViewModel .我建议您最好使用ViewModel而不是侦听器。 You only need to create ViewModel class and create its instance in fragment but using the activity scope so that it will be available for all the fragment contained within the activity including activity itself.您只需要创建 ViewModel 类并在片段中创建其实例,但使用活动范围,以便它可用于活动中包含的所有片段,包括活动本身。
In your fragment create a Shared ViewModel instance like below:在您的片段中创建一个 Shared ViewModel 实例,如下所示:
activity?.let {
sharedViewModel = ViewModelProviders.of(it).get(SharedViewModel::class.java)
}
Once the animation ends update the the ViewModel动画结束后更新 ViewModel
sharedViewModel?.onAnimationFinished()
Note: Inside you ViewModel class, have any live data member which is being obeserved by your Activity and then just update the variable within the function.注意:在您的 ViewModel 类中,拥有您的 Activity 正在观察的任何实时数据成员,然后只需更新函数内的变量。
In the activity we just need to create instance of our ViewModel and observe the required data like this在活动中,我们只需要创建 ViewModel 的实例并像这样观察所需的数据
val sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel::class.java)
sharedViewModel.animationEndEvent.observe(this, Observer {
it?.let {
// do some thing
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.