Skip to content

Commit

Permalink
Merge pull request #2 from phantomVK/feature/remove_custom_viewdraghe…
Browse files Browse the repository at this point in the history
…lper

Replace custom ViewDragHelper with androidx's.
  • Loading branch information
phantomVK authored Dec 28, 2023
2 parents 3ce3a3a + b797d41 commit 998d6b5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1,566 deletions.
24 changes: 24 additions & 0 deletions app/src/main/java/com/phantomvk/slideback/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import static androidx.customview.widget.ViewDragHelper.EDGE_RIGHT;
import static androidx.customview.widget.ViewDragHelper.EDGE_TOP;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.Toast;
Expand All @@ -24,10 +26,12 @@
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.customview.widget.ViewDragHelper;

import com.phantomvk.slideback.SlideLayout;
import com.phantomvk.slideback.demo.union.BaseActivity;

import java.lang.reflect.Field;
import java.util.regex.Pattern;

public class MainActivity extends BaseActivity {
Expand Down Expand Up @@ -88,6 +92,26 @@ protected void onDestroy() {
Log.i(tag, "onDestroy()");
}

public void setTouchSlop(Context context, float sensitivity) throws NoSuchFieldException, IllegalAccessException {
SlideLayout layout = (slideManager == null) ? null : slideManager.getSlideLayout();
if (layout == null) {
return;
}

ViewDragHelper helper = layout.getViewDragHelper();
if (helper == null) {
return;
}

float s = Math.max(0f, Math.min(1.0f, sensitivity));
ViewConfiguration conf = ViewConfiguration.get(context);
int touchSlop = (int) (conf.getScaledTouchSlop() * (1 / s));

Field field = ViewDragHelper.class.getDeclaredField("mTouchSlop");
field.setAccessible(true);
field.setInt(helper, touchSlop);
}

private void setWindow() {
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ buildscript {
// in the individual module build.gradle files
}
rootProject.ext {
versionCode = 901
versionName = "0.9.1"
versionCode = 902
versionName = "0.9.2"
}
}

Expand Down
1 change: 1 addition & 0 deletions slideback/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.customview:customview:1.1.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
Expand Down
22 changes: 5 additions & 17 deletions slideback/src/main/java/com/phantomvk/slideback/SlideLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.view.ViewCompat;
import androidx.customview.widget.ViewDragHelper;

import com.phantomvk.slideback.listener.SlideStateListener;
import com.phantomvk.slideback.utility.TranslucentHelper;
import com.phantomvk.slideback.utility.ViewDragHelper;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -280,17 +280,6 @@ public void setThreshold(final float threshold) {
mThreshold = threshold;
}

/**
* Set the sensitivity.
*
* @param context Context to get ViewConfiguration
* @param sensitivity value between 0 to 1, the final value for touchSlop =
* ViewConfiguration.getScaledTouchSlop * (1 / s);
*/
public void setSensitivity(Context context, float sensitivity) {
mHelper.setSensitivity(context, sensitivity);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!getFlag(FLAG_SLIDE_ENABLE)) {
Expand Down Expand Up @@ -584,6 +573,10 @@ public void convertFromTranslucent() {
}
}

public ViewDragHelper getViewDragHelper() {
return mHelper;
}

/**
* Activities cannot draw during the period that their windows are animating in. In order
* to know when it is safe to begin drawing they can override this method which will be
Expand Down Expand Up @@ -743,11 +736,6 @@ public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) {
}
return position;
}

@Override
public boolean isValidMoveAction() {
return isFlagsEnabled();
}
}

private boolean getFlag(int flag) {
Expand Down
Loading

0 comments on commit 998d6b5

Please sign in to comment.