簡體   English   中英

如何以編程方式滾動到底部(Android,kotlin)?

[英]How to programmatically scroll to bottom (Android, kotlin)?

我有一個 ScrollView,里面有很多逐步出現的布局(按鈕 1 顯示布局 1,...)。 而且,作為最后一步,我的最后一個按鈕顯示頁腳。

在布局的每次出現時,我希望滾動視圖滾動到底部。 當我的頁腳出現時也是如此。

我嘗試了在 Stack 上發布的解決方案,但它們對我不起作用......: https://stackoverflow.com/a/34866634/11656272binding.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>
   </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()
      }
   }
}

嘗試使用scroll.fullScroll

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM