Keno
Keno

Reputation: 2098

RecyclerView animates items all at once instead of one by one

My Problem

I've been trying to get my Recycler View's items to animate as I scroll down. These items are loaded from a local XML and passed through my custom adapter.

I'm using this library for my adapter animation

I noticed that no matter what I tried, the items would just not animate at all, or they would animate all at once (every item would slide in on screen at the same time).

What I've Tried So Far

Utilizing the library I mentioned, I tried animating items in my RecyclerView.Adapter.

> IndexFragment.java snippet

//Create an adapter
SongAdapter adapter = new SongAdapter(songs, getContext());
//Set adapter
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);

This one failed somehow. I also tried the setItemAnimator method which also failed.

recyclerView.setItemAnimator(new SlideInLeftAnimator());

Layout Structure

So I assume it has something to do with my RecyclerView being inside of two NestedScrollView's.

Another person seemed to be having a similar issue

Below are some portions of my layout files


activity_main.xml

....
<!-- Fragment Container -->
<android.support.v4.widget.NestedScrollView
    android:id="@+id/contentContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
....

This is where I put my fragments

index_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nestedScrollView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.v4.widget.NestedScrollView>

This is pretty much my Recyclerview and its parent Nested Scrollview.

item_song.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp">
        
        <!--I have two text views in here. Removed them just for this snippet -->
        
</LinearLayout>

    <!-- This displays a background on each item when I press them AKA touch effects -->
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground" />

</FrameLayout>

This is the structure of each item in the Recyclerview. I inflate this layout for each item. Pretty straightforward.


Any ideas as to what I could do to solve this issue? Thanks.

Upvotes: 3

Views: 727

Answers (1)

Keno
Keno

Reputation: 2098

So after much testing, I finally came up with a solution that worked.

  1. Change the fragment container in activity_main.xml from NestedScrollView to RelativeLayout or perhaps LinearLayout.

  2. Remove the parent NestedScrollView and keep only the RecyclerView in index_fragment.xml.

  3. Animations now work at last. Yay!

Apparently, it is a bad idea to put your RecyclerView inside of a NestedScrollView.

I read somewhere that because of having to load the proper height of its child layout, the NestedScrollView has to wait for it to completely load, in order to know the total height of the RecyclerView. However, I'm not totally sure if that's the reason, so feel free to correct me.

Hope this helps someone!

Upvotes: 1

Related Questions