Answer the question
In order to leave comments, you need to log in
How to get the required Fragment from Activity when using ViewPager?
I have an Activity that has a ViewPager that creates multiple pages for me. I need to call findViewById or something else from the Activity to find the element, but since it is on the fragment that the ViewPager created, everything becomes not so simple. As far as I understand how it works, I first need to find this fragment, and only then look for a view on it, but the studio does not give me the opportunity to call the findFragmentById method in the Activity.
Most likely, I just don’t understand something, so I ask you to point out my mistake and suggest how this is implemented correctly.
The code;
pastebin.com/G0vgJeDj The
error used to be here:
ListView playlist = (ListView) m.findViewById(R.id.Playlist);
Now here:
View m = mSectionsPagerAdapter.getItem(1).getView();
Answer the question
In order to leave comments, you need to log in
getItem - not suitable.
See the solution here:
stackoverflow.com/questions/8785221/retrieve-a-fra... Helped
me when I faced a similar problem.
getItem will not work because, roughly speaking, the fragment blanks are stored there, in fact, the FragmentStatePagerAdapter has a list called mFragments, that's where the link to the already created fragment is saved after the transaction has passed, and how to pull it out, you can simply make your FragmentStatePagerAdapter and add the method there
public Fragment getRegisteredFragment(int position){
return Is.empty(mFragments) ?null:mFragments.get(position);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question