简体   繁体   English

如何将视图设置为不使用Java中的scrollview滚动

[英]How to set a view to NOT scroll with scrollview in Java

is it possible to programmically force a View inside a ScrollView NOT to scroll? 是否有可能以编程方式强制ScrollView内的视图不滚动?

Like the alphabetic titles in the contacts list. 就像联系人列表中的字母标题一样。 The 'A' bar doesn't scroll when the user scrolls through the list of all contacts whose first names start with 'A'. 当用户滚动名字以“A”开头的所有联系人列表时,“A”栏不会滚动。 When the user scrolls till the end of 'A', 'B' bar simply scrolls up from the bottom of ScrollView, finally reaplacing 'A' at the top and staying there. 当用户滚动到'A'的末尾时,'B'栏只是从ScrollView的底部向上滚动,最后在顶部重新打开“A”并停留在那里。

My description is kind of abstract, hope you understand. 我的描述有点抽象,希望你理解。

Also thanks in advance. 还要提前感谢。

The contacts list uses the ListView and not the ScrollView. 联系人列表使用ListView而不是ScrollView。 One way I think it can be done is to have a fixed header at the top. 我认为可以做到的一种方法是在顶部有一个固定的标题。 You can change its content depending on what is being displayed on the screen. 您可以根据屏幕上显示的内容更改其内容。

Attach a OnScrollListener to ListView. OnScrollListener附加到ListView。 You will get the view with the first visible position in onScroll. 您将获得onScroll中第一个可见位置的视图。 Then change the content of the header according to the content in the view. 然后根据视图中的内容更改标题的内容。

You don't need to add a header in your ListView for the first section in your list as it will be already handled in your ScrollListener. 您不需要在ListView中为列表中的第一部分添加标题,因为它已在ScrollListener中处理。 Keep the style for the header rows and the view at the top the same and you will have the illusion for the fixed header. 保持标题行的样式和顶部的视图相同,您将获得固定标题的错觉。

As mentioned earlier you need a ListView. 如前所述,您需要一个ListView。 For an effect similar to that in the People app in Android 4+ I have used this excellent library (free and open source). 对于类似于Android 4+中的People应用程序的效果,我使用了这个优秀的 (免费和开源)。

I think you need a combination of the "static text" approach with "section headers" within the ScrollView: the list should contain disabled/non-clickable items with the letters "A", "B", etc. When a section header scrolls above the top of the view, change the contents of the static TextView above the ScrollView to the contents of the header. 我认为你需要在ScrollView中结合使用“静态文本”方法和“节标题”:列表应包含带有字母“A”,“B”等的禁用/不可点击的项目。当节标题滚动时在视图顶部上方,将ScrollView上方的静态TextView的内容更改为标题的内容。 Ie, when the "B" header item scrolls out of view, the TextView now contains "B". 即,当“B”标题项滚动出视图时,TextView现在包含“B”。

Of course, the tough part will be detecting when a "section header" scrolls off the top, or comes into view when scrolling in the opposite direction. 当然,困难的部分将检测何时“节标题”从顶部滚动,或在向相反方向滚动时进入视图。

For bonus points, use a "push up" Animation in the TextView when it's contents change to make it look like the list item is rolling into the TextView. 对于奖励积分,当TextView的内容发生变化时,在TextView中使用“向上推”动画,使其看起来像列表项滚动到TextView中。 (There is an example of this Animation in the API Demos app in the SDK in "...\\view\\Animation2.java"). (在“... \\ view \\ Animation2.java”中SDK中的API演示应用程序中有此动画的示例)。

UPDATE: after further review... here is some classes, etc. related towards making this work (it was a fun exercise for me!): 更新:经过进一步的审查......这里有一些课程等与制作这项工作相关(对我来说这是一个有趣的练习!):

android.widget.AbsListView.OnScrollListener interface to see how to detect scrolling events. android.widget.AbsListView.OnScrollListener接口,了解如何检测滚动事件。 You'll need to keep track of the previous "top of view" position to determine which direction you are scrolling (to pick the slide-up or -down animation). 您需要跟踪之前的“视图顶部”位置,以确定您正在滚动的方向(选择上滑或下移动画)。

The Animation I listed above is not really what you want -- better off to look at the android.widget.TextSwitcher class and the push*.xml files in the SDK/android-X/data/res/anim directory. 我上面列出的动画并不是你想要的 - 最好看看SDK / android-X / data / res / anim目录下的android.widget.TextSwitcher类和push*.xml文件。

You'll certainly need your own adapter subclass to inject section views into the list: getView(...) can set the row view's properties (bkgnd, text style, etc.) differently if the row is a section header or not. 您肯定需要自己的适配器子类将节视图注入列表:如果行是节标题,则getView(...)可以设置行视图的属性(bkgnd,文本样式等)。 Re-use this code in your TextSwitcher view factory to populate the static TextView/Switcher above the list. 在TextSwitcher视图工厂中重复使用此代码,以填充列表上方的静态TextView / Switcher。

Have fun...! 玩得开心...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM