Reputation: 12559
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+"");
}
});
Upvotes: 1
Views: 7135
Reputation: 12559
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 + "");*/
}
});
Upvotes: 10