Android: ViewPager FragmentStatePagerAdapter get current View
Asked Answered
G

1

9

Question: How to get current View of a ViewPager FragmentStatePagerAdapter?

  1. I have spent almost 24 hours on this and searched everywhere for the fit solutions.
  2. Get current position is easy, I already got that. This is get current View.
  3. FragmentStatePagerAdapter is required, not PagerAdapter.

    • This solution suggested setTag and findViewWithTag via instantiateItem(View container, int position). This is for PagerAdapter.
    • However in FragmentStatePagerAdapter the instantiateItem has a ViewGroup param not View param: instantiateItem(**ViewGroup** container, int position)

So I hope it's not impossible, is it? Please help!

Geochemistry answered 5/7, 2013 at 17:28 Comment(0)
F
4

You can use the setTag trick with the view you are returning in your instantiateItem method.

Here, I'm using a ViewPager with PagerAdapter, but I think this idea might work for you too.

So, when you are instantiating your views (in PagerAdapter the method is also instantiateItem(ViewGroup container, int position)), you should use setTag to your view.

Like this:
view.setTag("view" + position);
return view;

So, when you need the current view, you should do this: View view = (View) pager.findViewWithTag("view" + pager.getCurrentItem());, where pager is the ViewPager, which you can get passing it by params from your Activity to your adapter.

Friendship answered 15/7, 2013 at 19:43 Comment(2)
sorry for the late reply and thanks for the help. How did you get the 'view' in your 'view.setTag()' while the param is 'ViewGroup container'?Geochemistry
Sorry for the even later reply. Actually the view that I was talking about is the view you return in the end of instantiateItem.Friendship

© 2022 - 2024 — McMap. All rights reserved.