Search code examples
androidandroid-recyclerviewandroid-support-librarymvvmcrossandroid-appcompat

FindViewWithTag on RecyclerView (AppCompat)


To use ActivityOptionsCompat.MakeScaleUpAnimation(), I need access to the View from where the animation originates. I use MvvmCross, so I need to pass via a MvxFragmentsPresenter (shouldn't be relevant to the problem). A ViewModel request can contain key/value parameters. So I attach a Tag to each RecyclerView item and then pass on that Tag as a parameter in the ViewModel request, as done here. I can access this tag in the presenter.

Now inside the presenter I need to find the RecyclerView item that was clicked. I want to do this as follows:

View contentFrame = Activity.FindViewById(Resource.Id.content_frame);
View recyclerView = contentFrame.FindViewById(Resource.Id.recycler_view);
View item = recyclerView.FindViewWithTag("mytag");

Unfortunately this results in null. I can access the contentFrame and the recyclerView, but not an item by Tag. However I can even get the first item using:

recvddfg.GetChildAt(0);

... and access the Tag! So it's strange that FindViewWithTag isn't working. Does anyone have an idea?


Solution

  • After searching for hours, I decided to post this question. Then looked back at it and found the cause: I was searching for an item with a tag of type string, while I should have been searching for a number... You can use any type you want with a Tag, so in your case it might be different if you have the same problem. You just need to be consistent.