vendredi 22 octobre 2021

Reflection vs. finding child view

For the same reason described in this SO question, I want to get access to the RecyclerView inside a ViewPager2. This RecyclerView is not exposed in any way.

I kept the title deliberately broad, because my intention of this question is not to solve a specific question; the problem is solved. The ViewPager2 is mere an example. I want to know if there is an objective better long term approach.

One could:

  • Use reflection to access the property mRecyclerView
  • Find and cast a view child to RecyclerView.
  • (Do not use ViewPager2, but use an RecyclerView and add a layout manager, snapping and paging manually; deemed out of scope for this question)

I'd say:

  • The RV is fetched once, so cost is negligible
  • ViewPager2 is stable enough, that at least the name of the property or the amount of child views will not change.
  • Both solutions hardcode knowledge of ViewPager2 in the app it should now know

In this case, is one better than the other?





Aucun commentaire:

Enregistrer un commentaire