如何将android布局动画仅应用于某个索引以上的孩子?

时间:2011-01-11 14:03:19

标签: android listview animation android-listview layout-animation

我有一个包含一系列笔记的ListView。

目前我使用布局动画在列表首次加载时从侧面滑动所有笔记;这很有效。

但是,我试图找出如何仅应用布局动画来列出某个点以下的项目。假设我删除了列表中的项目:我希望它下面的所有项目都转移到已删除的笔记的旧位置。

我已经尝试找到一种通过子索引自定义动画延迟或插补器的方法,但是没有找到适合此位置的任何内容。有没有办法使用自定义布局动画(例如扩展LayoutAnimationController)来执行此操作,还是我必须执行此低级操作并单独为每个视图设置动画?

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

创建动画并在列表OnItemClickListener中调用它。之后,您可以使用适配器的notifyDataSetChanged刷新列表内容。

在这个例子中,我创建了一个名为removeListItem的方法,它接收你要删除的行以及列表内容数组中该行的位置。

public class MainActivity extends ListActivity implements OnItemClickListener{

ArrayList<String> values;
ArrayAdapter<String> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    values = generateMockData(50);

    adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, values);

    setContentView(R.layout.activity_main);

    getListView().setAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

private ArrayList<String> generateMockData(int number) {

    ArrayList<String> result = new ArrayList<String>();

    for(int i = 0; i < number; i++)
        result.add(""+i+" "+ (int)Math.random() * 13);

    return result;
}

private void removeListItem(View rowView, final int positon) {

    Animation anim = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);
    anim.setDuration(500);
    rowView.startAnimation(anim);

    new Handler().postDelayed(new Runnable() {

        public void run() {

            values.remove(positon);//remove the current content from the array

            adapter.notifyDataSetChanged();//refresh you list

        }

    }, anim.getDuration());

}

public void onItemClick(AdapterView<?> arg0, View row, int position, long arg3) {

       if(position == YOUR_INDEX) //apply your conditions here!
          removeListItem(row,position);
}

答案 1 :(得分:1)

我有一个非常类似的问题,并且能够找到一个简单的View子类的解决方案,它允许您使用布局动画控制器仅为您指定的视图设置动画。请看这个链接:

Can LayoutAnimationController animate only specified Views

答案 2 :(得分:0)

在你的布局xml中,尝试添加:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="true"/>  

这会自动为列表视图中的插入和删除设置动画 如果您有一个名为anim_translate_left的自定义动画,请改为使用:

<ListView  
  android:id="@android:id/list"
  ... 
  android:animateLayoutChanges="@anim/anim_translate_left"/>   

来源:Google API's

相关问题