2016-11-21 24 views
1

我已經開始在我的一個項目中使用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函數中獲取這些視圖。

+0

發佈您的代碼然後 – pskink

+0

發佈基本代碼,讓我知道如果您需要更多。 – Roadblock

回答

1

您應該能夠將其他視圖作爲綁定表達式中的值傳遞。然後

<TextView ... 
    app:animatev1="@{...}" 
    app:otherView="@{myOtherView}"/> 
<TextView android:id="@+id/myOtherView" .../> 

你綁定適配器可以採取兩個參數:

@BindingAdapter({"animatev1", "otherView"}) 
public static void animateV1(View view, boolean b, View otherView) { 
    //... 
} 

關於你的第二個問題,我不認爲你應該擔心的同步。他們應該同時開始動畫。

在數據綁定的兩個不同視圖上使用AnimatorSet沒有很好的方法。您可以通過將一個View傳遞給另一個的綁定適配器並在同一個綁定適配器中爲它們設置動畫。但這不是一個好選擇。最好在不同的綁定適配器中單獨設置動畫,並依靠框架來做正確的事情。

+0

這回答了我的問題。然而,只需要添加,視圖id必須是camelCase,並且不能用下劃線(_)分開,否則會出現錯誤「標識符必須是來自xml文件的用戶定義類型。my_other_view缺少它」 – Roadblock

+0

是,數據綁定更改從下劃線名稱到駱駝案例,就像文件名稱一樣。 –