安卓如何同时为ListView的所有项添加动画效果

6

我希望实现所有列表项的动画同时进行,以使其进入编辑模式。这里的效果与我的目标完全相同:http://youtu.be/cFSRusFkI_I?t=2m6s 我的列表项布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="4dp" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:paddingRight="5dip"
        android:src="@drawable/right_arrow" />

</RelativeLayout>

当用户在操作栏中点击按钮后,我想要通过适当的文本视图动画来从左到右滑动复选框,以便为复选框腾出空间。出于测量其宽度以进行动画的目的,我将复选框设置为不可见。我使用的动画复选框的方法(隐藏或显示,取决于状态参数):

public void setListHandlesVisibleState(boolean state) {
    AnimatorSet as = new AnimatorSet();
    int childCount = mListView.getChildCount();
    ArrayList<Animator> list = new ArrayList<Animator>();
    for (int i = 0; i<childCount; ++i) {
        CheckBox v = (CheckBox) mListView.getChildAt(i).findViewById(
                R.id.checkbox);
        TextView tv = (TextView) mListView.getChildAt(i).findViewById(
                R.id.nameTextView);
        if (state) {
            list.add(ObjectAnimator.ofFloat(v, "x", -v.getWidth(),
                    v.getLeft()));
            list.add(ObjectAnimator.ofFloat(v, "alpha", 0, 1));
            list.add(ObjectAnimator.ofFloat(tv, "x", tv.getLeft(),
                    (float) (v.getWidth() * 0.9)));
        } else {
            list.add(ObjectAnimator.ofFloat(v, "x", v.getLeft(),
                    -v.getWidth()));
            list.add(ObjectAnimator.ofFloat(v, "alpha", 1, 0));
            list.add(ObjectAnimator.ofFloat(tv, "x",
                    (float) (v.getWidth() * 0.9), tv.getLeft()));
        }
    }
    as.addListener(this);
    as.playTogether(list);
    as.setDuration(200);
    as.start();
}
@Override
public void onAnimationEnd(Animator animation) {
    mAdapter.notifyDataSetChanged();
}

当ListView比一个屏幕还要短时(没有视图复用),动画看起来是完美的,非常流畅。当ListView更长时,似乎一些ListView项目尚未创建,因此没有动画效果。我尝试在适配器中创建视图缓存,并使用那些视图而不是在适配器的getView方法中使用convertView,然后在该缓存中包含的视图上播放动画。我还注意到,所有可重用视图都在创建ListView时创建(当我滚动ListView时,convert view始终!= null)。 动画之前 动画之后 动画之后并滚动

我也想实现像你这样的东西,你能否在这里发布整个代码或给我链接,以便我可以帮助你并且我也可以使用那段代码。 - Jayesh
1个回答

0

看看这个DevBytes视频,了解如何为ListViews添加动画效果。

解决方案是使用StableArrayAdapter(代码的直接链接)。这可以防止在执行动画时View对象被回收。

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

你可能会发现这个视频很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接