animation化删除ListView项目

我试图animation删除一个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(); } }); 

这是行不通的。 我基本上遵循了这个post顶部的第四个答案的build议:

如何animation添加或删除Android ListView行

但是,有一些有趣的绘画东西进行,或回收,或者什么,因为在animation发生时,下滑动屏幕之下的项目也被删除出于某种原因。 问题提出者最终标记为正确的答案不幸是一个RTFM,朝向整个Android的来源。 我已经看过那里了,我无法在JellyBean中find我想要模拟的通知下拉菜单。

TIA。 约翰

Solutions Collecting From Web of "animation化删除ListView项目"

我刚刚find了一个漂亮的解决scheme: https : //github.com/paraches/ListViewCellDeleteAnimation

以下是video: http : //www.youtube.com/watch?v = bOl5MIti7n0

非常感谢“paraches”;)

编辑

从Android版本22.0.0开始,新的android.support.v7.widget.RecyclerView可以作为ListView的后继版本。 标准的添加/删除/更新animation是开箱即用的。 小部件animation也很容易定制( less量自定义animation )。

如果您需要自定义项目animation,我强烈build议从ListView切换到RecyclerView

想法:当用户select行时我开始animation。 animation完成后,我从适配器中删除该行。下面的代码为选定的行生成animation:

 @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文件:

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

正如Chet Haase在DevBytes中指出的那样, https://www.youtube.com/watch? v = 8MIfSxgsHIs,所有这些都可以工作,但是不要忘记添加transientState标志。

从他的代码:

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

我在刷新ListView时使用了从上到下的幻灯片animation。 这是我使用的代码:

  adapter.notifyDataSetChanged(); Animation animation = AnimationUtils.loadAnimation( getApplication(), R.anim.slide_top_to_bottom); getListView().startAnimation(animation); getListView().setSelection(0); 

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

编辑:试试这个:

 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(); } });