最近我写了一个关于每个列表项中的刷新按钮的function.it&#39。我想要的是单击按钮或列表项,刷新按钮开始旋转。请求完成后停止。 我使用动画如下:
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="358" />
和一些源代码在这里:
public void refresh(View v) {
Animation rotation = AnimationUtils.loadAnimation(mContext,
R.anim.rotate);
rotation.setFillAfter(true);
v.startAnimation(rotation);
}
public void completeRefresh(View v) {
v.clearAnimation();
}
请求完成后,我调用notifyDataSetChanged()来刷新LiseView。
问题是按钮确实在旋转。但是当我第二次点击它时。它旋转但有点模糊。就像这样:
有什么建议吗?非常感谢。
答案 0 :(得分:0)
第二次(以及随后的点击)最有可能发生的是动画再次在其上运行。
在您当前的实施中,请尝试使用setTag(...)
,如下所示:
public void refresh(View v) {
if(v.getTag() != null && (boolean)v.getTag()) {
//do nothing, since we are setting the tag to be true once pressed
} else {
//this view hasn't been clicked yet, show animation and set tag
v.setTag(true);
Animation rotation = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
rotation.setFillAfter(true);
v.startAnimation(rotation);
}
}
在你的适配器中,你应该跟踪哪些列表项是动画的(我假设可以同时点击多个项目)。一旦您知道“请求”已完成,您就可以使用正确的项目更新适配器,然后致电notifyDataSetChanged()