How to set view to NOT scroll using scrollview in Java

Is it possible to programmatically make the View inside ScrollView NOT scroll?

Like alphabetical names in your contact list. The bar "A" does not scroll when the user scrolls the list of all contacts whose first names begin with "A". When the user scrolls to the end of β€œA”, the β€œB” panel simply scrolls from the bottom of the ScrollView, finally pushing β€œA” up again and staying there.

My description is abstract, hope you understand.

Also thanks in advance.

+4
source share
3 answers

The contact list uses a ListView, not a ScrollView. One way, I think, can be done to have a fixed header at the top. You can change its contents depending on what is displayed on the screen.

Attach OnScrollListener to ListView. You will get a view with the first visible position in onScroll. Then change the contents of the header to match the content in the view.

You do not need to add a title to your ListView for the first section in your list, as it will already be processed in your ScrollListener. Keep the style for the title bars, and the view at the top is the same, and you will have the illusion of a fixed title.

+3
source

It seems to me that you need a combination of the "static text" approach with the "section headings" in ScrollView: the list should contain elements with disabled / inaccurate code with the letters "A", "B", etc. When the section title scrolls over the top of the view, change the contents of the static TextView above ScrollView to the contents of the title. Ie, when the title element β€œB” scrolls out of view, the TextView now contains β€œB”.

Of course, the hard part will determine when the "section header" scrolls from the top or appears when scrolling in the opposite direction.

For bonus points, use the "push up" animation in the TextView when changing content so that it looks like a list item rolls in the TextView. (An example of this animation in the Demos API application in the SDK is shown in "... \ view \ Animation2.java").

UPDATE: after further consideration ... here are some classes, etc. related to doing this work (it was a fun exercise for me!):

android.widget.AbsListView.OnScrollListener to learn how to define scroll events. You will need to follow the previous β€œtop” position to determine which direction you are scrolling (to select a slide animation or down).

The animation above is not really what you want - it's better to look at the android.widget.TextSwitcher class and push*.xml in the SDK / android-X / data / res / anim directory.

You will definitely need your own adapter subclass to insert sections into the list: getView(...) can distinguish between the properties of the string type (bkgnd, text style, etc.) otherwise if the string is the section title or not. Reuse this code in the TextSwitcher factory view to populate the static TextView / Switcher above the list.

Good luck ...!

+2
source

As mentioned earlier, you need a ListView. For an effect similar to the effect of the People app in Android 4+, I used this excellent library (free and open source).

+2
source

Source: https://habr.com/ru/post/1343724/


All Articles