由于最近在做墨水屏的相关工作,ViewPager自带的滑动效果在墨水屏上表现的很不好,残影太重了。所以禁止自带的滑动效果,用接口来接管相关逻辑实现自处理。
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
/**
* Created by Hu.Lei on 2019年11月28日, 0028.
* 禁止滑动
*/
public class NoSlidingViewPager extends ViewPager {
float beforeX = 0;
float lastX = 0;
public NoSlidingViewPager(@NonNull Context context) {
super(context);
}
public NoSlidingViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
if (action == MotionEvent.ACTION_DOWN){
beforeX = ev.getX();
Utils.log("NoSlidingViewPager(ACTION_DOWN): x轴方向:beforeX --> " + beforeX);
} else if (action == MotionEvent.ACTION_UP){
lastX = ev.getX();
Utils.log("NoSlidingViewPager(ACTION_UP): x轴方向:beforeX --> " + beforeX + " lastX --> " + lastX);
if (beforeX == lastX){ // 点击事件
return super.onInterceptTouchEvent(ev);
} else {
if (beforeX > lastX){ // 向左滑动
if (listener != null){
listener.onLeftFling();
}
} else if (beforeX < lastX){ // 向右滑动
if (listener != null){
listener.onRightFling();
}
}
beforeX = 0;
lastX = 0;
return true;
}
}
return false;
}
public interface FlingListener{
void onLeftFling();
void onRightFling();
}
private FlingListener listener;
public void setFlingListener(FlingListener listener){
this.listener = listener;
}
}
网友评论