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

Cod*_*ile 14 animation android listview android-listview layout-animation

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

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

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

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

有什么建议?

Thi*_*cha 1

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

在此示例中,我创建了一个名为removeListItemwith 的方法,用于接收要删除的行以及该行在列表内容数组中的位置。

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);
}
Run Code Online (Sandbox Code Playgroud)