我已經開始在我的一個項目中使用DataBinding,並且一直面臨着管理視圖和動畫的問題。我在我的應用程序中使用DataBinding,並且在設置某個布爾值時使用BindingAdapters來動畫視圖。我使用了this post中描述的類似方法。在Android中使用DataBinding時的動畫視圖
在我試圖用DataBinding實現的動畫中,我在屏幕中間有一個搜索欄,下面有一個列表。當用戶點擊searchBar時,它將動畫到屏幕的頂部,並且列表滾動到屏幕的底部。 (拆分中心類型的動畫)
我的模型中有一定的布爾值,當searchButton被點擊時,它被設置爲true,並且BindingAdapter在布爾值中監聽該變化,爲視圖添加動畫。我可以得到需要動畫的視圖,但視圖動畫的數量取決於另一個視圖的大小,而不是在BindingAdapter中傳遞。有沒有辦法在BindingAdapter中傳遞多個視圖?我的第二個問題是,由於搜索欄和列表都在同一個值的變化上生成動畫,但我如何確保它們同時動畫。如果我使用2個objectAnimators,我可以使用AnimatorSet並確保同步,但是如何使用DataBinding執行此操作?
public class AnimHelper {
@BindingAdapter({"animatev1", "app:model"})
public static void Animate1(View view, boolean b, final Model model) {
if (b) {
ObjectAnimator a = ObjectAnimator
.ofFloat(view, View.TRANSLATION_Y, -view.getHeight())
.setDuration(2000);
a.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
model.setAnim2(true);
}
});
a.start();
}
}
@BindingAdapter({"animatev2", "app:model"})
public static void Animate2(View view, boolean anim, final Model model) {
if (anim) {
ObjectAnimator a = ObjectAnimator
.ofFloat(view, View.TRANSLATION_Y, view.getHeight())
.setDuration(2000);
a.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
a.start();
}
}
}
xml文件如下:
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="searchviewmodel"
type="com.example.mvvmvariableupdatinginanimend.ViewModel" />
</data>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:animatev1="@{searchviewmodel.model.anim1}"
android:background="@android:color/transparent" >
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<TextView
android:id="@+id/search_button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="202dp"
android:background="#fff"
android:drawableLeft="@android:drawable/ic_menu_search"
android:drawablePadding="12dp"
android:gravity="center_vertical"
android:hint="Search Button1..."
android:onClick="@{searchviewmodel.playSearchAnimation}"
android:textSize="16sp" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrl_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
app:animatev1="@{searchviewmodel.model.anim1}"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
在點擊搜索欄的上面的代碼,我設置anim1爲true,之前寫的BindingAdapter確實AppbarLayout動畫和NestedScrollView監聽anim1中的更改。這似乎按預期工作,但我想要說明我的動畫中Statusbar和searchButton的高度,目前沒有發生,因爲我沒有在AnimHelper中的Animate1函數中獲取這些視圖。
發佈您的代碼然後 – pskink
發佈基本代碼,讓我知道如果您需要更多。 – Roadblock