如何检测 FragmentPagerAdapter 显示的第一页或最后一页?

How can I detect the first or last page shown by a FragmentPagerAdapter?

我有一个非常简单的设置伴侣应用程序(用于可穿戴表盘),它使用大约 8 个滑动页面来设置单选按钮的一些选项。我使用根据所选选项实例化的各个片段。

     public class SectionsPagerAdapter extends FragmentPagerAdapter {
 ...
         @Override
    public Fragment getItem(int position) {

        // getItem is called to instantiate the fragment for the given page.
        // Return a different static fragment depending on which menu item
        // It isn't called if we already have the given fragment available
        if ((position < 0) || (position >= mMenuItems.length)) return null;
        String menu = mMenuItems[position];

        //FIXME: We are setting the existing values when instantiated, which means they may not reflect
        //actual current values when the fragment is displayed
        if (menu.equals(getResources().getString(R.string.period_length))) {
            return PeriodLengthFragment.newInstance(menu, mPeriodLength);
        } else if (menu.equals(getResources().getString(R.string.home_team_color))) {
            return HomeTeamColorFragment.newInstance(menu);
        } else if (menu.equals(getResources().getString(R.string.away_team_color))) {
            return AwayTeamColorFragment.newInstance(menu);
        } else if (menu.equals(getResources().getString(R.string.number_of_periods))) {
            return NumberOfPeriodsFragment.newInstance(menu,mNumberOfPeriods);
        }  else if (menu.equals(getResources().getString(R.string.interval_length))) {
            return IntervalLengthFragment.newInstance(menu, mIntervalLength);
        } else if (menu.equals(getResources().getString(R.string.count_up_or_down))) {
            return CountUpOrDownFragment.newInstance(menu);
        } else if (menu.equals(getResources().getString(R.string.about))) {
            return AboutFragment.newInstance(menu);
        }  else if (menu.equals(getResources().getString(R.string.match_results))) {
            return MatchResultsFragment.newInstance(menu,mHomeTeamScore,mAwayTeamScore );
        }
        return null;
    }

我还提供了两个浮动操作按钮 ("fabs"),您可以使用它们代替滑动,同时提供视觉提示,表明还有其他设置页面:

             FloatingActionButton fabPrev = (FloatingActionButton) findViewById(R.id.fab_previous);
        FloatingActionButton fabNext = (FloatingActionButton) findViewById(R.id.fab_next);
        //Make the floating action button invisible if it doesn't make sense
        fabPrev.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
        fabNext.setVisibility(position == mMenuItems.length-1 ? View.INVISIBLE : View.VISIBLE);

不幸的是,虽然我可以在 getItem 方法中访问 position(并相应地更改 fab 的可见性),但它是在页面实例化时调用的,而不是在显示时调用的。因为 PagerAdapter 实例化了下一页和上一页,这导致 fabs 被隐藏在错误的页面上。

有没有我可以在名为 onCreateView 的 PagerAdapter 或 FragmentPagerAdapter 中覆盖并给我位置的方法? 一个丑陋的解决方案是在实际中设置 fab 可见性片段,但如果我重新排列第一页和最后一页的位置,那会很脆弱。

您可以使用 ViewPagersetOnPageChangeListener:

    myViewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int    positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // Check if this is the page you want.
    }
});

在回答我的第二个问题(你如何处理第一页)时,请参阅此处隐藏的建议:https://code.google.com/p/android/issues/detail?id=27526

答案是手动调用 onPageSelected,但前提是您设置了适配器。我的代码出现:

         // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    ViewPager.SimpleOnPageChangeListener vpsopcl = new ViewPager.SimpleOnPageChangeListener() {
        public void onPageSelected(int position) {
            //Make the floating action button invisible if it doesn't make sense
            fabPrev.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
            fabNext.setVisibility(position == mMenuItems.length - 1 ? View.INVISIBLE : View.VISIBLE);
        }
    };
    mViewPager.addOnPageChangeListener(vpsopcl);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    //manually call the OnPageSelected to set the buttons on the first page
    vpsopcl.onPageSelected(0);