1

I have a special animation that populates the cards for my RecyclerView. The animation works good (at least for inflating). I don't want this animation to be used after initial inflation. In other words, I don't want any animation for scrolling (or really any action by the user after initial inflation of enough cards to fill the user's screen). I can't figure out how to grab the position number -or- turn off animation after populating -or- any way to do this. My animation is called in the adapter. Here's the adapter code that I am using to set the animation.

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ViewHolder>{
private ArrayList<TagDataModel> dataSet;
private Context context;

public TagAdapter (ArrayList<TagDataModel> ds, Context ctx){
    this.dataSet =ds;
    this.context =ctx;
}

class ViewHolder extends RecyclerView.ViewHolder {
    TextView tv_tag;
    ArrayList<TagDataModel> dataSet = new ArrayList<>();
    Context context;
    CardView card;

    ViewHolder(View itemView, Context ctx, ArrayList<TagDataModel> ds) {
        super(itemView);
        this.dataSet = ds;
        this.context =ctx;
        this.tv_tag = (TextView) itemView.findViewById(R.id.tv_cardTag);
        this.card = (CardView) itemView.findViewById(R.id.card_view_tag);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_tag, parent, false);
    ViewHolder viewHolder = new ViewHolder(view, context, dataSet);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    final int activePosition = context.getSharedPreferences("userPrefs", Context.MODE_PRIVATE).getInt("np_tag", 0);
    TextView tv_tag = holder.tv_tag;
    CardView card = holder.card;
    tv_tag.setText(dataSet.get(position).getTag());

    });
    animate(holder.itemView, position);  //animate only first page UPDATE
}

@Override
public int getItemCount() {
    return dataSet.size();
}

private void animate(View view, final int pos) {
    view.animate().cancel();
    view.setTranslationY(100);
    view.setAlpha(0);
    view.animate().alpha(1.0f).translationY(0).setDuration(300).setStartDelay(pos *100);
}
seekingStillness
  • 4,833
  • 5
  • 38
  • 68

2 Answers2

0

Remove your current animation from bindViewHolder and use custom ItemAnimator and override animateAdd method. You can find more here

This should give you the desired result.

public class MyItemAnimator extends DefaultItemAnimator {

 @Override
    public boolean animateAdd(ViewHolder viewHolder) {
        animate(viewHolder.itemView);
        return true;
    }

private void animate(View view) {
    view.setTranslationY(100);
    view.setAlpha(0);
    view.animate().alpha(1.0f).translationY(0).setDuration(300);
}


}

Now set this item animator to your recyclerview.

recyclerView.setItemAnimator(new MyItemAnimator());

Try this :)

Community
  • 1
  • 1
Nishant Verma
  • 110
  • 2
  • 8
0

It would be helpful if you provide the code of Activity where you use this RecyclerView. Anyhow, this answer will be helpful for you to detect whether your RecyclerView has reached the last item or not.
And this answer will help you to detect whether your layout is completely created/inflated.

Community
  • 1
  • 1
  • I do not want to target last item, I want to target RecyclerView has *finished inflating*. This will be a much smaller number. – seekingStillness Apr 07 '17 at 06:46
  • Your requirement is to start animation when `RecyclerView` starts being created/inflated and stop animation when it is done. Am I right? In order to properly understand what you're currently doing with `RecyclerView`, you'll also have to provide the code where you're using this recyclerView (i.e your `Activity`) – Muhammad Waqas Apr 07 '17 at 07:43
  • Anyhow, [this answer](http://stackoverflow.com/a/31854673/2788613) might still help. This contains a listener that is called when layout is completely loaded/inflated. – Muhammad Waqas Apr 07 '17 at 07:46