V
V
vladocc2015-12-15 05:10:21
Java
vladocc, 2015-12-15 05:10:21

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

2 answer(s)
V
vahaketiladze, 2015-12-24
@vahaketiladze

getItem - not suitable.
See the solution here:
stackoverflow.com/questions/8785221/retrieve-a-fra... Helped
me when I faced a similar problem.

P
PaulWeb, 2015-12-28
@PaulWeb

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);
    }

and yes, if for some reason your adapter does not work, then you need to look in fragmentmanager, you need to look under what id the fragment was written there and that’s it, I don’t remember the order in which the identifier was formed, but you can easily look at stackoverflow

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question