MainActivity: increase velocity threshold for viewPager

main
Alibek Omarov 5 years ago
parent 15ff7caa3a
commit 216f0d3e2b
  1. 4
      app/src/main/java/com/keylesspalace/tusky/MainActivity.java
  2. 40
      app/src/main/java/com/keylesspalace/tusky/util/ViewPager2Fix.java
  3. 34
      app/src/test/java/com/keylesspalace/tusky/util/ViewPager2FixTest.java

@ -57,6 +57,7 @@ import com.keylesspalace.tusky.pager.MainPagerAdapter;
import com.keylesspalace.tusky.util.CustomEmojiHelper;
import com.keylesspalace.tusky.util.NotificationHelper;
import com.keylesspalace.tusky.util.ShareShortcutHelper;
import com.keylesspalace.tusky.util.ViewPager2Fix;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
@ -196,6 +197,7 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
composeButton = findViewById(R.id.floating_btn);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.pager);
ViewPager2Fix.reduceVelocity(viewPager, 2.0f);
composeButton.setOnClickListener(v -> {
Intent composeIntent = new Intent(getApplicationContext(), ComposeActivity.class);
@ -660,4 +662,4 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut
public AndroidInjector<Object> androidInjector() {
return androidInjector;
}
}
}

@ -0,0 +1,40 @@
package com.keylesspalace.tusky.util;
import androidx.viewpager2.widget.ViewPager2;
import androidx.recyclerview.widget.RecyclerView;
import java.lang.reflect.*;
import java.lang.*;
/**
* ViewPager2 written by monkeys!
*/
public class ViewPager2Fix {
/**
* Thanks to @al.e.shevelev@medium.com for solution
*/
public static Field getViewPagerRecyclerViewField() throws NoSuchFieldException {
Field f = ViewPager2.class.getDeclaredField("mRecyclerView");
f.setAccessible(true);
return f;
}
public static Field getRecyclerViewTouchSlopField() throws NoSuchFieldException {
Field f = RecyclerView.class.getDeclaredField("mTouchSlop");
f.setAccessible(true);
return f;
}
public static void reduceVelocity(ViewPager2 pager, float val) {
try {
Field recyclerViewField = getViewPagerRecyclerViewField();
Field touchSlopField = getRecyclerViewTouchSlopField();
RecyclerView recyclerView = (RecyclerView)recyclerViewField.get(pager);
int touchSlop = (int)touchSlopField.get(recyclerView);
touchSlopField.setInt(recyclerView, (int)(touchSlop*val));
} catch(Exception e) {
// all possible exceptions must be caught during tests
;
}
}
}

@ -0,0 +1,34 @@
package com.keylesspalace.tusky.util;
import org.junit.Before;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.viewpager2.widget.ViewPager2;
import androidx.recyclerview.widget.RecyclerView;
import org.junit.runners.Parameterized;
import java.lang.reflect.*;
import java.lang.*;
import com.keylesspalace.tusky.util.ViewPager2Fix;
public class ViewPager2FixTest {
@Test
public void getViewPagerRecyclerViewFieldTest() {
try {
Field f = ViewPager2Fix.getViewPagerRecyclerViewField();
} catch(Exception e) {
Assert.fail("asdf");
}
}
@Test
public void getRecyclerViewTouchSlopFieldTest() {
try {
Field f = ViewPager2Fix.getRecyclerViewTouchSlopField();
} catch(Exception e) {
Assert.fail("asdf");
}
}
}
Loading…
Cancel
Save