14

多くのコンテンツを含むスクロールビューがあります。ユーザーがフリングまたはスクロールダウンを行うと、アニメーションを実行している特定のビューの場所でスクロールビューを停止し、ユーザーは再びフリングまたはスクロールダウンできるようになります。

ここで述べたようにスクロールビューを無効にしようとしましたが、スクロールビューが完全に停止し、フリングの途中で停止できない場合にのみ無効になります。

特定のビュー位置または特定の y 値に達したときに、スクロールビューのフリングを停止する方法はありますか?

4

6 に答える 6

14

私は私の解決策と同じ問題を抱えていました。

listView.smoothScrollBy(0,0)

これにより、スクロールが停止します。

于 2016-07-07T06:23:03.153 に答える
8

特定のポイントでフリングを停止するには、単純に呼び出します

fling(0)

flings のみに関心がある場合、これが最も論理的な方法であると私は考えていvelosityYます。

fling メソッドの javadoc は次のとおりです。

/**
 * Fling the scroll view
 *
 * @param velocityY The initial velocity in the Y direction. Positive
 *                  numbers mean that the finger/cursor is moving down the screen,
 *                  which means we want to scroll towards the top.
 */
于 2017-12-21T19:34:37.977 に答える
4

1 つの方法は、既存のスクロール モーションを停止するSmoothScrollToPositionを使用することです。このメソッドには API レベル >= 8 (Android 2.2、Froyo) が必要であることに注意してください。

現在の位置が目的の位置から遠く離れている場合、スムーズなスクロールにはかなりの時間がかかり、少しぎこちなく見えることに注意してください (少なくとも Android 4.4 KitKat でのテストでは)。また、setSelection と SmoothScrollToPosition の呼び出しを組み合わせて使用​​すると、位置がわずかに「ミス」することがあることがわかりました。これは、現在の位置が目的の位置に非常に近い場合にのみ発生するようです。

私の場合、ユーザーがボタンを押したときにリストが一番上(位置 = 0) にジャンプするようにしました (これはユースケースとは少し異なるため、ニーズに合わせて調整する必要があります)。

私は次の方法を使用しました

private void smartScrollToPosition(ListView listView, int desiredPosition) {
    // If we are far away from the desired position, jump closer and then smooth scroll
    // Note: we implement this ourselves because smoothScrollToPositionFromTop
    // requires API 11, and it is slow and janky if the scroll distance is large,
    // and smoothScrollToPosition takes too long if the scroll distance is large.
    // Jumping close and scrolling the remaining distance gives a good compromise.
    int currentPosition = listView.getFirstVisiblePosition();
    int maxScrollDistance = 10;
    if (currentPosition - desiredPosition >= maxScrollDistance) {
        listView.setSelection(desiredPosition + maxScrollDistance);
    } else if (desiredPosition - currentPosition >= maxScrollDistance) {
        listView.setSelection(desiredPosition - maxScrollDistance);
    }
    listView.smoothScrollToPosition(desiredPosition); // requires API 8
}

ボタンのアクションハンドラーで、次のようにこれを呼び出しました

    case R.id.action_go_to_today:
        ListView listView = (ListView) findViewById(R.id.lessonsListView);
        smartScrollToPosition(listView, 0); // scroll to top
        return true;

上記はあなたの質問に直接答えるものではありませんが、現在の位置が目的の位置またはその近くにあることを検出できる場合は、SmoothScrollToPositionを使用してスクロールを停止できます。

于 2014-03-15T15:13:01.087 に答える
1

ScrollViewフリング操作の処理を無効にする必要があります。これを行うには、flingメソッド inScrollViewと commentを単純にオーバーライドしますsuper.fling()。これがうまくいくかどうか教えてください!

public class CustomScrollView extends ScrollView {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
    return false;
    }

    @Override
    public void fling (int velocityY)
    {
    /*Scroll view is no longer gonna handle scroll velocity.
    * super.fling(velocityY);
     */
    }
 }
于 2013-06-26T05:48:23.957 に答える
0

実際にこの方法を使用しています:

 list.post(new Runnable() 
                        {
                            @Override
                            public void run() 
                            {
                               list.smoothScrollToPositionFromTop(arg2, 150); 
                            }
                        });
于 2014-04-23T13:50:40.003 に答える