qba*_*sso 5 android android-animation android-listview
我想在所有列表视图项上实现同步动画以将其置于编辑模式。这里的效果与我想要的效果相同: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>
Run Code Online (Sandbox Code Playgroud)
用户单击 actionBar 中的按钮后,我想动画滑动复选框从左到右滑动,并使用适当的 textview 动画,为复选框腾出空间。我让复选框不可见是为了测量它的动画宽度。我用于动画复选框的方法(隐藏或显示,它取决于状态参数):
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();
}
Run Code Online (Sandbox Code Playgroud)
当列表视图短于一屏(没有视图重用)时,动画看起来很完美并且非常流畅。当列表视图较长时,似乎某些列表视图项尚未创建,因此没有动画。我尝试在适配器中创建视图缓存并使用这些视图而不是在适配器 getView 方法中使用 convertView,并在该缓存中包含的视图上播放动画。我还注意到所有可重用的视图都是在创建 listview 时创建的(当我滚动 listview 时,转换视图总是 != null)。



查看有关动画的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;
}
}
Run Code Online (Sandbox Code Playgroud)
您可能还会发现此视频很有帮助。
| 归档时间: |
|
| 查看次数: |
1995 次 |
| 最近记录: |