Search code examples
androidandroid-recyclerviewandroid-imageview

How to rotate image when expand recyclerView in Android


In my application, I want expand and collapse recyclerView!
I can expand and collapse recyclerView and for this I use this library : https://github.com/thoughtbot/expandable-recycler-view

I want when click on recyclerView (expand or collapse) rotate arrow image!
I write below code in viewHolder class but not work and not rotate arrow image!
ParentViewHolder codes:

public class seasonParentViewHolder extends GroupViewHolder {

    private TextView row_epGuideParent_Title, row_epGuideParent_Count, row_epGuideParent_Date;
    private ImageView row_epGuideParent_arrowImg;

    public seasonParentViewHolder(View itemView) {
        super(itemView);
        row_epGuideParent_Title = (TextView) itemView.findViewById(R.id.row_epGuideParent_Title);
        row_epGuideParent_Count = (TextView) itemView.findViewById(R.id.row_epGuideParent_Count);
        row_epGuideParent_Date = (TextView) itemView.findViewById(R.id.row_epGuideParent_Date);
        row_epGuideParent_arrowImg = (ImageView) itemView.findViewById(R.id.row_epGuideParent_arrowImg);
    }

    public void setGenreTitle(ExpandableGroup title) {
        if (title instanceof ExpandableModel) {
            row_epGuideParent_Title.setText(title.getTitle());
            row_epGuideParent_Count.setText(((ExpandableModel) title).getCount());
            row_epGuideParent_Date.setText(((ExpandableModel) title).getDate());
        }
    }

    @Override
    public void expand() {
        animateExpand();
    }

    @Override
    public void collapse() {
        animateCollapse();
    }

    private void animateExpand() {
        RotateAnimation rotate =
                new RotateAnimation(360, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(300);
        rotate.setFillAfter(true);
        row_epGuideParent_arrowImg.setAnimation(rotate);
    }

    private void animateCollapse() {
        RotateAnimation rotate =
                new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(300);
        rotate.setFillAfter(true);
        row_epGuideParent_arrowImg.setAnimation(rotate);
    }
}

Called expand and collapse in library :

public abstract class GroupViewHolder extends RecyclerView.ViewHolder implements OnClickListener {

  private OnGroupClickListener listener;

  public GroupViewHolder(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    if (listener != null) {
      if (listener.onGroupClick(getAdapterPosition())) {
        collapse();
      } else {
        expand();
      }
    }
  }

  public void setOnGroupClickListener(OnGroupClickListener listener) {
    this.listener = listener;
  }

  public void expand() {}

  public void collapse() {}

}

How can I fix this problem and rotate arrow image when click on recyclerView (expand or collapse) ???


Solution

  • TL;DR You have to use startAnimation() for your ImageView instead of setAnimation().

    void setAnimation (Animation animation)

    Sets the next animation to play for this view. If you want the animation to play immediately, use startAnimation(android.view.animation.Animation) instead [...]

    (quoted from developer.android.com/reference/android/view/View.html)