我正在显示一个仅包含 ImageViews 的 ViewPager。但是,我需要它以循环方式连续发生。例如:...c > B > A > B > c...
问问题
7630 次
3 回答
4
只需实现 ViePager.OnPageChangeListener:
YOUR_VIEWPAGER.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// not needed
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
int pageCount = pages.size();
if (currentPage == 0){
YOUR_VIEWPAGER.setCurrentItem(pageCount-2,false);
} else if (currentPage == pageCount-1){
YOUR_VIEWPAGER.setCurrentItem(1,false);
}
}
}
});
于 2012-11-07T05:43:16.163 回答
1
试试这个。它会得到像 C <--> A <--> B <--> C <--> A 这样的结果
viewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
var currentPage : Int = 0
var mPreviousPosition : Int = 0
var mIsEndOfCycle = false
override fun onPageScrollStateChanged(state: Int) {
val pageCount = viewPager?.adapter?.count
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (mPreviousPosition == currentPage && !mIsEndOfCycle) {
if (currentPage == 0) {
pageCount?.minus(1)?.let { viewPager?.setCurrentItem(it, false) };
} else {
viewPager?.setCurrentItem(0, false);
}
mIsEndOfCycle = true;
} else {
mIsEndOfCycle = false;
}
mPreviousPosition = currentPage;
}
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
currentPage = position
}
})
于 2020-02-12T05:25:17.547 回答
0
以下为我做了。我不希望用户从第一页“向后”滚动到最后一页。所以基本上我有三个不同的屏幕,但实际上我在适配器中有四个,如下所示:A -> B -> C -> A。当用户点击第二个“A”页面,并且寻呼机状态变为空闲时,我无缝切换到第一个“A”页。我什至有一个同样被覆盖的页面指示器。
val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrollStateChanged(state: Int) {
super.onPageScrollStateChanged(state)
if (state == ViewPager.SCROLL_STATE_IDLE) {
if (viewPager.currentItem == NUM_PAGES - 1) {
viewPager.setCurrentItem(0, false)
}
}
}
}
于 2021-01-08T13:59:05.657 回答