Android 动画删除 ListView 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12595610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate the removal of a ListView item
提问by John Wheeler
I'm attempting to animate the removal of a ListView item using this:
我正在尝试使用此动画删除 ListView 项目:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, final View view, final int i, long l) {
view.animate().setDuration(500).x(-view.getWidth()).alpha(0f);
adapter.remove(tasks.get(i));
adapter.notifyDataSetChanged();
}
});
It does not work. I basically followed the advice of the 4th answer from the top of this post:
这是行不通的。我基本上遵循了这篇文章顶部的第四个答案的建议:
How to Animate Addition or Removal of Android ListView Rows
However, there's some funny drawing stuff going on, or recycling, or something because while the animation occurs, the item below the one that slides off screen also gets deleted for some reason. The answer that the question asker eventually marked as correct is unfortunately an RTFM towards the whole of Android's source. I've looked through there, and I can't find the notifications pull-down in JellyBean which I'm trying to emulate.
然而,有一些有趣的绘图东西正在进行,或者回收,或者其他东西,因为当动画发生时,滑出屏幕的项目下方的项目也会由于某种原因被删除。不幸的是,提问者最终标记为正确的答案是针对整个 Android 源代码的 RTFM。我已经浏览了那里,但在 JellyBean 中找不到我想要模拟的通知下拉菜单。
TIA. John
TIA。约翰
回答by Alexey
Idea: I start animation when the user selects the row. When the animation is complete i remove the row from the adapter.The following code animates the selected row:
想法:当用户选择行时我开始动画。动画完成后,我从适配器中删除该行。以下代码为所选行设置动画:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView1);
final ArrayList<String> values = new ArrayList<String>();
values.add("Android");
values.add("iPhone");
values.add("WindowsMobile");
values.add("Blackberry");
values.add("Windows7");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
final Animation animation = AnimationUtils.loadAnimation(this,
R.anim.slide_out);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
adapter.remove(adapter.getItem(selectedRow));
adapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
" " + values.get(position), Toast.LENGTH_LONG).show();
view.startAnimation(animation);
selectedRow = position;
}
});
}
slide_out.xml file:
slide_out.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
回答by Michal Vician
I just found a beautiful solution: https://github.com/paraches/ListViewCellDeleteAnimation
我刚刚找到了一个漂亮的解决方案:https: //github.com/paraches/ListViewCellDeleteAnimation
Here is video: http://www.youtube.com/watch?v=bOl5MIti7n0
这是视频:http: //www.youtube.com/watch?v=bOl5Miti7n0
Many thanks to 'paraches' ;)
非常感谢'paraches';)
Edit
编辑
As of Android version 22.0.0 the new android.support.v7.widget.RecyclerView
is available as a successor of ListView
. Standard add/remove/update animations are available out of the box. The widget animations are also easy to customize (few custom animations).
从 Android 版本 22.0.0 开始,新版本android.support.v7.widget.RecyclerView
可作为ListView
. 标准的添加/删除/更新动画是开箱即用的。小部件动画也很容易自定义(很少有自定义动画)。
I strongly recommend switching from ListView
to RecyclerView
if you need custom item animations.
如果您需要自定义项目动画,我强烈建议从 切换ListView
到RecyclerView
。
回答by AllDayAmazing
As Chet Haase points out in DevBytes, https://www.youtube.com/watch?v=8MIfSxgsHIs, all of this will work but don't forget to add transientState flags.
正如 Chet Haase 在 DevBytes 中指出的,https://www.youtube.com/watch?v =8MIfSxgsHIs ,所有这些都可以工作,但不要忘记添加瞬态标志。
From his code:
从他的代码:
// Here's where the problem starts - this animation will animate a View object.
// But that View may get recycled if it is animated out of the container,
// and the animation will continue to fade a view that now contains unrelated
// content.
ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.ALPHA, 0);
anim.setDuration(1000);
if (setTransientStateCB.isChecked()) {
// Here's the correct way to do this: if you tell a view that it has
// transientState, then ListView ill avoid recycling it until the
// transientState flag is reset.
// A different approach is to use ViewPropertyAnimator, which sets the
// transientState flag internally.
view.setHasTransientState(true);
}
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
cheeseList.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
if (setTransientStateCB.isChecked()) {
view.setHasTransientState(false);
}
}
});
anim.start();
回答by Nishant
I have used the Top to bottom slide animation on refreshing the ListView. Here is the code I used:
我在刷新 ListView 时使用了从上到下的幻灯片动画。这是我使用的代码:
adapter.notifyDataSetChanged();
Animation animation = AnimationUtils.loadAnimation(
getApplication(), R.anim.slide_top_to_bottom);
getListView().startAnimation(animation);
getListView().setSelection(0);
and slide_top_to_bottom.xml (save it inside res/anim
folder)
和 slide_top_to_bottom.xml (将其保存在res/anim
文件夹中)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="-100%" android:toXDelta="0" android:duration="100" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="50" />
</set>
EDIT:Try this out:
编辑:试试这个:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, final View view, final int i, long l) {
ValueAnimator fader = ObjectAnimator.ofFloat(view, "alpha", 1, 0);
AnimatorSet animation = new AnimatorSet();
animation.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
((AnimatorSet) animation).play(fader);
animation.setDuration(500);
animation.start();
adapter.remove(tasks.get(i));
adapter.notifyDataSetChanged();
}
});