0

I want to animate ListView items when they first appear. I have the following viewholder:

public class SimpleViewHolder extends RecyclerView.ViewHolder
{
    private TextView  simpleTextView;

    public SimpleViewHolder(final View itemView, final SimpleAdapter.onItemClickListener listener) 
    {
        super(itemView);

        simpleTextView  = (TextView)  itemView.findViewById(R.id.simple_text);

        RotateAnimation rotate = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotate.setDuration(1000);
        rotate.setRepeatCount( 0 );
        simpleTextView.setAnimation(rotate);
    }

    public void bindData(final SimpleViewModel viewModel)
    {
        simpleTextView.setText( viewModel.getSimpleText() );
    }
}

Everything works great, except instead of setting the animations programmatically, I would like to load them from an XML file using the following method:

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);

But I'm not clear how to get/pass the context to the RecyclerView.ViewHolder or is this even the proper place where to do animations.

How can I load the XML animation within the RecyclerView.ViewHolder and is this correct place for doing animations for the list items? Thanks!

0x29a
  • 733
  • 1
  • 7
  • 23
  • https://stackoverflow.com/questions/26724964/how-to-animate-recyclerview-items-when-they-appear – Lino Aug 05 '18 at 14:05

3 Answers3

3

you can use itemView.context() to get the context

just context() is enough

Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34
  • 1
    down-voted, the question was `How can I load the XML animation within the RecyclerView` - which is not being answered, at all. – Martin Zeitler Aug 05 '18 at 14:49
  • 2
    The problem clearly says that not able to get context inside the viewholder. Hence it was not possible to call animation. With my solution, context will be provided and it resolves the animation issue in the above code – Viswanath Kumar Sandu Aug 05 '18 at 14:57
  • I just wondered why you accepted this answer, which does neither fully answer the question, nor consider the situation of the XML preview (where one cannot get the context that easily, which is subsequently preventing the preview from rendering). just compare, a quality answer looks different. – Martin Zeitler Aug 05 '18 at 15:02
  • Maybe you should read the post again. Its very clear that missing context is the issue. If its resolved, I don't understand what would be the problem for you. A Quality answer doesn't mean lengthy messages. It means a direct solution to the problem – Viswanath Kumar Sandu Aug 05 '18 at 15:06
  • 1
    Upvoted because you answered the question in the title, which is what we're all here for. – Anthony Aug 22 '20 at 16:41
0

I don't disagree with the placing of animation. I think it's the correct place. About context, I would send it in the constructor.

public SimpleViewHolder(final View itemView, final SimpleAdapter.onItemClickListener listener, Context context) {
 //use this context...

}

if your Recyclerview has no context then you can pass context to Recycleview as well. I dont think there is another way around

Abubakar
  • 1,022
  • 10
  • 20
0

the proper way to obtain the Context might be eg. within the implementation of onLongClick():

@Override
public boolean onLongClick(View viewHolder) {

    this.mRecyclerView = (SomeLinearView) viewHolder.getParent();

    Context context;
    if(viewHolder.isInEditMode()) {
        context = ((ContextThemeWrapper) this.mRecyclerView.getContext()).getBaseContext();
    } else  {
        context = this.mRecyclerView.getContext();
    }
}

and this does not crash in edit-mode (which is the XML preview). declaring all the variables as final is merely useless and often hindering, unless being required to do so, because of changing the scope.

and one can apply a layout-animation alike:

int resId = R.anim.some_animation;
LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(context, resId);
this.mRecyclerView.setLayoutAnimation(animation);
Martin Zeitler
  • 1
  • 19
  • 155
  • 216