2017-10-11 102 views
0

我跟着一個教程來爲recyclerview項目設置動畫,但動畫不起作用,我很困惑動畫是否被應用。Recycler查看項目輸入動畫不起作用

Activity.Java: 更新的代碼是在這裏,我試圖用按一下按鈕,但我不知道如何從調用動畫方法onBindHolder形成適配器

private void setupRecyclerView() { 
       RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(mLayoutManager); 
     final LayoutAnimationController controller = 
       AnimationUtils.loadLayoutAnimation(this, R.anim.layout_animation); 

     recyclerView.setLayoutAnimation(controller); 
     recyclerView.setAdapter(specialistListAdapter); 
    } 


    //placed a dummy button onclick to check animation working or not 
public void reloadData(View view) { 
     runLayoutAnimation(recyclerView); 
    } 

private void runLayoutAnimation(final RecyclerView recyclerView) { 
    final Context context = recyclerView.getContext(); 

    final LayoutAnimationController controller = 
      AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation); 

    recyclerView.setLayoutAnimation(controller); 
    recyclerView.getAdapter().notifyDataSetChanged(); 
    recyclerView.scheduleLayoutAnimation(); 
} 

layout_animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:animation="@anim/item_list_animation" 
    android:delay="15%" 
    android:animationOrder="normal" 
    /> 

item_list_animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 

    <translate 
     android:fromYDelta="-20%" 
     android:toYDelta="0" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     /> 

    <alpha 
     android:fromAlpha="0" 
     android:toAlpha="1" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     /> 

    <scale 
     android:fromXScale="105%" 
     android:fromYScale="105%" 
     android:toXScale="100%" 
     android:toYScale="100%" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:interpolator="@android:anim/decelerate_interpolator" 
     /> 
</set> 

預先感謝

回答

2

首先你需要加載動畫無論是在XML或編程

<android.support.v7.widget.RecyclerView 
android:layout_width="match_parent" 
android:layout_height="match_parent"           
android:layoutAnimation="@anim/layout_animation"/> 

OR

LayoutAnimationController animation = 
AnimationUtils.loadLayoutAnimation(ctx, R.anim.layout_animation); 
recyclerview.setLayoutAnimation(animation); 

回收站視圖你也需要調用這個方法在OnBindViewHolder中,如runLayoutAnimation(YOUR_RECYCLER_VIEW)

+0

已經載入了動畫,你可以在runLayoutAnimationfunction中看到,但是我怎麼會在onBindViewHolder中調用方法 –

+0

在onBindViewholder中,只要你有一些動作需要執行,你需要調用該方法。想要刪除一行,然後在onclickListener中調用該行。 – Anonymous

+0

此外,您不需要適配器中的回收視圖對象。您可以獲取像yourViewHolder.itemView.getContext()這樣的上下文; – Anonymous