English 中文(简体)
Android: 自下而上的观点
原标题:Android: ScrollView force to bottom

我希望能从底部开始。 任何方法?

最佳回答

scroll.fullScroll (View.FOCUS_DOWN) also should work.

页: 1 员额(管理)

Kotlin 法典

scrollView.post {
   scrollView.fullScroll(View.FOCUS_DOWN)
}
问题回答

您应在滚动中操作该守则。 担任这一职务:

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

scroll.fullScroll (View.FOCUS_DOWN)将导致重点的改变。 如果有不止一个重点观点,例如两个EditText,那将产生一些奇怪的行为。 这个问题还有另一种方式。

    View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
    int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
    int sy = scrollLayout.getScrollY();
    int sh = scrollLayout.getHeight();
    int delta = bottom - (sy + sh);

    scrollLayout.smoothScrollBy(0, delta);

这很好。

http://www.ohchr.org。

fun ScrollView.scrollToBottom() {
    val lastChild = getChildAt(childCount - 1)
    val bottom = lastChild.bottom + paddingBottom
    val delta = bottom - (scrollY+ height)        
    smoothScrollBy(0, delta)
}

或者,如果你想进一步加强,以检查它是否在开车前已经处于底层,那么你可以做这样的事情:

    fun ScrollView.scrollToBottom() {
    val lastChild = children.lastOrNull() ?: return
    val bottom = lastChild.bottom + paddingBottom
    val currentY = height + scrollY
    val alreadyAtBottom = bottom <= currentY
    if (!alreadyAtBottom) {
        val delta = bottom - currentY
        smoothScrollBy(0, delta)
    } else {
        // already at bottom, do nothing
    }
}

有时是滚动 观点:职位上的工作

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

BUT 如果你使用roll 观点。 推迟,它将无限期工作

 scrollView.postDelayed(new Runnable() {
        @Override
        public void run() {
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
        }
    },1000);

这对我来说是最好的事

scroll_view.post(new Runnable() {
     @Override
     public void run() {
         // This method works but animates the scrolling 
         // which looks weird on first load
         // scroll_view.fullScroll(View.FOCUS_DOWN);

         // This method works even better because there are no animations.
         scroll_view.scrollTo(0, scroll_view.getBottom());
     }
});

我非常积极地工作。

    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();
    }

<><>>>>>

答案是真正老化的甲状腺的环绕。 今天,Delayed 。 更不用说,你应该使用。

i 成功。

scrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
        scrollView.smoothScrollTo(0, scrollView.getHeight());
    }
}, 1000);

这里还有其他一些向底层发展的方法。

fun ScrollView.scrollToBottom() {
    // use this for scroll immediately
    scrollTo(0, this.getChildAt(0).height) 

    // or this for smooth scroll
    //smoothScrollBy(0, this.getChildAt(0).height)

    // or this for **very** smooth scroll
    //ObjectAnimator.ofInt(this, "scrollY", this.getChildAt(0).height).setDuration(2000).start()
}

<>Using

您已经提出的评论

my_scroll_view.scrollToBottom()

如果您的简历没有完成(例如:你在活动领域向下演进:<条码>

my_scroll_view.post { 
   my_scroll_view.scrollToBottom()          
}

应考虑的是NOT要确定的内容。 某些儿童控制,特别是EditText控制,没有要求基金会的财产。 这可能是最后解释的布局财产之一,并且将压倒父母的重心环境(布局或雕刻)。

问题的答复并非完全如此,但一旦埃德蒂特提女士集中,我就必须立即缩减。 然而,所接受的回答将使ET也失去重点(根据我所假设的情况)。

我的工作是:

emailEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            Toast.makeText(getActivity(), "got the focus", Toast.LENGTH_LONG).show();
            scrollView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                }
            }, 200);
        }else {
            Toast.makeText(getActivity(), "lost the focus", Toast.LENGTH_LONG).show();
        }
    }
});

我实际上认为,要求全员发展是两倍:

myScrollView.fullScroll(View.FOCUS_DOWN);

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

在实施第一个(未成功)滚动后,它可能与启动(或)员额方法有关。 我认为,这种行为是在以往任何方法都要求我发号施令之后发生的,因此,你可以尝试用可能与你相关的任何其他东西取代第一种完整的施展方法。

采用另一种冷却方式,与科特林的cor体一道这样做。 利用一名反对一名有可操作(邮政/邮政)的残疾者的共同路线的好处是,它不会造成执行拖延行动的代价高昂的准备。

launch(UI){
    delay(300)
    scrollView.fullScroll(View.FOCUS_DOWN)
}

重要的是,要把共同路线的《手稿》具体化,否则,可能不会把拖延的行动从“情报分析”的线索上来。

在这些案例中,仅使用scroll.scrollTo(0, sc.getBottom(),不使用t work,使用scroll.post

例:

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

为什么<代码>scroll.fullScroll(View.FOCUS_DOWN)甚至不能在<代码>上包装(<>post(>)是,该观点没有阐明。 在该案中,View.do OnLayout()可作为更好的选择:

scroll.doOnLayout(){
    scroll.fullScroll(View.FOCUS_DOWN)
}

或,为勇敢的灵魂作了更详尽的阐述:

在我看来,所有答案都是trick切的:

Extension Function PostDelayed

private fun ScrollView.postDelayed(
    time: Long = 325, // ms
    block: ScrollView.() -> Unit
) {
    postDelayed({block()}, time)
}

Extension Function measureScrollHeight

fun ScrollView.measureScrollHeight(): Int {
    val lastChild = getChildAt(childCount - 1)
    val bottom = lastChild.bottom + paddingBottom
    val delta = bottom - (scrollY+ height)
    return delta
}

Extension Function ScrolltoBottom

fun ScrollView.scrollToBottom() {
    postDelayed {
        smoothScrollBy(0, measureScrollHeight())
    }
}

知道最低拖延时间至少应为325万ms,而滚动将是ug(不是向整个底层发展)。 目前的高度与底线之间的距离越大,越多,时间就越迟。

这里的一些人说, that 观点。

如果你不希望使用滚动 观点认为,另一种选择是使用听众。 我在另一个使用案件中这样做了:

ViewTreeObserver.OnPreDrawListener viewVisibilityChanged = new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        if (my_view.getVisibility() == View.VISIBLE) {
            scroll_view.smoothScrollTo(0, scroll_view.getHeight());
        }
        return true;
    }
};

您可以提出以下看法:

my_view.getViewTreeObserver().addOnPreDrawListener(viewVisibilityChanged);

如果你的最低SDK是29或29,你可以这样做:

View childView = findViewById(R.id.your_view_id_in_the_scroll_view)
if(childView != null){
  scrollview.post(() -> scrollview.scrollToDescendant(childView));
}

http://strong>stantly。 毫不拖延地。

// wait for the scroll view to be laid out
scrollView.post(new Runnable() {
  public void run() {
    // then wait for the child of the scroll view (normally a LinearLayout) to be laid out
    scrollView.getChildAt(0).post(new Runnable() {
      public void run() {
        // finally scroll without animation
        scrollView.scrollTo(0, scrollView.getBottom());
      }
    }
  }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签