ViewPager2 PageChangeListener 不像普通的 ViewPager 那样工作。?
问问题
6475 次
3 回答
26
有一种registerOnPageChangeCallback
方法:
pager.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels)
}
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
}
override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
}
})
于 2021-03-05T10:10:10.477 回答
7
下面是 ViewPager2 中 OnPageChangeListener 的示例代码。
class ViewPager2PageChangeCallback(private val listener: (Int) -> Unit) : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
when (position) {
//YOUR CODE
}
}
}
于 2020-01-22T06:36:00.247 回答
1
在 Java 中可以这样编码:
mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position,
float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
setProgress((position + positionOffset) / (mPageCount - 1));
}
});
于 2021-09-15T10:19:16.627 回答