从代码here开始,我有一个设置适配器的ViewPager。有没有办法禁用滚动动画,以便它在我滑动时“跳转”到新页面?我不打算在onTouchEvent
和onInterceptTouchEvent
中禁用整个滚动功能,只是滑动动画。
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
答案 0 :(得分:16)
我不知道是否有一个干净的解决方案。但是您可以使用技巧并使用另一个变换器撤消标准页面变换器。 NoPageTransformer
看起来像这样:
private static class NoPageTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
if (position < 0) {
view.setScrollX((int)((float)(view.getWidth()) * position));
} else if (position > 0) {
view.setScrollX(-(int) ((float) (view.getWidth()) * -position));
} else {
view.setScrollX(0);
}
}
}
要将其添加到ViewPager,请致电:
mViewPager.setPageTransformer(false, new NoPageTransformer());
可以在SDK 16及更高版本中使用。
答案 1 :(得分:2)
Kotlin:
viewpager.setPageTransformer(false, FadePageTransformer())
inner class FadePageTransformer : ViewPager.PageTransformer {
override fun transformPage(view: View, position: Float) {
view.translationX = view.width * -position
if (position <= -1.0f || position >= 1.0f) {
view.alpha = 0.0f
view.setVisibility(View.GONE);
} else if (position == 0.0f) {
view.alpha = 1.0f
view.setVisibility(View.VISIBLE);
} else {
// position is between -1.0F & 0.0F OR 0.0F & 1.0F
view.alpha = 1.0f - Math.abs(position)
view.setVisibility(View.GONE);
}
}
}
答案 2 :(得分:0)
接受的答案仍然具有滑动动画 我正在搜索甚至禁用用户滑动动画 这是我的实现
1-覆盖Viewpager
方法onInterceptTouchEvent
和onTouchEvent
2-创建自己的GestureDetector
3-检测滑动手势并使用setCurrentItem(item, false)
ViewPager
public class ViewPagerNoSwipe extends ViewPager {
private final GestureDetector gestureDetector;
private OnSwipeListener mOnSwipeListener;
public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
mOnSwipeListener = onSwipeListener;
}
public ViewPagerNoSwipe(@NonNull Context context) {
super(context);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public ViewPagerNoSwipe(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
gestureDetector.onTouchEvent(ev);
return false;
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
if(mOnSwipeListener!=null)
mOnSwipeListener.onSwipeRight();
} else {
if(mOnSwipeListener!=null)
mOnSwipeListener.onSwipeLeft();
}
result = true;
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
if(mOnSwipeListener!=null)
mOnSwipeListener.onSwipeBottom();
} else {
if(mOnSwipeListener!=null)
mOnSwipeListener.onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public interface OnSwipeListener {
void onSwipeRight();
void onSwipeLeft();
void onSwipeTop();
void onSwipeBottom();
}
}
设置ViewPager时,设置swipeListener
postsPager.setOnSwipeListener(new ViewPagerNoSwipe.OnSwipeListener() {
@Override
public void onSwipeRight() {
postsPager.setCurrentItem(postsPager.getCurrentItem() + 1,false);
}
@Override
public void onSwipeLeft() {
postsPager.setCurrentItem(postsPager.getCurrentItem() - 1, false);
}
...
}
答案 3 :(得分:0)
viewPager.setPageTransformer(false, new NoPageTransformer());
private static class NoPageTransformer implements ViewPager.PageTransformer {
public void transformPage(@NonNull View view, float position) {
view.setTranslationX(view.getWidth() * -position);
if (position <= -1.0f || position >= 1.0f) {
view.setVisibility(View.GONE);
} else if (position == 0.0f) {
view.setVisibility(View.VISIBLE);
} else {
view.setVisibility(View.GONE);
}
}
}