airbnb / lottie-android

Render After Effects animations natively on Android and iOS, Web, and React Native
http://airbnb.io/lottie/
Apache License 2.0
35.03k stars 5.41k forks source link

Animation freeze during reyclerview.notifyDataSetChanged(); #322

Closed Guimareshh closed 7 years ago

Guimareshh commented 7 years ago

Hi there !

I'm using Lottie since months now and I found a strange issue today. In my fragment I have the Lottie animation already set, and using the animation to make the user wait during the call of API nor loading data from Realm. When it's done I animate the Lottie file to set the Alpha to 0 and call notifyDataSetChanged() for two recyclerView.

Here is the video

And here is my code :

binding.animationView.animate().alpha(0f).setDuration(250).start();

binding.fragmentDiscoverTextViewServices.setVisibility(View.VISIBLE);
binding.fragmentDiscoverTextViewDomaines.setVisibility(View.VISIBLE);

categoryAdapter.notifyDataSetChanged();
subCategoryAdapter.notifyDataSetChanged();

One of the RecyclerView has a lot of items (±50) and the other only 5.

And how the Lottie XML looks like :

<com.airbnb.lottie.LottieAnimationView
                android:id="@+id/animation_view"
                android:layout_width="@dimen/loader_size"
                android:layout_height="@dimen/loader_size"
                android:layout_marginTop="@dimen/spacing_extra_huge"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/fragment_discover_searchView"
                app:lottie_autoPlay="true"
                app:lottie_fileName="loader.json"
                app:lottie_loop="true" />

Any idea why this is happening ? Thanks for your help !

gpeal commented 7 years ago

@Guimareshh is your animation in the recyclerview?

Guimareshh commented 7 years ago

@gpeal no the animation is inside the ConstraintLayout, same level as the recyclerView. That's why I find this issue very strange

gpeal commented 7 years ago

@Guimareshh Can you attach a sample project that repros this?

Guimareshh commented 7 years ago

@gpeal I tried in a sample project to repro the same bug, but it was impossible. After trying few things in my main project I have found that it doesn't come from the notifyDataSetChanged();

It seems to be the Realm insertion that freeze the animation. Indeed the freeze is when I start to save my objects inside Realm :

DiscoverRest discoverRest = ServiceGenerator.createService(DiscoverRest.class, true);
        discoverRest
                .getCategories()
                .enqueue(new Callback<CategoryListResponse>() {
                    @Override
                    public void onResponse(Call<CategoryListResponse> call, Response<CategoryListResponse> response) {
                        if (response.isSuccessful()) {

                            Realm realm = Realm.getDefaultInstance();
                            for (final Category category : response.body().getCategories()) {
                                realm.executeTransaction(new Realm.Transaction() {
                                    @Override
                                    public void execute(Realm realm) {
                                        realm.copyToRealmOrUpdate(category);
                                    }
                                });
                                if (category.getSubCategories() != null) {
                                    for (final SubCategory subCategory : category.getSubCategories()) {
                                        subCategory.setIdCategory(category.getId());
                                        realm.executeTransaction(new Realm.Transaction() {
                                            @Override
                                            public void execute(Realm realm) {
                                                realm.copyToRealmOrUpdate(subCategory);
                                            }
                                        });
                                    }
                                }
                            }
                            loadFromRealm();

                        } else {
                            APIError error = ErrorUtils.parseError(response);
                            Toast.makeText(getActivity(), error.message(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<CategoryListResponse> call, Throwable t) {
                        Toast.makeText(getActivity(), getString(R.string.api_failed), Toast.LENGTH_LONG).show();
                    }
                });
    }

If I remove the Realm part and add the following code :

categories.addAll(response.body().getCategories());
categoryAdapter.notifyDataSetChanged();
binding.animationView.animate().alpha(0f).setDuration(250).start();

The Lottie animation doesn't freeze. For now, the solution will be to save objects with Realm after the animation has "gone".

gpeal commented 7 years ago

@Guimareshh This doesn't seem like a Lottie issue then. You may be able to use systrace or something to figure out why it's not working though.

zhangjianhd commented 11 months ago

I've met the question as you too.do nothing but notifyDataSetChanged,the animation freeze. lottie version is 6.2.0