Get focused View from ViewPager

后端 未结 11 1306
情深已故
情深已故 2020-11-27 12:50

i use the ViewPager for switching views with left/right swipe.

The ViewPager needs an Adapter, so I\'ve built this one:

public class ListViewPagerAda         


        
11条回答
  •  [愿得一人]
    2020-11-27 13:42

    Am I missing something? There's only ever 3 children inside the ViewGroup, so it boils down to:

    int current = viewPager.getCurrentItem();
    int childCount = viewPager.getChildCount();
    // If there's a single page or we're at the beginning, return the first view
    if (childCount == 1 || current == 0) {
      return viewPager.getChildAt(0);
    } else { //For any other case, we want the second child. This is either the last page or the page in the middle for any other case.
      return viewPager.getChildAt(1);
    }
    

提交回复
热议问题