我的目标是禁用标签和滑动到最后一页 当用户点击最后一页或尝试滑动到最后一页时,我想要回调,以便我可以显示祝酒..
目前我正在关注此但无法制作
public class CustomSwipeViewPager extends ViewPager {
private boolean mLastPageEnabled = false;
private int mLastPageIndex = 0;
public NoSwipeViewPager(Context context) {
super(context);
}
public NoSwipeViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setLastPageEnabled(boolean enabled) {
mLastPageEnabled = enabled;
}
public void setLastPageIndex(int index) {
mLastPageIndex = index;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event){
if(!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
// Always return false to disable user swipes
return false;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
// Always return false to disable user swipes
return false;
}
return true;
}
}
答案 0 :(得分:1)
试试这个。希望这可以按你的意愿运作。
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!mLastPageEnabled && getCurrentItem() > (mLastPageIndex - 1)) {
switch (event.getAction()) {
case (MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case (MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
dx = x2 - x1;
dy = y2 - y1;
// Use dx and dy to determine the direction
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
//right//previous
return super.onInterceptTouchEvent(event);
} else {
//left//next
return false;
}
}
else
{
return false;
}
}
break;
}
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!mLastPageEnabled && getCurrentItem() > (mLastPageIndex - 1)) {
switch (event.getAction()) {
case (MotionEvent.ACTION_DOWN):
x1 = event.getX();
y1 = event.getY();
break;
case (MotionEvent.ACTION_UP): {
x2 = event.getX();
y2 = event.getY();
dx = x2 - x1;
dy = y2 - y1;
// Use dx and dy to determine the direction
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
//right//previous
return super.onTouchEvent(event);
} else {
//left//next
return false;
}
}
else
{
return false;
}
}
break;
}
}
return super.onTouchEvent(event);
}