Ann*_*him 4 android android-layout android-recyclerview
我遵循了有关为recyclerview项设置动画的教程,但是该动画无法正常工作,无论是否应用该动画,我都感到困惑。
Activity.Java: 更新的代码是我在这里单击按钮时尝试过的,但是我不知道如何从适配器的onBindHolder调用动画方法
private void setupRecyclerView() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation);
recyclerView.setLayoutAnimation(controller);
recyclerView.setAdapter(specialistListAdapter);
}
//placed a dummy button onclick to check animation working or not
public void reloadData(View view) {
runLayoutAnimation(recyclerView);
}
private void runLayoutAnimation(final RecyclerView recyclerView) {
final Context context = recyclerView.getContext();
final LayoutAnimationController controller =
AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation);
recyclerView.setLayoutAnimation(controller);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
}
Run Code Online (Sandbox Code Playgroud)
layout_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/item_list_animation"
android:delay="15%"
android:animationOrder="normal"
/>
Run Code Online (Sandbox Code Playgroud)
item_list_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="-20%"
android:toYDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
</set>
Run Code Online (Sandbox Code Playgroud)
提前致谢
我迟到了回答这个问题,但我面临同样的问题,同时遵循相同的教程,并尝试不同的东西捆后,我意识到,我并没有添加的属性:android:duration在set tag中item_list_animation.xml文件,该文件是一样的案件。因此,item_list_animation.xml XML现在变为:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime">
<translate
android:fromYDelta="-20%"
android:toYDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator" />
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator" />
<scale
android:fromXScale="105%"
android:fromYScale="105%"
android:toXScale="100%"
android:toYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
</set>
Run Code Online (Sandbox Code Playgroud)
注意:更改数据集时,您需要在RecyclerView上重新应用动画,因为notifyDataSetChanged()随时调用都会取消动画。您只需调用以下命令即可重新应用动画:
adapter.notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
Run Code Online (Sandbox Code Playgroud)
如果您使用LiveData& ViewModel(来自 Android 架构组件),请确保您recyclerView.setLayoutAnimation(controller);在 viewmodel.yourListLive().observe(){} 中调用。像这样的东西:
vm.getGamesPageList().observe(this, games -> {
if (games == null)
return;
LayoutAnimationController animationController =
AnimationUtils.loadLayoutAnimation(v.getContext(), ANIM_RES_ID);
v.setLayoutAnimation(animationController);
adapter.submitList(games);
...
});
Run Code Online (Sandbox Code Playgroud)
首先,您需要以 xml 或以编程方式在回收器视图中加载动画
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_animation"/>
Run Code Online (Sandbox Code Playgroud)
或者
LayoutAnimationController animation =
AnimationUtils.loadLayoutAnimation(ctx, R.anim.layout_animation);
recyclerview.setLayoutAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
您还需要像这样调用 OnBindViewHolder 中的方法runLayoutAnimation(YOUR_RECYCLER_VIEW)。
| 归档时间: |
|
| 查看次数: |
3269 次 |
| 最近记录: |