mirror of
https://gitlab.shinice.net/pixeldroid/PixelDroid
synced 2024-12-19 11:43:36 +01:00
Merge branch 'swipe_sensitivity' into 'master'
Make it a bit harder to swipe between tabs Closes #285 See merge request pixeldroid/PixelDroid!341
This commit is contained in:
commit
c3ba4f41fd
@ -11,10 +11,13 @@ import android.widget.ImageView
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.view.GravityCompat
|
import androidx.core.view.GravityCompat
|
||||||
|
import androidx.core.view.NestedScrollingChild
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.paging.ExperimentalPagingApi
|
import androidx.paging.ExperimentalPagingApi
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||||
|
import androidx.viewpager2.widget.ViewPager2
|
||||||
import com.bumptech.glide.Glide
|
import com.bumptech.glide.Glide
|
||||||
import com.google.android.material.tabs.TabLayoutMediator
|
import com.google.android.material.tabs.TabLayoutMediator
|
||||||
import org.pixeldroid.app.databinding.ActivityMainBinding
|
import org.pixeldroid.app.databinding.ActivityMainBinding
|
||||||
@ -40,6 +43,7 @@ import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
|
|||||||
import com.mikepenz.materialdrawer.util.DrawerImageLoader
|
import com.mikepenz.materialdrawer.util.DrawerImageLoader
|
||||||
import com.mikepenz.materialdrawer.widget.AccountHeaderView
|
import com.mikepenz.materialdrawer.widget.AccountHeaderView
|
||||||
import org.ligi.tracedroid.sending.sendTraceDroidStackTracesIfExist
|
import org.ligi.tracedroid.sending.sendTraceDroidStackTracesIfExist
|
||||||
|
import org.pixeldroid.app.posts.NestedScrollableHost
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@ -268,8 +272,22 @@ class MainActivity : BaseActivity() {
|
|||||||
header.setActiveProfile(account.toLong())
|
header.setActiveProfile(account.toLong())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use reflection to make it a bit harder to swipe between tabs
|
||||||
|
*/
|
||||||
|
private fun ViewPager2.reduceDragSensitivity() {
|
||||||
|
val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView")
|
||||||
|
recyclerViewField.isAccessible = true
|
||||||
|
val recyclerView = recyclerViewField.get(this) as RecyclerView
|
||||||
|
|
||||||
|
val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop")
|
||||||
|
touchSlopField.isAccessible = true
|
||||||
|
val touchSlop = touchSlopField.get(recyclerView) as Int
|
||||||
|
touchSlopField.set(recyclerView, touchSlop*NestedScrollableHost.touchSlopModifier)
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupTabs(tab_array: List<() -> Fragment>){
|
private fun setupTabs(tab_array: List<() -> Fragment>){
|
||||||
|
binding.viewPager.reduceDragSensitivity()
|
||||||
binding.viewPager.adapter = object : FragmentStateAdapter(this) {
|
binding.viewPager.adapter = object : FragmentStateAdapter(this) {
|
||||||
override fun createFragment(position: Int): Fragment {
|
override fun createFragment(position: Int): Fragment {
|
||||||
return tab_array[position]()
|
return tab_array[position]()
|
||||||
|
@ -94,11 +94,12 @@ class NestedScrollableHost : ConstraintLayout {
|
|||||||
val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
|
val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
|
||||||
|
|
||||||
// assuming ViewPager2 touch-slop is 2x touch-slop of child
|
// assuming ViewPager2 touch-slop is 2x touch-slop of child
|
||||||
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f
|
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f/ touchSlopModifier else 1f
|
||||||
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
|
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f/touchSlopModifier
|
||||||
|
|
||||||
|
if(dx.absoluteValue * .5f > touchSlop || scaledDy > touchSlop) doubleTapCallback?.invoke(false)
|
||||||
|
|
||||||
if (scaledDx > touchSlop || scaledDy > touchSlop) {
|
if (scaledDx > touchSlop || scaledDy > touchSlop) {
|
||||||
doubleTapCallback?.invoke(false)
|
|
||||||
|
|
||||||
if (isVpHorizontal == (scaledDy > scaledDx)) {
|
if (isVpHorizontal == (scaledDy > scaledDx)) {
|
||||||
// Gesture is perpendicular, allow all parents to intercept
|
// Gesture is perpendicular, allow all parents to intercept
|
||||||
@ -116,4 +117,8 @@ class NestedScrollableHost : ConstraintLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val touchSlopModifier = 2
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user