当前位置: 首页 > 知识库问答 >
问题:

如何以编程方式滚动到底部(Android,kotlin)?

杨凌
2023-03-14

我有一个滚动视图,里面有很多布局,一步一步地出现(按钮1显示布局1,...)。最后一步,我的最后一个按钮显示一个页脚。

在每次出现布局时,我希望scrollview滚动到底部。我的页脚出现时也一样。

布局文件

<?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>
   </ScrollView>

   <!-- footer layout -->
   <LinearLayout
      android:id="@+id/footerLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical"
      android:visibility="gone">

      <!--- ... --->
   </LinearLayout>
   <!-- footer layout - END -->
   </RelativeLayout>
</layout>

kt文件

package com.example.myapp.controller


import ...

class MainActivity : AppCompatActivity() {

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

   private lateinit var binding: com.example.myapp.databinding.ActivityMainBinding

   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

      // *** buttons 1, 2, 3, ... show layout1, layout2, ...
      binding.btn1.setOnClickListener {_ ->
         binding.layout1.visibility = VISIBLE

         binding.scroll.scrollToBottom()
      }

      // ... etc for other layouts

      // *** Main button -> show footer layout and scroll down ***
      binding.btnLast.setOnClickListener {_ ->
         binding.footerLayout.visibility = VISIBLE

         binding.scroll.scrollToBottom()
      }
   }
}

共有1个答案

经景辉
2023-03-14

尝试使用scroll.fullscroll

scroll.post { 
    scroll.fullScroll(View.FOCUS_DOWN) 
}
 类似资料:
  • 问题内容: 如何滚动到代码底部?还是以更通用的方式,到子视图的任何一点? 问题答案: 您可以使用UIScrollView的功能滚动到内容视图的任何部分。假设您的scrollView是,这是一些滚动到底部的代码: 希望有帮助!

  • 我一直在为我和一些朋友开发一个小型聊天客户端,同时自学Java和JFX8。不幸的是,由于某些原因,我无法以编程方式滚动到滚动窗格的底部,那里有聊天消息。 我有一个简单地称为scrollToBottom()的方法,它执行以下代码: 下面是应用程序中唯一真正相关的类--ChatBox类型只是VBox的扩展,它只使用扩展类型的文本。 如果您有任何其他一般性建议(我现在正在学习Java),欢迎所有提示。:

  • 问题内容: 我有一个带有用于内容的内部DIV的HTML页面。内部DIV有自己的滚动条。我想自动滚动到DIV中的某个位置。 我怎样才能做到这一点?(请注意,我不想自动滚动“窗口”滚动条-我已经知道如何执行此操作) 需要跨平台解决方案 问题答案: div具有您可以设置的属性(及其pal,)。

  • 问题内容: 使用jQuery,我想禁用主体的滚动: 我的想法是: 组 捕获当前 绑定到正文滚动事件,将scrollTop / scrollLeft设置为捕获的值。 有没有更好的办法? 更新: 我知道有人会在想“为什么他不只是在面板上使用?”。 请不要提出这个建议,因为我还有其他原因。 问题答案: 我发现执行此操作的唯一方法与您所描述的类似: 抓住当前滚动位置(不要忘记水平轴!)。 将溢出设置为隐藏

  • 我有一个作为上的面板和一个按钮。如果我按下按钮,我希望显示滚动的地图摘录。 我试图通过lat/lon设置另一个显示位置,但我不知道如何获得旧的mapcenter lat/lon值..? 我的按钮Actionlistener,它应该显示一个稍微向上移动的地图摘录: 但是我不知道如何获得地图中心当前的LAT/LON值。我如何获得它们?

  • 问题内容: 我想构建一个聊天系统,并在进入窗口和收到新消息时自动滚动到底部。如何在React中自动滚动到容器的底部? 问题答案: 正如Tushar所提到的,您可以在聊天的底部保留一个虚拟div: 然后在组件更新时滚动到它(即,随着添加新消息而更新状态): 我在这里使用标准的Element.scrollIntoView方法。