Как программно прокрутить вниз (Android, kotlin)? - PullRequest
0 голосов
/ 06 ноября 2019

У меня есть ScrollView с множеством макетов внутри, который появляется шаг за шагом (кнопка 1 показывает макет 1, ...). И, как последний шаг, моя последняя кнопка отображает нижний колонтитул.

При каждом появлении макета я хочу, чтобы представление прокрутки прокручивалось вниз. То же самое, когда появляется мой нижний колонтитул.

Я пробовал решения, размещенные в стеке, но они не работают для меня ...: 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>
   </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 Ответ

1 голос
/ 06 ноября 2019

Попробуйте использовать scroll.fullScroll

scroll.post { 
    scroll.fullScroll(View.FOCUS_DOWN) 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...