我有一个 ViewPager,所以在这个视图寻呼机中我有不同的片段,所以在第一个片段中我有一个包含 360 度可旋转图像的 TouchImageView。当我为 Touchimageview 向左或向右滑动时,返回 viewpager 事件也触发意味着 viewpager 正在滚动,所以我想禁用此 imageview 区域的 viewpager 触摸事件。
例如:ViewPager -> Fragment -> TouchImageView
我已经根据需要完成了几个关于禁用 onInterceptTouchEvent() 和 onTouchEvent() 的答案,但它禁用了片段的完整视图。
任何帮助将不胜感激!
ViewPager.java
public class MyCardViewPager extends ViewPager {
private boolean isScrollingEnabled = true;
public MyCardViewPager(@Nonnull Context context, @Nonnull AttributeSet attrs) {
super(context, attrs);
}
public MyCardViewPager(@NonNull Context context) {
super(context);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isScrollingEnabled) {
return super.onInterceptTouchEvent(ev);
}
return false;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isScrollingEnabled) {
return super.onTouchEvent(ev);
}
return false;
}
public void enableScroll(boolean enable) {
isScrollingEnabled = enable;
}
}
片段.java
@Override
public View onCreateView(@Nonnull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myCardViewPager = new MyCardViewPager(context);
myCardViewPager.enableScroll(false);
imageViewSwipeListener();
}
private void imageViewSwipeListener() {
position = 18;
objectList();
imageview.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
@Override
public void onSwipeRight() {
super.onSwipeRight();
position += 1;
if (position == 36) {
position = 0;
}
imageview.setImageResource(objectList.get(position));
}
@Override
public void onSwipeLeft() {
super.onSwipeLeft();
position -= 1;
if (position == -1) {
position = 35;
}
imageview.setImageResource(objectList.get(position));
}
});
}
OnSwipeTouchListener 类
open class OnSwipeTouchListener(ctx: Context) : View.OnTouchListener {
private val gestureDetector: GestureDetector
companion object {
private val SWIPE_THRESHOLD = 100
private val SWIPE_VELOCITY_THRESHOLD = 100
}
init {
gestureDetector = GestureDetector(ctx, GestureListener())
}
override fun onTouch(v: View, event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event)
}
private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent): Boolean {
return true
}
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
var result = false
try {
val diffY = e2.y - e1.y
val diffX = e2.x - e1.x
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight()
} else {
onSwipeLeft()
}
result = true
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom()
} else {
onSwipeTop()
}
result = true
}
} catch (exception: Exception) {
exception.printStackTrace()
}
return result
}
}
open fun onSwipeRight() {}
open fun onSwipeLeft() {}
open fun onSwipeTop() {}
open fun onSwipeBottom() {}
}