すべてのリスト ビュー アイテムで同時アニメーションを実現して、編集モードにしたいと考えています。これは、私が目指しているものと同じ効果です: 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>
ユーザーがactionBarのボタンをクリックした後、スライドするチェックボックスを左から右にスライドさせ、テキストビューの適切なアニメーションでチェックボックス用のスペースを作りたいと思います。アニメーションの幅を測定するために、チェックボックスを非表示のままにしました。チェックボックスをアニメーション化するために使用する方法(非表示または表示、状態パラメーターに依存):
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();
}
リストビューが 1 画面より短い場合 (再利用するビューがない場合)、アニメーションは完璧に見え、非常にスムーズです。リストビューが長い場合、リストビュー項目の一部がまだ作成されていないように見えるため、アニメーション化されていません。アダプタでビュー キャッシュを作成し、代わりにアダプタの getView メソッドで convertView を使用してそれらのビューを使用し、そのキャッシュに含まれるビューでアニメーションを再生しようとしました。また、リストビューの作成時にすべての再利用可能なビューが作成されることにも気付きました (リストビューをスクロールすると、ビューの変換は常に != null になります)。