Android-ScrollView插入

问题描述 投票:0回答:3

我有一个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的主要内容之后异步获取的一些内容。其中一些是图像,可能会影响滚动的总高度...考虑到我做了

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

根本没用...

android scrollview smooth-scrolling
3个回答
1
投票

今天,我遇到了类似的问题。在我的特殊情况下,我在ScrollView中有一个RecyclerView。经过一段时间的试验,我发现ScrollView没有任何问题。这是RecyclerView内部负责的。所以我做到了:

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

也许您应该检查其他元素。Reference


0
投票

如果您不喜欢在松开手指以停止滚动时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的值来获得所需的结果。


0
投票

将scrollview包装在布局内,它将可以正常工作

下面提供示例代码供参考

<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>
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.