本文介绍了ViewPager OnPageChangeListener不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是自定义的ViewPager,当我滑到了新的一页ViewPager.OnPageChangeListener不起作用。可能是什么原因呢?
mPager =(WrapContentHeightViewPager)findViewById(R.id.pager); mPager.setOnPageChangeListener(新ViewPager.OnPageChangeListener(){
公共无效onPageScrollStateChanged(INT状态){}
公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){} 公共无效使用onPageSelected(INT位置){
//检查是否这是你想要的页面。
currentFav =位置;
Log.i(currentFav POS,currentFav +);
}
});
解决方案
我使用ViewPagerIndicator库,以便按照这个图书馆,我应该已经设置页面上监听指标。
mIndicator.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
@覆盖
公共无效使用onPageSelected(INT位置){
doYourThing();
}
});
更新@ powder366:
在您的build.gradle文件,添加到库的依赖
编译com.viewpagerindicator:库:2.4.1@aar
在顶层的build.gradle文件,添加这些:
//顶级构建文件,您可以添加常用的配置选项,所有分项目/模块。buildscript {
库{
行家{URLhttp://dl.bintray.com/populov/maven}
mavenCentral()
jcenter()
}
依赖{
类路径'com.android.tools.build:gradle:1.2.3 //注意:不要在这里把你的应用程序依赖关系;他们属于
//在单个模块的build.gradle文件
}
}allprojects {
库{
行家{URLhttp://dl.bintray.com/populov/maven}
mavenCentral()
jcenter()
}
}
您可以将XML添加的指标如下:
< com.viewpagerindicator.CirclePageIndicator
机器人:ID =@ + ID /指示器
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/蓝
机器人:填充=10dip
应用:填充颜色=@彩色/ circleindicatorfill
应用:pageColor =@彩色/ circleindicatorempty
应用:strokeWidth =0dp/>
获取指标如下:
mIndicator =(CirclePageIndicator)findViewById(R.id.indicator);mViewPager.setAdapter(mFragmentAdapter);mIndicator.setViewPager(mViewPager);mIndicator
.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){
公共无效onPageScrollStateChanged(INT状态){
} 公共无效onPageScrolled(INT位置,
浮positionOffset,诠释positionOffsetPixels){
} 公共无效使用onPageSelected(INT位置){ //检查是否这是你想要的页面。
/ * currentFav =位置;
Log.i(currentFav POS,currentFav +); * /
}
});
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+"");
}
});
解决方案
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();
}
});
Update for @powder366:
In your build.gradle file, add library into dependencies
compile 'com.viewpagerindicator:library:2.4.1@aar'
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()
}
}
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" />
Get indicator as following:
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不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!