簡體   English   中英

Android - 滾動停止后如何滾動到 ScrollView 的開頭或結尾?

[英]Android - How to Scroll to Begining or end of ScrollView after Scrolling Stopped?

我對 Java 編程比較陌生,目前在 Android Studio 中遇到了我的 ScrollView 問題。 我希望 scrollView 在用戶停止滾動后滾動到視圖的開始或結尾,具體取決於滾動停止的位置。 我一直在嘗試將 setOnScrollChangeListener() 與 setOnTouchListener() 結合使用來檢測滾動何時停止。 這不起作用,因為一旦啟動觸摸,滾動將不起作用。

我應該如何解決這個問題? 或者我應該使用其他一些視圖來代替哪種視圖在我的情況下更可取?

我在這里找到了一個類似問題的舊答案: Android: Detect when ScrollView通過 Aleksandarf 停止滾動,其中正在使用一個類。 但我不明白如何或何時打電話給班級。

public class ScrollViewWithOnStopListener extends ScrollView {

    OnScrollStopListener listener;

    public interface OnScrollStopListener {
        void onScrollStopped(int y);
    }

    public ScrollViewWithOnStopListener(Context context) {
        super(context);
    }

    public ScrollViewWithOnStopListener(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                checkIfScrollStopped();
        }

        return super.onTouchEvent(ev);
    }

    int initialY = 0;

    private void checkIfScrollStopped() {
        initialY = getScrollY();
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                int updatedY = getScrollY();
                if (updatedY == initialY) {
                    //we've stopped
                    if (listener != null) {
                        listener.onScrollStopped(getScrollY());
                    }
                } else {
                    initialY = updatedY;
                    checkIfScrollStopped();
                }
            }
        }, 50);
    }

    public void setOnScrollStoppedListener(OnScrollStopListener yListener) {
        listener = yListener;
    }
} 

提前致謝!

嗨,我使用您在此處提供的鏈接和另一個問題( Android:如何在頂部滾動 ScrollView

創建一個名為CustomScrollView的類:

public class CustomScrollView extends ScrollView {
private long lastScrollUpdate = -1;

public CustomScrollView(Context context) { super(context);}
public CustomScrollView(Context context, AttributeSet attrs) { super(context,attrs);}
public CustomScrollView (Context context, AttributeSet attrs, int default_style){super(context, attrs, default_style);}

private class ScrollStateHandler implements Runnable {

    @Override
    public void run() {
        long currentTime = System.currentTimeMillis();
        if ((currentTime - lastScrollUpdate) > 100) {
            lastScrollUpdate = -1;
            onScrollEnd();
        } else {
            postDelayed(this, 100);
        }
    }
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (lastScrollUpdate == -1) {
        onScrollStart();
        postDelayed(new ScrollStateHandler(), 100);
    }

    lastScrollUpdate = System.currentTimeMillis();
}

private void onScrollStart() {
    // Feel Free to add something here
}

private void onScrollEnd() {
    this.fullScroll(View.FOCUS_UP); // Each time user finish with scrolling it will scroll to top
}}

在你的 xml 添加:

<YOUR-PACKAGE-NAME.CustomScrollView
android:id="@+id/scrollMe"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="tEST\nSCROLL DOWN\n\n\n\nHello World!\n test \n test \nSCROLL\n"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</YOUR-PACKAGE-NAME.CustomScrollView>

隨意問任何事情:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM