RecyclerView一次性动画项目而不是一个一个

时间:2017-02-03 14:23:26

标签: java android animation android-recyclerview

我的问题

当我向下滚动时,我一直试图让我的Recycler View的项目动画化。这些项从本地XML加载并通过我的自定义适配器传递。

I'm using this library for my adapter animation

我注意到无论我尝试什么,这些物品根本就没有动画,或者它们会同时制作动画(每个项目会同时在屏幕上滑动)。

我到目前为止所做的事

利用我提到的库,我尝试在RecyclerView.Adapter中设置动画。

> IndexFragment.java代码段

//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);

这个以某种方式失败了。我也尝试了setItemAnimator方法也失败了。

recyclerView.setItemAnimator(new SlideInLeftAnimator());

布局结构

所以我认为它与我RecyclerViewNestedScrollView内的activity_main.xml有关。

另一个人似乎有a similar issue

以下是我的布局文件的一些部分

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

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>

item_song.xml

这几乎是我的Recyclerview及其父嵌套Scrollview。

<?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>

stream.forEach((url) -> {
     StatusThread statusGetter = new StatusThread(url, outputWriter);
     Future<String> result = executor.submit(statusGetter);
});

这是Recyclerview中每个项目的结构。我为每个项目夸大了这个布局。很简单。

关于我可以做些什么来解决这个问题?感谢。

1 个答案:

答案 0 :(得分:0)

经过多次测试后,我终于想出了一个有效的解决方案。

  1. activity_main.xml中的片段容器从NestedScrollView更改为 RelativeLayout LinearLayout

  2. 删除父NestedScrollView并仅在RecyclerView中保留index_fragment.xml

  3. 动画现在终于有用了。耶!

  4. 显然,将RecyclerView放在NestedScrollView中是个坏主意。

    我在某处读到,由于必须加载其子布局的正确高度,NestedScrollView必须等待它完全加载,才能知道RecyclerView的总高度。但是,我不完全确定这是否是原因,所以请随时纠正我。

    希望这有助于某人!