プログラムでスクロール ビューを特定の編集テキストまでスクロールする方法はありますか?
-
27-10-2019 - |
質問
スクロールビューで非常に長いアクティビティを行っています。これは、ユーザーが入力する必要があるさまざまなフィールドを含むフォームです。フォームの中ほどにチェックボックスがあり、ユーザーがチェックすると、ビューの特定の部分までスクロールしたいと考えています。プログラムで EditText オブジェクト (またはその他のビュー オブジェクト) にスクロールする方法はありますか?
また、X 座標と Y 座標を使用するとこれが可能であることはわかっていますが、フォームはユーザーごとに異なる可能性があるため、これを避けたいと考えています。
解決
private final void focusOnView(){
your_scrollview.post(new Runnable() {
@Override
public void run() {
your_scrollview.scrollTo(0, your_EditBox.getBottom());
}
});
}
他のヒント
Sherif elKhatib の答えは、必要に応じて大幅に改善できます。 ビューをスクロールビューの中心までスクロールします. 。この再利用可能なメソッドは、水平スクロールビューの表示中心までビューをスムーズにスクロールします。
private final void focusOnView(final HorizontalScrollView scroll, final View view) {
new Handler().post(new Runnable() {
@Override
public void run() {
int vLeft = view.getLeft();
int vRight = view.getRight();
int sWidth = scroll.getWidth();
scroll.smoothScrollTo(((vLeft + vRight - sWidth) / 2), 0);
}
});
}
縦型の場合 ScrollView
変更 x
そして y
の入力の位置 smoothScroll
. 。そして使用してください view.getTop()
の代わりに view.getLeft()
そして view.getBottom()
の代わりに v.getRight()
.
:)
これは私にとってうまくいきます:
targetView.getParent().requestChildFocus(targetView,targetView);
public void requestChildFocus(子を見る、焦点を合わせて表示)
子 - 集中を望んでいるこのビューパレントの子。このビューには、フォーカスビューが含まれます。実際に焦点を当てているのは必ずしもそうではありません。
焦点 - 実際に焦点を当てている子供の子孫であるビュー
私の意見では、特定の長方形にスクロールする最良の方法は View.requestRectangleOnScreen(Rect, Boolean)
. 。あなたはそれをaで呼ぶべきです View
画面に表示されるローカル長方形にスクロールして渡す必要があります。 2番目のパラメーターは次のとおりです false
滑らかなスクロール用と true
即時スクロール用。
final Rect rect = new Rect(0, 0, view.getWidth(), view.getHeight());
view.requestRectangleOnScreen(rect, false);
Warrenfaithからの回答に基づいて小さなユーティリティ方法を作成しました。このコードは、Scrollviewでそのビューがすでに表示されている場合、スクロールは必要ありません。
public static void scrollToView(final ScrollView scrollView, final View view) {
// View needs a focus
view.requestFocus();
// Determine if scroll needs to happen
final Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (!view.getLocalVisibleRect(scrollBounds)) {
new Handler().post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, view.getBottom());
}
});
}
}
あなたはあなたを作るべきです TextView
フォーカスをリクエスト:
mTextView.requestFocus();
私の編集は、ScrollView内にいくつかのレイヤーがネストされており、それ自体がレイアウトのルートビューではありません。 getTop()とgetBottom()は、その内部の座標内の座標を報告しているように見えたため、ScrollViewの上部からEdittextの上部から編集の両親を繰り返すことで編集の上部まで計算しました。
// Scroll the view so that the touched editText is near the top of the scroll view
new Thread(new Runnable()
{
@Override
public
void run ()
{
// Make it feel like a two step process
Utils.sleep(333);
// Determine where to set the scroll-to to by measuring the distance from the top of the scroll view
// to the control to focus on by summing the "top" position of each view in the hierarchy.
int yDistanceToControlsView = 0;
View parentView = (View) m_editTextControl.getParent();
while (true)
{
if (parentView.equals(scrollView))
{
break;
}
yDistanceToControlsView += parentView.getTop();
parentView = (View) parentView.getParent();
}
// Compute the final position value for the top and bottom of the control in the scroll view.
final int topInScrollView = yDistanceToControlsView + m_editTextControl.getTop();
final int bottomInScrollView = yDistanceToControlsView + m_editTextControl.getBottom();
// Post the scroll action to happen on the scrollView with the UI thread.
scrollView.post(new Runnable()
{
@Override
public void run()
{
int height =m_editTextControl.getHeight();
scrollView.smoothScrollTo(0, ((topInScrollView + bottomInScrollView) / 2) - height);
m_editTextControl.requestFocus();
}
});
}
}).start();
別のバリションは次のとおりです。
scrollView.postDelayed(new Runnable()
{
@Override
public void run()
{
scrollView.smoothScrollTo(0, img_transparent.getTop());
}
}, 2000);
または、を使用できます post()
方法。
これはより良い答えには遅すぎるかもしれませんが、望ましい完璧なソリューションはポジショナーのようなシステムでなければなりません。つまり、システムがエディターフィールドのポジショニングを行うと、フィールドをキーボードまで配置します。
以下のコードが作成するのは、Android Wayの配置をスムーズに配置することです。まず、現在のスクロールポイントを基準点として保持します。第二に、エディターに最適なポジショニングスクロールポイントを見つけることです。これを行うには、上にスクロールしてから、エディターフィールドにscrollviewコンポーネントを作成して最高のポジショニングを行うように要求します。ガッチャ!私たちは最高のポジションを学びました。さて、私たちがすることは、前のポイントから新しく見つけたポイントまでスムーズにスクロールすることです。必要に応じて、使用してスムーズなスクロールを省略できます スクロールする それ以外の smoothscrollto それだけ。
ノート: メインコンテナScrollViewは、ScrollViewSignupという名前のメンバーフィールドです。これは、私の例がサインアップ画面だったためです。
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(final View view, boolean b) {
if (b) {
scrollViewSignup.post(new Runnable() {
@Override
public void run() {
int scrollY = scrollViewSignup.getScrollY();
scrollViewSignup.scrollTo(0, 0);
final Rect rect = new Rect(0, 0, view.getWidth(), view.getHeight());
view.requestRectangleOnScreen(rect, true);
int new_scrollY = scrollViewSignup.getScrollY();
scrollViewSignup.scrollTo(0, scrollY);
scrollViewSignup.smoothScrollTo(0, new_scrollY);
}
});
}
}
});
このブロックをすべてに使用したい場合 edittext インスタンス、そしてそれを画面コードとすばやく統合します。以下のようにトラバーサを作るだけです。これを行うために、私はメインのonfocuschangelistenerに名前のメンバーフィールドにしました FocusChangelistenertOsCrolleditor, 、そして、以下のようにオンクリート中にそれを呼び出します。
traverseEditTextChildren(scrollViewSignup, focusChangeListenerToScrollEditor);
メソッドの実装は以下のとおりです。
private void traverseEditTextChildren(ViewGroup viewGroup, View.OnFocusChangeListener focusChangeListenerToScrollEditor) {
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof EditText)
{
((EditText) view).setOnFocusChangeListener(focusChangeListenerToScrollEditor);
}
else if (view instanceof ViewGroup)
{
traverseEditTextChildren((ViewGroup) view, focusChangeListenerToScrollEditor);
}
}
}
それで、私たちがここでやったことは、すべてのEdittextインスタンスの子供たちにフォーカスのリスナーに電話をかけるようにすることです。
このソリューションに到達するために、ここですべてのソリューションをチェックアウトし、より良いUI/UX結果のための新しいソリューションを生成しました。
Many thanks to all other answers inspiring me much.
参照 : https://stackoverflow.com/a/6438240/2624806
以下ははるかにうまく機能しました。
mObservableScrollView.post(new Runnable() {
public void run() {
mObservableScrollView.fullScroll([View_FOCUS][1]);
}
});
ScrollviewがChildViewの直接の親である場合、上記の回答は正常に機能します。 ScrollViewの別のViewGroupにChildViewが包まれている場合、View.getTop()が親に対して位置を取得するため、予期しない動作を引き起こします。そのような場合、これを実装する必要があります。
public static void scrollToInvalidInputView(ScrollView scrollView, View view) {
int vTop = view.getTop();
while (!(view.getParent() instanceof ScrollView)) {
view = (View) view.getParent();
vTop += view.getTop();
}
final int scrollPosition = vTop;
new Handler().post(() -> scrollView.smoothScrollTo(0, scrollPosition));
}
よりエレガントでエラーが発生しやすいソリューションを使用していると感じたと思います
数学は関係ありません。他の提案されたソリューションに反して、上下の両方で正しくスクロールすることを処理します。
/**
* Will scroll the {@code scrollView} to make {@code viewToScroll} visible
*
* @param scrollView parent of {@code scrollableContent}
* @param scrollableContent a child of {@code scrollView} whitch holds the scrollable content (fills the viewport).
* @param viewToScroll a child of {@code scrollableContent} to whitch will scroll the the {@code scrollView}
*/
void scrollToView(ScrollView scrollView, ViewGroup scrollableContent, View viewToScroll) {
Rect viewToScrollRect = new Rect(); //coordinates to scroll to
viewToScroll.getHitRect(viewToScrollRect); //fills viewToScrollRect with coordinates of viewToScroll relative to its parent (LinearLayout)
scrollView.requestChildRectangleOnScreen(scrollableContent, viewToScrollRect, false); //ScrollView will make sure, the given viewToScrollRect is visible
}
それを包むのは良い考えです postDelayed
その場合に備えて、それをより信頼できるようにするために ScrollView
現時点で変更されています
/**
* Will scroll the {@code scrollView} to make {@code viewToScroll} visible
*
* @param scrollView parent of {@code scrollableContent}
* @param scrollableContent a child of {@code scrollView} whitch holds the scrollable content (fills the viewport).
* @param viewToScroll a child of {@code scrollableContent} to whitch will scroll the the {@code scrollView}
*/
private void scrollToView(final ScrollView scrollView, final ViewGroup scrollableContent, final View viewToScroll) {
long delay = 100; //delay to let finish with possible modifications to ScrollView
scrollView.postDelayed(new Runnable() {
public void run() {
Rect viewToScrollRect = new Rect(); //coordinates to scroll to
viewToScroll.getHitRect(viewToScrollRect); //fills viewToScrollRect with coordinates of viewToScroll relative to its parent (LinearLayout)
scrollView.requestChildRectangleOnScreen(scrollableContent, viewToScrollRect, false); //ScrollView will make sure, the given viewToScrollRect is visible
}
}, delay);
}
私の解決策は次のとおりです。
int[] spinnerLocation = {0,0};
spinner.getLocationOnScreen(spinnerLocation);
int[] scrollLocation = {0, 0};
scrollView.getLocationInWindow(scrollLocation);
int y = scrollView.getScrollY();
scrollView.smoothScrollTo(0, y + spinnerLocation[1] - scrollLocation[1]);
Androidソースコードを調べると、既にメンバー関数があることがわかります ScrollView
– scrollToChild(View)
- それはまさに要求されていることをします。不幸なことに、この関数はいくつかのあいまいな理由のためにマークされています private
. 。その関数に基づいて、最初の機能を見つけた次の関数を書きました ScrollView
の上に View
パラメーターとして指定され、それをスクロールして、 ScrollView
:
private void make_visible(View view)
{
int vt = view.getTop();
int vb = view.getBottom();
View v = view;
for(;;)
{
ViewParent vp = v.getParent();
if(vp == null || !(vp instanceof ViewGroup))
break;
ViewGroup parent = (ViewGroup)vp;
if(parent instanceof ScrollView)
{
ScrollView sv = (ScrollView)parent;
// Code based on ScrollView.computeScrollDeltaToGetChildRectOnScreen(Rect rect) (Android v5.1.1):
int height = sv.getHeight();
int screenTop = sv.getScrollY();
int screenBottom = screenTop + height;
int fadingEdge = sv.getVerticalFadingEdgeLength();
// leave room for top fading edge as long as rect isn't at very top
if(vt > 0)
screenTop += fadingEdge;
// leave room for bottom fading edge as long as rect isn't at very bottom
if(vb < sv.getChildAt(0).getHeight())
screenBottom -= fadingEdge;
int scrollYDelta = 0;
if(vb > screenBottom && vt > screenTop)
{
// need to move down to get it in view: move down just enough so
// that the entire rectangle is in view (or at least the first
// screen size chunk).
if(vb-vt > height) // just enough to get screen size chunk on
scrollYDelta += (vt - screenTop);
else // get entire rect at bottom of screen
scrollYDelta += (vb - screenBottom);
// make sure we aren't scrolling beyond the end of our content
int bottom = sv.getChildAt(0).getBottom();
int distanceToBottom = bottom - screenBottom;
scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
}
else if(vt < screenTop && vb < screenBottom)
{
// need to move up to get it in view: move up just enough so that
// entire rectangle is in view (or at least the first screen
// size chunk of it).
if(vb-vt > height) // screen size chunk
scrollYDelta -= (screenBottom - vb);
else // entire rect at top
scrollYDelta -= (screenTop - vt);
// make sure we aren't scrolling any further than the top our content
scrollYDelta = Math.max(scrollYDelta, -sv.getScrollY());
}
sv.smoothScrollBy(0, scrollYDelta);
break;
}
// Transform coordinates to parent:
int dy = parent.getTop()-parent.getScrollY();
vt += dy;
vb += dy;
v = parent;
}
}
私の場合、そうではありません EditText
, 、そうです googleMap
. 。そして、それはこのようにうまく機能します。
private final void focusCenterOnView(final ScrollView scroll, final View view) {
new Handler().post(new Runnable() {
@Override
public void run() {
int centreX=(int) (view.getX() + view.getWidth() / 2);
int centreY= (int) (view.getY() + view.getHeight() / 2);
scrollView.smoothScrollBy(centreX, centreY);
}
});
}
QUE:スクロールビューを特定のEdittextにプログラム的にスクロールする方法はありますか?
ANS:RecyclerViewのネストされたスクロールビューの最後の位置に記録データが追加されました。
adapter.notifyDataSetChanged();
nested_scroll.setScrollY(more Detail Recycler.getBottom());
以下は私が使用しているものです:
int amountToScroll = viewToShow.getBottom() - scrollView.getHeight() + ((LinearLayout.LayoutParams) viewToShow.getLayoutParams()).bottomMargin;
// Check to see if scrolling is necessary to show the view
if (amountToScroll > 0){
scrollView.smoothScrollTo(0, amountToScroll);
}
これにより、ビューの底部を表示するために必要なスクロール量が得られます。
垂直スクロール、フォームに適しています。回答は、アフマダリバロックの水平スクロールに基づいています。
private final void focusOnView(final HorizontalScrollView scroll, final View view) {
new Handler().post(new Runnable() {
@Override
public void run() {
int top = view.getTop();
int bottom = view.getBottom();
int sHeight = scroll.getHeight();
scroll.smoothScrollTo(0, ((top + bottom - sHeight) / 2));
}
});
}
使用できます ObjectAnimator
このような:
ObjectAnimator.ofInt(yourScrollView, "scrollY", yourView.getTop()).setDuration(1500).start();
scrlmainがnestedscrollviewの場合は、次のものを使用してください。
scrlMain.post(new Runnable() {
@Override
public void run() {
scrlMain.fullScroll(View.FOCUS_UP);
}
});