155

ScrollViewプログラムで特定の位置にスクロールする方法はありますか?

TableLayoutに配置されるダイナミックを作成しましたScrollView。したがって、特定のアクション(ボタンのクリックなど)で、特定の行が自動的に一番上の位置にスクロールするようにします。

出来ますか?

4

18 に答える 18

210

Pragnaからの回答は常に機能するとは限りません。これを試してください:

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.scrollTo(0, mScrollView.getBottom());
        } 
});

また

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.fullScroll(mScrollView.FOCUS_DOWN);
        } 
});

スクロールして開始する場合

mScrollView.post(new Runnable() { 
        public void run() { 
             mScrollView.fullScroll(mScrollView.FOCUS_UP);
        } 
});
于 2011-12-30T22:26:27.573 に答える
173
ScrollView sv = (ScrollView)findViewById(R.id.scrl);
sv.scrollTo(0, sv.getBottom());

また

sv.scrollTo(5, 10);

于 2011-06-22T10:16:31.453 に答える
35

私はscrollViewがonCreateView()の直後にスクロールするようにしたかった(例えばボタンクリックの後ではない)。それを機能させるには、ViewTreeObserverを使用する必要がありました。

mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });

ただし、これは何かがレイアウトされるたびに呼び出されることに注意してください(たとえば、ビューを非表示または同様に設定した場合)。したがって、必要がなくなった場合は、このリスナーを削除することを忘れないでください。

public void removeGlobalOnLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)SDKレベル<16

また

public void removeOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)SDKレベルで>=16

于 2013-05-15T09:43:32.397 に答える
29

ここには良い答えがたくさんありますが、1つだけ追加したいと思います。上または下に完全にスクロールするのではなく、ScrollViewをレイアウトの特定のビューにスクロールしたい場合があります。

簡単な例:登録フォームで、フォームの編集テキストが入力されていないときにユーザーが[サインアップ]ボタンをタップした場合、その特定の編集テキストまでスクロールして、そのフィールドに入力する必要があることをユーザーに通知します。

その場合、次のようなことができます。

scrollView.post(new Runnable() { 
        public void run() { 
             scrollView.scrollTo(0, editText.getBottom());
        } 
});

または、インスタントスクロールの代わりにスムーズなスクロールが必要な場合:

scrollView.post(new Runnable() { 
            public void run() { 
                 scrollView.smoothScrollTo(0, editText.getBottom());
            } 
    });

もちろん、テキストの編集の代わりに任意のタイプのビューを使用できます。getBottom()は、親レイアウトに基づいてビューの座標を返すため、ScrollView内で使用されるすべてのビューには、親のみが必要であることに注意してください(たとえば、線形レイアウト)。

ScrollViewの子の中に複数の親がある場合、私が見つけた唯一の解決策は、親ビューでrequestChildFocusを呼び出すことです。

editText.getParent().requestChildFocus(editText, editText);

ただし、この場合、スムーズなスクロールはできません。

この答えが同じ問題を抱えている人に役立つことを願っています。

于 2016-09-15T15:26:08.000 に答える
19

次のようなものを使用します。

mScrollView.scrollBy(10, 10);

また

mScrollView.scrollTo(10, 10);
于 2011-06-22T10:07:11.337 に答える
9

scrollToメソッドを使用してみてください詳細情報

于 2011-06-22T10:04:48.373 に答える
9

すぐにスクロールしたい場合は、次を使用できます:

ScrollView scroll= (ScrollView)findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());

            OR

scroll.fullScroll(View.FOCUS_DOWN);

            OR

scroll.post(new Runnable() {            
    @Override
    public void run() {
           scroll.fullScroll(View.FOCUS_DOWN);              
    }
});

または、これを使用できるようにスムーズかつゆっくりとスクロールしたい場合:

private void sendScroll(){
        final Handler handler = new Handler();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Thread.sleep(100);} catch (InterruptedException e) {}
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        }).start();
    }
于 2014-12-11T06:38:56.090 に答える
5

**希望の高さまでスクロールします。私はいくつかの良い解決策を考え出しました**

                scrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollBy(0, childView.getHeight());
                    }
                }, 100);
于 2016-09-01T12:44:29.993 に答える
5

はい、できます。

あなたが1つLayoutを持っていて、その中に、あなたは多くを持っているとしましょうViews。したがって、Viewプログラムでスクロールする場合は、次のコードスニペットを作成する必要があります。

例えば:

content_main.xml

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txtView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>

MainActivity.java

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
Button btn = (Button) findViewById(R.id.ivEventBanner);
TextView txtView = (TextView) findViewById(R.id.ivEditBannerImage);

特定の場所にスクロールしたい場合は、たとえばtxtviewViewとしましょう。この場合は、次のように記述します。

scrollView.smoothScrollTo(txtView.getScrollX(),txtView.getScrollY());

そして、これで完了です。

于 2017-10-16T09:40:21.767 に答える
4

ScrollViewの一番下までスクロールするようにこれを機能させました(内部にTextViewがあります):

(これをTextViewを更新するメソッドに配置します)

final ScrollView myScrollView = (ScrollView) findViewById(R.id.myScroller);
    myScrollView.post(new Runnable() {
    public void run() {
        myScrollView.fullScroll(View.FOCUS_DOWN);
    }
});
于 2015-05-07T18:12:10.637 に答える
3

注:すでにスレッドに参加している場合は、新しい投稿スレッドを作成する必要があります。そうしないと、(私にとっては)完全に終了するまで新しい長い高さでスクロールされません。例:

void LogMe(final String s){
    runOnUiThread(new Runnable() {
        public void run() {
            connectionLog.setText(connectionLog.getText() + "\n" + s);
            final ScrollView sv = (ScrollView)connectLayout.findViewById(R.id.scrollView);
            sv.post(new Runnable() {
                public void run() {
                    sv.fullScroll(sv.FOCUS_DOWN);
                    /*
                    sv.scrollTo(0,sv.getBottom());
                    sv.scrollBy(0,sv.getHeight());*/
                }
            });
        }
    });
}
于 2014-06-15T08:51:06.870 に答える
3

座標を含まない別の回答を追加します。

これにより、目的のビューにフォーカスが移動します(ただし、一番上の位置には移動しません)。

  yourView.getParent().requestChildFocus(yourView,yourView);

public void RequestChildFocus(子を表示、フォーカスを表示)

child-フォーカスが必要なこのViewParentの子このビューには、フォーカスされたビューが含まれます。実際に焦点が当てられているのは必ずしもビューではありません。

フォーカス-実際にフォーカスを持っている子の子孫であるビュー

于 2015-08-01T17:49:39.960 に答える
3

ページスクロールだけ:

ScrollView sv = (ScrollView) findViewById(your_scroll_view);
sv.pageScroll(View.FOCUS_DOWN);
于 2017-04-13T17:03:50.487 に答える
2

誰もがそのような複雑な答えを投稿しています。

一番下までスクロールすると、簡単な答えが見つかりました。

final ScrollView myScroller = (ScrollView) findViewById(R.id.myScrollerView);

// Scroll views can only have 1 child, so get the first child's bottom,
// which should be the full size of the whole content inside the ScrollView
myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );

また、必要に応じて、上記の2行目のコードを実行可能コードに入れることができます。

myScroller.post( new Runnable() {
    @Override
    public void run() {
        myScroller.smoothScrollTo( 0, myScroller.getChildAt( 0 ).getBottom() );
    }
}

この単純な解決策を見つけるには、多くの調査と遊びが必要でした。お役に立てば幸いです!:)

于 2015-12-13T20:51:22.620 に答える
1

Runnableをsv.fullScroll(View.FOCUS_DOWN);で使用していました。それは当面の問題に対して完全に機能しますが、そのメソッドはScrollViewに画面全体からフォーカスを取得させます。そのAutoScrollを毎回発生させると、EditTextはユーザーから情報を受け取ることができなくなり、私の解決策は別のコードを使用しましたランナブルの下:

sv.scrollTo(0、sv.getBottom()+ sv.getScrollY());

重要な見解に焦点を失うことなく同じことをする

あいさつ。

于 2015-02-11T05:13:10.247 に答える
1

それは私のために働いています

mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });
于 2018-08-25T06:39:43.660 に答える
0
private int totalHeight = 0;

ViewTreeObserver ScrollTr = loutMain.getViewTreeObserver();
ScrollTr.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            loutMain.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            loutMain.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        TotalHeight = loutMain.getMeasuredHeight();

    }
});

scrollMain.smoothScrollTo(0, totalHeight);
于 2019-05-28T12:25:12.713 に答える
0
I had to create Interface

public interface ScrollViewListener {
    void onScrollChanged(ScrollViewExt scrollView, 
                         int x, int y, int oldx, int oldy);
}    

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class CustomScrollView extends ScrollView {
    private ScrollViewListener scrollViewListener = null;
    public ScrollViewExt(Context context) {
        super(context);
    }

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

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

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}




<"Your Package name ".CustomScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:scrollbars="vertical">

    private CustomScrollView scrollView;

scrollView = (CustomScrollView)mView.findViewById(R.id.scrollView);
        scrollView.setScrollViewListener(this);


    @Override
    public void onScrollChanged(ScrollViewExt scrollView, int x, int y, int oldx, int oldy) {
        // We take the last son in the scrollview
        View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
        int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

        // if diff is zero, then the bottom has been reached
        if (diff == 0) {
                // do stuff
            //TODO keshav gers
            pausePlayer();
            videoFullScreenPlayer.setVisibility(View.GONE);

        }
    }
于 2019-07-04T11:01:48.513 に答える