redstonedev
redstonedev

Reputation: 119

How to get reference to a view of a fragment that's in a ViewPager?

I have a Fragment in a ViewPager, and I want to access a View in said Fragment in order to change its background from the FragmentActivity.

I've been trying with every answer related I could find. No luck yet. Any ideas?

-R

Upvotes: 1

Views: 703

Answers (2)

Ankit Bansal
Ankit Bansal

Reputation: 1811

  • Create a function inside ViewPager Adapter to send back Fragments
  • In the getItem() function, along with returning the fragment add it in an array as well.. this will keep track of instantiated fragments based on their position.
  • Now whenever required call get the fragment you need, then retrieve the view from that fragment.

Upvotes: 1

ElDuderino
ElDuderino

Reputation: 3263

Try this to get your fragment :

YourFrag frag = (YourFrag) viewPager.getAdapter().instantiateItem(viewPager, pageNumber);

then you can get the views as usual

frag.getView().findViewById() ... 

or however you like.

Upvotes: 1

Related Questions