pull/981/head
gedoor 4 years ago
parent a22ade97d4
commit 03f9eb1538
  1. 23
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  2. 20
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 41
      app/src/main/java/io/legado/app/ui/widget/recycler/RecyclerViewAtViewPager2.kt

@ -5,6 +5,7 @@ import android.view.KeyEvent
import android.view.MenuItem import android.view.MenuItem
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView 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.ui.widget.dialog.TextDialog
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import java.lang.reflect.Field
class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(), class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
@ -46,7 +48,8 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
return ActivityMainBinding.inflate(layoutInflater) return ActivityMainBinding.inflate(layoutInflater)
} }
override fun onActivityCreated(savedInstanceState: Bundle?) = with(binding) { override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.apply {
ATH.applyEdgeEffectColor(viewPagerMain) ATH.applyEdgeEffectColor(viewPagerMain)
ATH.applyBottomNavigationColor(bottomNavigationView) ATH.applyBottomNavigationColor(bottomNavigationView)
viewPagerMain.offscreenPageLimit = 3 viewPagerMain.offscreenPageLimit = 3
@ -70,6 +73,8 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity) bottomNavigationView.setOnNavigationItemReselectedListener(this@MainActivity)
bottomNavigationView.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS bottomNavigationView.menu.findItem(R.id.menu_rss).isVisible = AppConfig.isShowRSS
} }
resetViewPager2TouchSlop()
}
override fun onPostCreate(savedInstanceState: Bundle?) { override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState) super.onPostCreate(savedInstanceState)
@ -85,6 +90,22 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
}, 3000) }, 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) { override fun onNavigationItemSelected(item: MenuItem): Boolean = with(binding) {
when (item.itemId) { when (item.itemId) {
R.id.menu_bookshelf -> viewPagerMain.setCurrentItem(0, false) R.id.menu_bookshelf -> viewPagerMain.setCurrentItem(0, false)

@ -10,7 +10,9 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import io.legado.app.R 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.ui.main.bookshelf.books.BooksFragment
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import java.lang.reflect.Field
/** /**
* 书架界面 * 书架界面
@ -63,6 +66,23 @@ class BookshelfFragment : VMBaseFragment<BookshelfViewModel>(R.layout.fragment_b
setSupportToolbar(binding.titleBar.toolbar) setSupportToolbar(binding.titleBar.toolbar)
initView() initView()
initBookGroupData() 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) { override fun onCompatCreateOptionsMenu(menu: Menu) {

@ -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)
}
}
Loading…
Cancel
Save