今天我要设置ViewPager。setOnPageChangeListener,我发现它已弃用。


当前回答

: viewpage。setOnPageChangeListener现在已弃用。需要使用ViewPager。addOnPageChangeListener代替。

例如,

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

你可以在这里找到这个。

其他回答

使用:viewpage。addOnPageChangeListener而不是viewpage . setonpagechangelistener。

这招对我很管用:

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {       
        conterchange.setText(""+(1+position));
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

这是新的方法,导航栏与viewpager它为我工作。

binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Home).setChecked(true);
                        break;
                    case 1:
                        binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Booking).setChecked(true);
                        break;
                    case 2:
                        binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Transaction).setChecked(true);
                        break;
                    case 3:
                        binding.bottomNavigation.getMenu().findItem(R.id.nav_item_HealthyFood).setChecked(true);
                        break;
                }

            }
        });

你可以通过SimpleOnPageChangeListener来实现 守则如下:

private Fragment page = null;
/** Defining a listener for pageChange */
        ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager_selectPhotos + ":" + position);

                System.out.println(page);
                switch (position) {
                case 0:

                    break;
                case 1:

                    break;

                default:
                    break;
                }
            }
        };
        /** Setting the pageChange listener to the viewPager */
        _pager.setOnPageChangeListener(pageChangeListener);

如果需要,创建一个接口; FragmentLifecycle { onPauseFragment(); onsumefragment (); } 如所示,在viewpager中的每个片段中实现接口

公共类FragmentCast扩展Fragment实现FragmentLifecycle { @Override onPauseFragment() { 日志。我(标签,“onPauseFragment () "); Toast.makeText(getActivity(), "onPauseFragment():" + TAG, Toast.LENGTH_SHORT).show(); } @Override onResumeFragment() { 日志。我(标签,“onResumeFragment () "); Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show(); }

List this is how u user the addOnPageListener enter viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { int currentPosition = 0; @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { FragmentLifecycle fragmentLifecycleToShow =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem()); fragmentLifecycleToShow.onResumeFragment(); FragmentLifecycle fragmentLifecycleToHide =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, currentPosition); fragmentLifecycleToHide.onPauseFragment(); currentPosition = position; } @Override public void onPageScrollStateChanged(int state) { } }); Implement your calls in the onPauseFragment() or onResumeFragment()