我正在构建一个类似聊天的应用程序,使用滚动视图显示用户输入到屏幕的文本.我正在做的是自动滚动滚动视图,因为更多文本被附加到屏幕上.我正在使用
ScrollView my_scrollview = (ScrollView) findViewById(R.id.scroller);
my_scrollview.fullScroll(ScrollView.FOCUS_DOWN);
Run Code Online (Sandbox Code Playgroud)
这似乎有用,虽然由于某种原因,因为键盘通常在屏幕上聊天,当滚动视图向下滚动它并不完全 - 添加的最新文本视图不显示(你必须手动向下滚动到最新一).我该如何解决这个问题?
当我放大滚动视图内容的大小时,滚动视图需要一段时间才能"知道"它的孩子的大小变化.如何让ScrollView立即检查它的孩子?
我在ScrollView中的LinearLayout中有一个ImageView.
在我的ScaleListener.onScale中,我更改了LinearLayout的大小.然后我尝试在scrollview上订购滚动条.在ScaleListener.onScale中:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
params.width = (int) (startX * scaleFactor);
params.height = (int) (startY * scaleFactor);
imageView.setLayoutParams(params);
(...)
scrollView.scrollBy(scrollX, scrollY);
Run Code Online (Sandbox Code Playgroud)
但是,在无法进行缩放滚动之前的情况下不会发生滚动,因为视图太小而无法滚动.在setLayoutParams之后,视图应该更大,但不会发生滚动,因为scrollview认为孩子仍然很小.
当一个fes ms之后再次调用onScroll时,它确实滚动得很好,它以某种方式发现子项更大且可滚动.
如何立即通知滚动视图,孩子的大小已经改变?那么scrollBy会在它的孩子的setLayoutParams之后正常工作吗?
如何在Android中以编程方式将ScrollView滚动到底部?
建议的代码
logScroll.scrollTo(0, logScroll.getBottom());
Run Code Online (Sandbox Code Playgroud)
不起作用(滚动到开头的底部,而不是实际的底部).
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.inthemoon.trylocationlistener.MainActivity">
<ScrollView
android:id="@+id/log_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/log_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
填充代码如下:
@BindView(R.id.log_scroll)
ScrollView logScroll;
@BindView(R.id.log_text)
TextView logText;
private void log(String msg) {
logText.append(new SimpleDateFormat( "HH:mm:ss ", Locale.US ).format(new Date()) + msg + "\n");
logScroll.scrollTo(0, logScroll.getBottom());
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我读了一些答案并写道:
private void log(String msg) {
logText.append(new SimpleDateFormat( "HH:mm:ss ", Locale.US ).format(new Date()) + msg + "\n");
//logScroll.scrollTo(0, logScroll.getBottom());
logScroll.fullScroll(View.FOCUS_DOWN);
}
Run Code Online (Sandbox Code Playgroud)
为什么比使用更糟 …
我有一个 ScrollView,里面有很多布局,逐步显示(按钮 1 显示布局 1,...)。而且,作为最后一步,我的最后一个按钮显示页脚。
每次出现布局时,我希望滚动视图滚动到底部。当我的页脚出现时也是如此。
我尝试了 Stack 上发布的解决方案,但它们对我不起作用...:
https ://stackoverflow.com/a/34866634/11656272
或binding.scroll.scrollTo(0, Int.MAX_VALUE)
我还尝试使用“findViewById”而不是绑定......
任何想法?
布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp">
<!-- scrollView -->
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/footerLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<!-- ... -->
</LinearLayout>
<!-- same for btn2, layout2, ... -->
<LinearLayout
android:id="@+id/layoutLast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<Button
android:id="@+id/btnLast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last button" />
</LinearLayout>
</LinearLayout> …Run Code Online (Sandbox Code Playgroud)