问题描述
我有一个扩展SimpleOnPageChangeListener类,并在我的onPageScrollStateChanged方法,我希望能够确定用户是否刷卡向前或向后通过ViewPager。即他们是否有刷卡从左或从右到左。
I have a class that extends SimpleOnPageChangeListener and in my onPageScrollStateChanged method I want to be able to determine whether the user has swiped forwards or backwards through the ViewPager. I.e. Whether they have swiped left-to-right or right-to-left.
我已经做了很多谷歌上搜索这个,但我无法找到任何关于它。我期待onPageScrollStateChanged方法将提供一个参数,说明哪个方向轻扫是,但事实并非如此。
I've done a lot of googling on this but I can't find anything about it. I was expecting the onPageScrollStateChanged method would provide a parameter stating which direction the swipe was but it doesn't.
@Override
public void onPageScrollStateChanged(int state) {
// Determine whether the user is swiping forwards or backwards through the ViewPager
}
有没有人有什么想法?
Does anyone have any ideas?
干杯迈克
推荐答案
使用的ViewPager.SimpleOnPageChangeListener,并保持一个实例变种与当前选项卡POS机,这样就可以计算出哪一种方式它被偷走。
Use the ViewPager.SimpleOnPageChangeListener and keep a instance var with the current tab pos, that way you can work out which way it's been swiped.
private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(final int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
mCurrentTabPosition = position;
}
};
protected void onTabChanged(final PagerAdapter adapter, final int oldPosition, final int newPosition) {
//Calc if swipe was left to right, or right to left
if (oldPosition>newPosition){
// left to right
}
else{
//right to left
}
}
这篇关于Android的SimpleOnPageChangeListener - 确定刷卡方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!