【问题标题】:Android - ScrollView sluggingAndroid - ScrollView 重击
【发布时间】:2014-07-03 23:01:58
【问题描述】:

我有一个 ScrollView,它正在工作,我能够到达底部并返回顶部。 问题是当用户将手指从屏幕上移开时,滚动会立即停止。它应该更顺畅,不会累到底部。谁能帮帮我?

这是我的代码:

XML:

<ScrollView
    android:id="@+id/home_content_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/color_background_default"
    android:fadingEdgeLength="@dimen/app_fading_edge_length" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_background_default"
        android:orientation="vertical"
        android:padding="@dimen/app_content_padding" >

        <!-- Lots of different components here -->
    </LinearLayout>
</ScrollView>

我试着做 scrollView.setSmoothScrollingEnabled(true); 但这也没有用。

另外,我展示了一些在滚动视图的主要内容被渲染后异步获取的内容。其中一些是图像,可能会影响滚动的总高度......想想我这样做了

scrollView.refreshDrawableState(); scrollView.requestLayout();

这根本不起作用......

【问题讨论】:

标签: android scrollview smooth-scrolling


【解决方案1】:

今天,我遇到了类似的问题。在我的特殊情况下,我在 ScrollView 中有一个 RecyclerView。试验了一阵子,发现ScrollView并没有什么问题。是里面的 RecyclerView 负责的。所以我这样做了:

myRecyclerView.setNestedScrollingEnabled(false); /** THIS is the line that makes Recyclerview inside scrollview to scroll smoothly!*/

也许你应该看看其他元素。 Reference

【讨论】:

    【解决方案2】:

    如果您不喜欢 Android 在您移开手指以停止滚动时的处理方式,您可以像这样重载该逻辑:

    ScrollView myScrollView = (ScrollView) findViewById(R.id.my_scrollview_id);
    myScrollView.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (mVelocityTracker == null) {
                        mVelocityTracker = VelocityTracker.obtain();
                    } else {
                        mVelocityTracker.clear();
                    }
                    mVelocityTracker.addMovement(event);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mVelocityTracker.addMovement(event);
                    mVelocityTracker.computeCurrentVelocity(1000);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    ((ScrollView)v).smoothScrollBy(0, (int) (mVelocityTracker.getYVelocity()*myScalarMultiple));
                    return true;
                }
                return false;
            }
    
        });
    

    我们为 ACTION_DOWN 和 ACTION_MOVE 返回“false”,因为我们只想影响速度跟踪器,而不是消耗 ScrollView 的触摸事件。您还必须尝试使用​​ myScalarMultiple 的值才能获得您喜欢的结果。

    【讨论】:

    • 我不知道为什么,但它没有进入那个功能,所以我不知道这是否有效。如果我设法解决了原因,并且有效,我将接受您的回答。不过还是谢谢!
    • 请发布您的代码;我最终运行了这个源,它运行良好。
    【解决方案3】:

    将滚动视图包裹在布局中,它会顺利运行

    下面给出一个示例代码供参考

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ScrollView android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    
            <LinearLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      相关资源
      最近更新 更多