2015-05-30 31 views
0

我正在使用recyclerview显示服务器的搜索结果,并且我想要显示一张卡片在其中一个接一个地滑动的动画,以便您获得良好的滑动效果。如何做到这一点?Android recyclerview在加载时刷卡

这个动画可以在继电器reddit的应用程序被发现,当你刷新内容

+0

你想要的动画时,项目加载? – Kartheek

+0

是的。它在加载项目时显示进度曲线,一旦加载完成,卡片应该刷入。 – qwertz

回答

1

按照ItemAnimator文档:

此类定义取物品的地方为改变 动画是由适配器制造的。

所以,除非您逐项将您的项目添加到您的RecyclerView并在每次迭代中刷新视图,否则我认为ItemAnimator不是您需要的解决方案。

下面是当他们出现使用CustomAdapter你如何动画RecyclerView项目:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> 
{ 
    private Context context; 

    // The items to display in your RecyclerView 
    private ArrayList<String> items; 
    // Allows to remember the last item shown on screen 
    private int lastPosition = -1; 

    public static class ViewHolder extends RecyclerView.ViewHolder 
    { 
     TextView text; 
     // You need to retrieve the container (ie the root ViewGroup from your custom_item_layout) 
     // It's the view that will be animated 
     FrameLayout container; 

     public ViewHolder(View itemView) 
     { 
      super(itemView); 
      container = (FrameLayout) itemView.findViewById(R.id.item_layout_container); 
      text = (TextView) itemView.findViewById(R.id.item_layout_text); 
     } 
    } 

    public CustomAdapter(ArrayList<String> items, Context context) 
    { 
     this.items = items; 
     this.context = context; 
    } 

    @Override 
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_item_layout, parent, false); 
     return new ViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) 
    { 
     holder.text.setText(items.get(position)); 

     // Here you apply the animation when the view is bound 
     setAnimation(holder.container, position); 
    } 

    /** 
    * Here is the key method to apply the animation 
    */ 
    private void setAnimation(View viewToAnimate, int position) 
    { 
     // If the bound view wasn't previously displayed on screen, it's animated 
     if (position > lastPosition) 
     { 
      Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); 
      viewToAnimate.startAnimation(animation); 
      lastPosition = position; 
     } 
    } 
} 

和你custom_item_layout应该是这样的:

<FrameLayout 
    android:id="@+id/item_layout_container" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/item_layout_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
     android:gravity="center_vertical" 
     android:minHeight="?android:attr/listPreferredItemHeightSmall"/> 

</FrameLayout> 
+0

谢谢,将尽快尝试 – qwertz