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

标签 java android fragmentpageradapter

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

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

我还提供了两个 float 操作按钮(“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 被隐藏在错误的页面上。

在 PagerAdapter 或 FragmentPagerAdapter 中是否有任何我可以重写的方法,它被称为 onCreateView 并会给我位置?一个丑陋的解决方案是在实际 fragment 中设置 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.
    }
});

关于java - 如何检测 FragmentPagerAdapter 显示的第一页或最后一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35417976/

相关文章:

android - 在 ViewPager 中加载具有不同数据的相同 fragment

java - null = ""对于字符串

java - InflateException 二进制 XML 文件,第 7 行 : Error inflating class fragment

android - java.lang.InstantiationException : can't instantiate class . .. 没有空构造函数

android - 扩展 FragmentPagerAdapter 存在缺少 super 构造函数的问题

android - 标记没有从 mapV2 android 中删除

Java添加类型编写器效果,不会停止整个程序

java - 如何存储文件结构类型的数据以便于检索和插入

java - 将数据放入数组时出现问题

android - 如何从 xml 元素中获取成员值