将 SearchView 滚动到顶部 - searchView 位于片段的中间 [英] Scroll SearchView to the top - searchView is in the middle of fragment
问题描述
我在片段中间有一个 searchView.当我点击它时,它会展开一个键盘,其中包含列表(在 searchView 下)和用于编写查询的位置(searchView).当点击搜索图标时,是否有可能以某种方式滚动到整个布局的顶部?目前,我将 searchView 上方的所有视图设置为 myViews.visibility = View.GONE
并且我收到了正确的行为,但我不确定这是否正确 - 特别是因为上面的 searchView 我有一张地图.你有什么看法?
I have a searchView in the middle of fragment. When I tap on it, it expand a keyboard which cover list (which is under the searchView) and place for writing queries (searchView). Is there any possibility to somehow scroll to the top whole layout when tap on search icon? Currently I set all the views above searchView to myViews.visibility = View.GONE
and I've received proper behaviour but I am not sure if this is correct - especially since above searchView I have a map. What's your opinion?
问候
推荐答案
假设 searchComponent
是根布局的直接子级.您可以简单地在平移 y 上使用 objectAnimator
将其移动到屏幕顶部.
Assuming the searchComponent
is a direct child of the root layout. You can simply use objectAnimator
over translation y to move it to the top of the screen.
var originalY = searchComponent.y // its the starting position of searchComponent which can be used to bring it back to the initial position
val animator = ObjectAnimator.ofFloat(searchComponent, "translationY", originalY, 0)
animator.duration = 500
animator.start()
searchComponent
- 由 recyclerView
和 searchView
本身组成
searchComponent
- is composed of the recyclerView
and the searchView
itself
0
是 y 的最终位置
这篇关于将 SearchView 滚动到顶部 - searchView 位于片段的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!