Skip to content

Commit 86e2985

Browse files
committed
workaround: photoview gestures not working correctly in ViewPager2
1 parent 26f6f88 commit 86e2985

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2024 Christians Martínez Alvarado
3+
*
4+
* Licensed under the GNU General Public License v3
5+
*
6+
* This is free software: you can redistribute it and/or modify it under
7+
* the terms of the GNU General Public License as published by
8+
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU General Public License for more details.
13+
*/
14+
package com.simplified.wsstatussaver.views
15+
16+
import android.content.Context
17+
import android.util.AttributeSet
18+
import android.view.MotionEvent
19+
import android.widget.FrameLayout
20+
21+
/**
22+
* https://github.com/Baseflow/PhotoView/issues/708#issuecomment-1116960531
23+
*/
24+
class PhotoViewWrapper @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
25+
FrameLayout(context, attrs) {
26+
27+
private var isParentInterceptionDisallowed = false
28+
29+
override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
30+
isParentInterceptionDisallowed = disallowIntercept
31+
if (disallowIntercept) {
32+
// PhotoView wants to disallow parent interception, let it be.
33+
parent.requestDisallowInterceptTouchEvent(true) // don't ban wrapper itself
34+
} else {
35+
// PhotoView wants to allow parent interception, we need to re-check it.
36+
}
37+
}
38+
39+
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
40+
// always false when up or cancel event,
41+
// which will allow parent interception normally.
42+
val isMultiTouch = ev.pointerCount > 1
43+
44+
// re-check if it's multi touch
45+
parent.requestDisallowInterceptTouchEvent(isParentInterceptionDisallowed || isMultiTouch)
46+
return false
47+
}
48+
}

app/src/main/res/layout/fragment_image.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
~ See the GNU General Public License for more details.
1313
-->
1414

15-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
15+
<com.simplified.wsstatussaver.views.PhotoViewWrapper xmlns:android="http://schemas.android.com/apk/res/android"
1616
xmlns:tools="http://schemas.android.com/tools"
1717
android:layout_width="match_parent"
1818
android:layout_height="match_parent"
@@ -38,4 +38,4 @@
3838

3939
</FrameLayout>
4040

41-
</FrameLayout>
41+
</com.simplified.wsstatussaver.views.PhotoViewWrapper>

0 commit comments

Comments
 (0)