ViewPager OnPageChangeListener不起作用 [英] ViewPager OnPageChangeListener doesn't work

查看:168
本文介绍了ViewPager OnPageChangeListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义ViewPager和ViewPager.OnPageChangeListener在滑到新页面时不起作用.可能是什么原因?

I am using a custom ViewPager and ViewPager.OnPageChangeListener doesn't work when I slide to a new page. What might be the reason?

mPager = (WrapContentHeightViewPager) findViewById(R.id.pager);

mPager.setOnPageChangeListener(new ViewPager.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.
        currentFav = position;
        Log.i("currentFav pos", currentFav+"");
    }
});

推荐答案

我正在使用ViewPagerIndicator库,因此根据该库,我应该将页面侦听器设置到指标上.

I am using ViewPagerIndicator library so according to this library, I should have set page listener onto indicator.

mIndicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            doYourThing();
        }
    });

@ powder366的更新:

Update for @powder366:

在build.gradle文件中,将库添加到依赖项中

In your build.gradle file, add library into dependencies

compile 'com.viewpagerindicator:library:2.4.1@aar'

在顶级build.gradle文件中,添加以下内容:

In your top-level build.gradle file, add those:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        mavenCentral()
        jcenter()
    }
}

您可以按如下所示将指示符添加到xml中:

You can add indicator to your xml as following:

 <com.viewpagerindicator.CirclePageIndicator
                            android:id="@+id/indicator"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@color/blue"
                            android:padding="10dip"
                            app:fillColor="@color/circleindicatorfill"
                            app:pageColor="@color/circleindicatorempty"
                            app:strokeWidth="0dp" />

获取指标如下:

mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);

mViewPager.setAdapter(mFragmentAdapter);

mIndicator.setViewPager(mViewPager);

mIndicator
         .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    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.
                       /* currentFav = position;
                        Log.i("currentFav pos", currentFav + "");*/
                    }
                });

这篇关于ViewPager OnPageChangeListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆