我的问题是我在 a 中有 2 个不同的列表ViewPager2
,它们的大小不同。而且我不希望能够在小家伙下方滚动。
NestedScrollView (Activity)
|
|
|
FrameLayout
|
|
|
LinearLayout (Fragment)
|
|
|
ViewPager2
|
|
|
RecyclerView
_______________________________________________
| ViewPager2 |
_______________________________________________
| | |
| | |
| | |
| Page 1 | Page 2 |
| | |
| | |
| |_______________________
| |
| |
| |
| | Space I don't want to be able to see / scroll
| |
| |
| |
| |
________________________
我有这个UnScrollView
可以禁用滚动的地方,但问题是当我设置scrollable
为假时。它不进去onTouchEvent
,但它仍然可以滚动。
class UnScrollView @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyle: Int = 0) : NestedScrollView(context, attributeSet, defStyle) {
var scrollable = true
fun isScrollable() = scrollable
override fun performClick() = super.performClick()
override fun onTouchEvent(ev: MotionEvent): Boolean {
return scrollable && super.onTouchEvent(ev)
}
override fun onInterceptTouchEvent(ev: MotionEvent)
= scrollable && super.onInterceptTouchEvent(ev)
}
这是检测我们何时可以滚动的代码
(requireActivity() as MainActivity).binding
.scroll
.setOnScrollChangeListener { v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int ->
println("####################################################################################")
println("ScrollY : ${v?.cast<NestedScrollView>()?.scrollY}")
println("ViewPager H: ${binding.viewPager.height}")
binding.viewPager.children.first().cast<RecyclerView>()
?.children
?.mapNotNull { it.findViewById<RecyclerView>(R.id.list) }
?.elementAt(binding.viewPager.currentItem)
?.let {
val manager = (it.layoutManager as GridLayoutManager)
val tmp = (it.adapter?.itemCount ?: 0) / manager.spanCount
val minTmp = it.children.first().height.times(tmp)
val displayMetrics = DisplayMetrics()
requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
println("Row: $tmp")
println("HeightRow: ${it.children.first().height}")
println("Height: ${it.children.first().height.times(tmp)}")
println("MinTMP: $minTmp")
println("ScrollY: ${v?.cast<NestedScrollView>()?.scrollY}")
println("Display HEIGHT: ${requireActivity().px2dp(displayMetrics.heightPixels.toFloat())}")
println("Display + ScrollY: ${v?.cast<NestedScrollView>()?.scrollY?.plus(requireActivity().px2dp(displayMetrics.heightPixels.toFloat()))}")
if ((v?.cast<NestedScrollView>()?.scrollY?.plus(requireActivity().px2dp(displayMetrics.heightPixels.toFloat())) ?: 0f) > minTmp) {
println("blocked")
v?.cast<UnScrollView>()?.scrollable = false
} else {
println("not blocked")
v?.cast<UnScrollView>()?.scrollable = true
}
}
}
并且所有屏幕都必须滚动,我在上方有 aTabLayout
和 a ,所以滚动需要在页面之外TextView
ViewPager2
ViewPager2