我在屏幕上工作,其中有 scrollview
,其中包含 4 个 view
,并且我有四个选项卡代表该部分。最初 view1 tab
在我滚动 view2
时处于 Activity 状态 visible
那时 view2 tab
应该处于 Activity 状态。让我知道如果有人有解决方案。
最佳答案
首先应该将 ScrollViewListener
添加到您的 ScrollView 中
yourscrollview.setScrollViewListener(this)//replace yourscrollview with your scrollview and override onScrollChanged
然后为所有四个 View
创建变量
现在是主要部分。在这里我们得到所有四个的高度。虽然滚动 position
和 views heights
我们检查当前是哪个 View
可见
在屏幕上然后您可以激活
选项卡。
现在在onScrollChanged
方法中
override fun onScrollChanged(scrollView: ScrollViewExt?, x: Int, y: Int, oldx: Int, oldy: Int) {
val scrollBounds = Rect()
scrollView?.getDrawingRect(scrollBounds)
var top4 = 0f
var temp4:View = view4
while (temp4 !is ScrollView){
top4 += (temp4).y
temp4 = temp4.parent as View
}
var top3 = 0f
var temp3:View = view3
while (temp3 !is ScrollView){
top3 += (temp3).y
temp3 = temp3.parent as View
}
var top2 = 0f
var temp2:View = view2
while (temp2 !is ScrollView){
top2 += (temp2).y
temp2 = temp2.parent as View
}
var top1 = 0f
var temp1:View = view1
while (temp1 !is ScrollView){
top1 += (temp1).y
temp1 = temp1.parent as View
}
//this are the height of four views
view1_height = top1 + view1.height
view2_height = top2 + view2.height
view3_height = top3 + view3.height
view4_height = top4 + view4.height
//now check which view is visible . we go in descending order
if (scrollBounds.bottom + view4.height >= view4_height {
//4th view is visible
//active your 4th tab
} else if (scrollBounds.bottom + view3.height >= view3_height {
//3th view is visible
//active your 3rd tab
} else if (scrollBounds.bottom + view2.height >= view2_height) {
//2th view is visible
//active your 2th tab
} else if ( scrollBounds.bottom + view1.height >= view1_height) {
//1th view is visible
//active your 1st tab
}
}
我希望这对你有用。
关于android - 如何在 android kotlin 中更改 onScrollChanged 上的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387961/