From 03f9eb15385da327d813cd667264fe28fdb01fb8 Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 12 May 2021 15:02:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/main/MainActivity.kt | 63 ++++++++++++------- .../ui/main/bookshelf/BookshelfFragment.kt | 20 ++++++ .../recycler/RecyclerViewAtViewPager2.kt | 41 ------------ 3 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/ui/widget/recycler/RecyclerViewAtViewPager2.kt diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index b7d448502..b3abb11e3 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -5,6 +5,7 @@ import android.view.KeyEvent import android.view.MenuItem import androidx.activity.viewModels import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.bottomnavigation.BottomNavigationView @@ -30,6 +31,7 @@ import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.observeEvent import io.legado.app.utils.toastOnUi +import java.lang.reflect.Field class MainActivity : VMBaseActivity(), @@ -46,29 +48,32 @@ class MainActivity : VMBaseActivity(), return ActivityMainBinding.inflate(layoutInflater) } - override fun onActivityCreated(savedInstanceState: Bundle?) = with(binding) { - ATH.applyEdgeEffectColor(viewPagerMain) - ATH.applyBottomNavigationColor(bottomNavigationView) - viewPagerMain.offscreenPageLimit = 3 - viewPagerMain.adapter = TabFragmentPageAdapter() - viewPagerMain.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { - override fun onPageSelected(position: Int) { - pagePosition = position - when (position) { - 0, 1, 3 -> bottomNavigationView.menu.getItem(position).isChecked = true - 2 -> if (AppConfig.isShowRSS) { - bottomNavigationView.menu.getItem(position).isChecked = true - } else { - bottomNavigationView.menu.getItem(3).isChecked = true + override fun onActivityCreated(savedInstanceState: Bundle?) { + binding.apply { + ATH.applyEdgeEffectColor(viewPagerMain) + ATH.applyBottomNavigationColor(bottomNavigationView) + viewPagerMain.offscreenPageLimit = 3 + viewPagerMain.adapter = TabFragmentPageAdapter() + viewPagerMain.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + pagePosition = position + when (position) { + 0, 1, 3 -> bottomNavigationView.menu.getItem(position).isChecked = true + 2 -> if (AppConfig.isShowRSS) { + bottomNavigationView.menu.getItem(position).isChecked = true + } else { + bottomNavigationView.menu.getItem(3).isChecked = true + } } } - } - }) - bottomNavigationView.elevation = - if (AppConfig.elevation < 0) elevation else AppConfig.elevation.toFloat() - bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity) - bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity) - bottomNavigationView.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS + }) + bottomNavigationView.elevation = + if (AppConfig.elevation < 0) elevation else AppConfig.elevation.toFloat() + bottomNavigationView.setOnNavigationItemSelectedListener(this@MainActivity) + bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity) + bottomNavigationView.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS + } + resetViewPager2TouchSlop() } override fun onPostCreate(savedInstanceState: Bundle?) { @@ -85,6 +90,22 @@ class MainActivity : VMBaseActivity(), }, 3000) } + /** + * 重新设置viewpager2的滑动灵敏度 + */ + private fun resetViewPager2TouchSlop() = kotlin.runCatching { + val recyclerViewField: Field = ViewPager2::class.java.getDeclaredField("mRecyclerView") + recyclerViewField.isAccessible = true + + val recyclerView = recyclerViewField.get(binding.viewPagerMain) as RecyclerView + + val touchSlopField: Field = RecyclerView::class.java.getDeclaredField("mTouchSlop") + touchSlopField.isAccessible = true + + val touchSlop = touchSlopField.get(recyclerView) as Int + touchSlopField.set(recyclerView, touchSlop * 4) + } + override fun onNavigationItemSelected(item: MenuItem): Boolean = with(binding) { when (item.itemId) { R.id.menu_bookshelf -> viewPagerMain.setCurrentItem(0, false) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 7a7113054..390de88b0 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -10,7 +10,9 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.LiveData +import androidx.recyclerview.widget.RecyclerView import androidx.viewpager2.adapter.FragmentStateAdapter +import androidx.viewpager2.widget.ViewPager2 import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import io.legado.app.R @@ -36,6 +38,7 @@ import io.legado.app.ui.main.MainViewModel import io.legado.app.ui.main.bookshelf.books.BooksFragment import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import java.lang.reflect.Field /** * 书架界面 @@ -63,6 +66,23 @@ class BookshelfFragment : VMBaseFragment(R.layout.fragment_b setSupportToolbar(binding.titleBar.toolbar) initView() initBookGroupData() + resetViewPager2TouchSlop() + } + + /** + * 重新设置viewpager2的滑动灵敏度 + */ + private fun resetViewPager2TouchSlop() = kotlin.runCatching { + val recyclerViewField: Field = ViewPager2::class.java.getDeclaredField("mRecyclerView") + recyclerViewField.isAccessible = true + + val recyclerView = recyclerViewField.get(binding.viewPagerBookshelf) as RecyclerView + + val touchSlopField: Field = RecyclerView::class.java.getDeclaredField("mTouchSlop") + touchSlopField.isAccessible = true + + val touchSlop = touchSlopField.get(recyclerView) as Int + touchSlopField.set(recyclerView, touchSlop * 4) } override fun onCompatCreateOptionsMenu(menu: Menu) { diff --git a/app/src/main/java/io/legado/app/ui/widget/recycler/RecyclerViewAtViewPager2.kt b/app/src/main/java/io/legado/app/ui/widget/recycler/RecyclerViewAtViewPager2.kt deleted file mode 100644 index 79132f627..000000000 --- a/app/src/main/java/io/legado/app/ui/widget/recycler/RecyclerViewAtViewPager2.kt +++ /dev/null @@ -1,41 +0,0 @@ -package io.legado.app.ui.widget.recycler - -import android.content.Context -import android.util.AttributeSet -import android.view.MotionEvent -import androidx.recyclerview.widget.RecyclerView -import kotlin.math.abs - -class RecyclerViewAtViewPager2(context: Context, attrs: AttributeSet?) : - RecyclerView(context, attrs) { - - private var startX: Int = 0 - private var startY: Int = 0 - - override fun dispatchTouchEvent(ev: MotionEvent): Boolean { - when (ev.action) { - MotionEvent.ACTION_DOWN -> { - startX = ev.x.toInt() - startY = ev.y.toInt() - parent.requestDisallowInterceptTouchEvent(true) - } - MotionEvent.ACTION_MOVE -> { - val endX = ev.x.toInt() - val endY = ev.y.toInt() - val disX = abs(endX - startX) - val disY = abs(endY - startY) - if (disX > disY) { - parent.requestDisallowInterceptTouchEvent(canScrollHorizontally(startX - endX)) - } else { - parent.requestDisallowInterceptTouchEvent(canScrollVertically(startY - endY)) - } - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> parent.requestDisallowInterceptTouchEvent( - false - ) - } - return super.dispatchTouchEvent(ev) - } - - -} \ No newline at end of file