Android ViewPager get the current View

前端 未结 15 2411
天命终不由人
天命终不由人 2020-11-28 06:24

I have a ViewPager, and I\'d like to get the current selected and visible view, not a position.

  1. getChildAt(getCurrentItem) returns wrong Vi
15条回答
  •  我在风中等你
    2020-11-28 06:27

    You can find fragment by system tag. It's work for me. I used it in OnMeasure function.

    id - viewPager ID

    position - fragment which you want to get

    Important! You get this fragment if your fragment was created in adapter. So you must to check supportedFragmentManager.findFragmentByTag("android:switcher:" + id + ":" + position) nullify

    You can get view like this:

    supportedFragmentManager.findFragmentByTag("android:switcher:" + id + ":" + position).view
    

    You shouldn't give custom tag in adapter

提交回复
热议问题