gpt4 book ai didi

android - ViewPager OnPageChangeListener 不起作用

转载 作者:行者123 更新时间:2023-11-29 14:24:31 29 4
gpt4 key购买 nike

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

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 库,所以根据这个库,我应该在指示器上设置页面监听器。

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

@powder366 的更新:

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

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

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

// 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 中,如下所示:

 <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 + "");*/
}
});

关于android - ViewPager OnPageChangeListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25980540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com