fixed scrolling behavior, disabled R8 full mode

This commit is contained in:
nuclearfog 2023-07-30 18:05:23 +02:00
parent f61cab03c9
commit 588a901db5
No known key found for this signature in database
GPG Key ID: 03488A185C476379
3 changed files with 34 additions and 40 deletions

View File

@ -3,10 +3,10 @@ package org.nuclearfog.twidda.ui.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
@ -26,15 +26,17 @@ public class CollapseLayout extends NestedScrollView implements OnScrollChangeLi
/**
* scrollview position threshold to lock/unlock child scrolling
*/
private static final int SCROLL_THRESHOLD = 50;
private static final int SCROLL_THRESHOLD = 10;
private int headerId, bodyId;
@Nullable
private View header;
@Nullable
private LockableConstraintLayout body;
private float xPos, yPos;
@IdRes
private int headerId = NO_ID;
@IdRes
private int bodyId = NO_ID;
/**
*
@ -68,49 +70,24 @@ public class CollapseLayout extends NestedScrollView implements OnScrollChangeLi
@Override
public boolean aquireLock() {
public boolean aquireVerticalScrollLock() {
return header != null && getScrollY() < header.getMeasuredHeight() - SCROLL_THRESHOLD;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
float deltaX = ev.getX() - xPos;
float deltaY = ev.getY() - yPos;
// lock x-axis when swiping up/down
if (Math.abs(deltaX) > Math.abs(deltaY) * 1.1f) {
setNestedScrollingEnabled(false);
}
// fall through
case MotionEvent.ACTION_DOWN:
// note start coordinates of the gesture
xPos = ev.getX();
yPos = ev.getY();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setNestedScrollingEnabled(true);
break;
}
return false;
public void lockVerticalScroll(boolean lock) {
setNestedScrollingEnabled(lock);
}
@Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
if (headerId != 0) {
header = findViewById(headerId);
}
if (bodyId != 0) {
body = findViewById(bodyId);
body.addLockCallback(this);
}
header = findViewById(headerId);
body = findViewById(bodyId);
if (body != null) {
body.addLockCallback(this);
body.getLayoutParams().height = getMeasuredHeight();
body.requestLayout();
}

View File

@ -14,6 +14,8 @@ import androidx.constraintlayout.widget.ConstraintLayout;
*/
public class LockableConstraintLayout extends ConstraintLayout {
private static final float LOCK_RATIO = 1.1f;
@Nullable
private LockCallback callback;
private boolean xLock = false;
@ -43,17 +45,20 @@ public class LockableConstraintLayout extends ConstraintLayout {
float deltaX = ev.getX() - xPos;
float deltaY = ev.getY() - yPos;
// lock x-axis when swiping up/down
if (!xLock && Math.abs(deltaY) > Math.abs(deltaX) * 2.0f) {
if (!xLock && Math.abs(deltaY) > Math.abs(deltaX) * LOCK_RATIO) {
if (callback != null) {
callback.lockVerticalScroll(true);
}
xLock = true;
}
// detect scroll down, then aquire scroll lock
if (xLock && deltaY < 0.0f && callback != null) {
yLock = callback.aquireLock();
yLock = callback.aquireVerticalScrollLock();
}
// fall through
case MotionEvent.ACTION_DOWN:
// note start coordinates of the gesture
// note the current coordinates touch event
xPos = ev.getX();
yPos = ev.getY();
break;
@ -61,6 +66,9 @@ public class LockableConstraintLayout extends ConstraintLayout {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// remove locks on gesture end
if (callback != null) {
callback.lockVerticalScroll(false);
}
xLock = false;
break;
}
@ -89,8 +97,17 @@ public class LockableConstraintLayout extends ConstraintLayout {
public interface LockCallback {
/**
* aquire scroll lock for child views
*
* @return true to lock child scroll
*/
boolean aquireLock();
boolean aquireVerticalScrollLock();
/**
* called to lock/unlock vertical scrolling of the parent view
*
* @param lock true to lock vertical scrolling
*/
void lockVerticalScroll(boolean lock);
}
}

View File

@ -15,7 +15,7 @@ org.gradle.configureondemand=false;
org.gradle.jvmargs=-Xmx1024M -Dkotlin.daemon.jvm.options\="-Xmx1024M"
android.enableJetifier=true
android.useAndroidX=true
android.enableR8.fullMode=true
android.enableR8.fullMode=false
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false