Seamless nested scrolling (Android)

前端 未结 8 1417
不知归路
不知归路 2020-12-15 01:46

We\'ve all been advised against nesting views that contain a scrolling mechanism. However, in the latest Android release (5.0), the Phone app caught my attention with what s

相关标签:
8条回答
  • 2020-12-15 01:57

    This is classic example of dummy layouts. Something not entirely obvious at first look. Basically the scenario is something like this.

    Grey Area->FrameLayout Followed by a listview that fills up the entire framelayout and followed by a imageview that overlaps the top half of a listview. The listview's first item is a dummy item and has a height identical to that of the imageview. (Note: The actual data starts from the second element)

    Next Step is easy Translate the Imageview as per the scroll of the listview.

    I suppose this is the best way to do that whilst avoiding nested scrolling

    0 讨论(0)
  • 2020-12-15 02:01

    Add latest support package 'com.android.support:support-v4:22.1.1' to your project. And try this:

    <android.support.v4.widget.NestedScrollView
            android:id="@+id/nScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    
            <FrameLayout ...>
               <ListView ... />
            </FrameLayout >
        </android.support.v4.widget.NestedScrollView>
    

    By default nested scrolling is Enabled.

    0 讨论(0)
  • 2020-12-15 02:05

    While trying to figure out how to solve this issue myself, I found this question first; however, the answer didn't really go into too much detail. I did find a lot of good resources, so if anyone else finds themselves looking for this, I'll link them below. A term for this effect is "Sticky Scrolling".


    An article talking about "Synchronized Scrolling".

    http://www.pushing-pixels.org/2011/07/18/android-tips-and-tricks-synchronized-scrolling.html


    A good video showcasing some Android scrolling tricks, "Quick Return" and "Sticky Scrolling".

    https://www.youtube.com/watch?v=PL9s0IJ9oiI

    Code: https://code.google.com/p/romannurik-code/source/browse/misc/scrolltricks


    And lastly, here is another one showcasing the same effect using a listView instead of a ScrollView.

    https://www.youtube.com/watch?v=rk-tLisxSgM

    Code: https://github.com/jatago/list_sticky_scroll_trick

    0 讨论(0)
  • 2020-12-15 02:08

    Android 5.0 Lollipop (API 21) added nested scrolling support.

    From what I can tell, both ListView (AbsListView) and ScrollView support this now (if running on API 21), but it must be enabled on the scrolling views.

    There are two ways, by calling setNestedScrollingEnabled(true) or with the layout attribute android:nestedScrollingEnabled="true" (which is undocumented)

    To learn about how it works, or to support this for a custom widget, the key methods are these:

    • onStartNestedScroll

    • onNestedScrollAccepted

    • onNestedPreScroll

    • onNestedScroll

    • onStopNestedScroll

    Unfortunately, there is no guide or training which explains how this works other than the JavaDoc itself which is rather light and there are no examples other than ScrollView.

    0 讨论(0)
  • 2020-12-15 02:15

    I am using something like this and it works ok I think

    scrollView.onScroll { x, y ->
                Timber.d("ScrollView offset: ($x, $y)")
    
                val height = dashboardChart.measuredHeight
                val recyclerView =  viewPager.findViewById<RecyclerView>(R.id.recyclerView)
                if(y >= height) {
                    Timber.d("ScrollView enable nested scrolling!")
                    recyclerView.isNestedScrollingEnabled = true
                } else {
                    Timber.d("ScrollView disable nested scrolling!")
                    recyclerView.isNestedScrollingEnabled = false
                }
            }
    

    Where scrollView is parent I am listening onScroll event (it is extension underneath it is viewTreeObserver.addOnScrollListener). Then depending whether I've scrolled initial offset or not I am enabling/disabling child recyclerView (similary ListView or other scrollView) scrolling.

    0 讨论(0)
  • 2020-12-15 02:16

    I found an alternative 'trick' which is quite simple... Use only a ListView with an added transparent header.

    0 讨论(0)
提交回复
热议问题