Home > database >  How to scroll the page using NestedScrollView to a given position. AndroidStudio
How to scroll the page using NestedScrollView to a given position. AndroidStudio

Time:11-11

How to scroll the page using NestedScrollView to a given position. I have a NestedScrollView which I set onCreateView to binding.NestedScrollView.scrollto(0,100) but nothing happens. How to fix it?

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

      binding.scrollView2.scrollTo(0,100)
}

CodePudding user response:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    android:id="@ id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="4dp"
    tools:context=".ComentThemeScreenFragment">

    <!-- TODO: Update blank fragment layout -->

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/constlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_2">


        <androidx.cardview.widget.CardView
            android:id="@ id/CardTost"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:background="@color/primary_text"
            android:backgroundTint="@color/primary_text"
            android:foregroundGravity="bottom"
            android:visibility="gone"
            app:cardCornerRadius="8dp"
            app:layout_constraintBottom_toBottomOf="@ id/scrollView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="12dp"
                android:layout_marginBottom="12dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@ id/imageView22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="4dp"
                    android:layout_weight="1"
                    android:visibility="visible"
                    app:srcCompat="@drawable/star_filled_white" />

                <TextView
                    android:id="@ id/textView42"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="@font/roboto"
                    android:text="Тема добавлена в избранное"
                    android:textColor="#FFFFFF"
                    android:textSize="16sp" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.core.widget.NestedScrollView
            android:id="@ id/scrollView2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@ id/toolbar3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/toolbar2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@ id/linearLayout8"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/background_1"
                    android:orientation="vertical"
                    android:padding="20dp">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@ id/imageView17"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="start|left"
                            android:layout_weight="1"
                            android:src="@drawable/ic_user_images_1useravatar" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="5dp"
                            android:layout_weight="1"
                            android:orientation="vertical">

                            <TextView
                                android:id="@ id/textView35"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="5dp"
                                android:layout_weight="1"
                                android:fontFamily="@font/roboto_medium"
                                android:inputType="textMultiLine"
                                android:text="TextView"
                                android:textColor="@color/primary_text"
                                android:textSize="14sp" />

                            <TextView
                                android:id="@ id/textView36"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:fontFamily="@font/roboto_medium"
                                android:text="TextView"
                                android:textColor="@color/secondary_text"
                                android:textSize="12sp" />
                        </LinearLayout>

                    </LinearLayout>

                    <TextView
                        android:id="@ id/textView37"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="10dp"
                        android:fontFamily="@font/roboto"
                        android:inputType="textMultiLine"
                        android:text="TextView"
                        android:textColor="@color/primary_text"
                        android:textSize="16sp"
                        app:lineHeight="24dp" />


                </LinearLayout>

                <LinearLayout
                    android:id="@ id/linearLayout9"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="16dp"
                    android:background="@color/background_1"
                    android:orientation="vertical"
                    android:padding="20dp">

                    <TextView
                        android:id="@ id/textView39"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="left"
                        android:layout_marginBottom="20dp"
                        android:fontFamily="@font/roboto_medium"
                        android:text="0 Комментариев"
                        android:textColor="@color/primary_text"
                        android:textSize="20sp" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@ id/RcView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />

                    <TextView
                        android:id="@ id/textView19"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto"
                        android:text="Обсуждение еще не началось Добавьте тему в избранное, и Вы сможете получать уведомления о начале беседы"
                        android:textColor="@color/secondary_text"
                        android:textSize="16sp"
                        android:visibility="gone" />

                </LinearLayout>
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@ id/toolbar2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/background_1"
            android:elevation="4dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageButton
                android:id="@ id/imageButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:backgroundTint="@color/white"
                android:foregroundGravity="left|center"
                android:src="@drawable/arrow_left" />

            <ImageButton
                android:id="@ id/imageButton6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_margin="10dp"
                android:backgroundTint="@color/white"
                android:foregroundGravity="left|center"
                android:src="@drawable/stargrey"
                tools:layout_editor_absoluteX="67dp"
                tools:layout_editor_absoluteY="10dp" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>


        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@ id/toolbar3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:elevation="4dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@ id/bottomAppBar2"
            app:layout_constraintEnd_toEndOf="@ id/bottomAppBar2"
            app:layout_constraintStart_toStartOf="@ id/bottomAppBar2">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="16dp">

                <LinearLayout
                    android:id="@ id/linearLayout10"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="10dp"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@ id/imageView21"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <TextView
                        android:id="@ id/textView40"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto_medium"
                        android:text="TextView"
                        android:textColor="@color/primary_text"
                        android:textSize="14sp" />

                    <TextView
                        android:id="@ id/textView38"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/roboto"
                        android:shadowColor="@color/secondary_text"
                        android:text="TextView"
                        android:textSize="12sp" />
                </LinearLayout>

                <ImageButton
                    android:id="@ id/imageView21"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:backgroundTint="#FFFFFF"
                    android:src="@drawable/cross_coment"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>


        </com.google.android.material.appbar.CollapsingToolbarLayout>


        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@ id/bottomAppBar2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:ignore="BottomAppBar">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="16dp">

                <EditText
                    android:id="@ id/editTextTextPersonName"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginEnd="10dp"
                    android:background="@android:drawable/divider_horizontal_dim_dark"
                    android:ems="10"
                    android:hint="Комментарий"
                    android:inputType="textMultiLine|textPersonName"
                    android:textColor="@color/primary_text"
                    android:textColorHint="@color/secondary_text"
                    android:textSize="16sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@ id/imageButton4"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageButton
                    android:id="@ id/imageButton4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:backgroundTint="#FFFFFF"
                    android:src="@drawable/send_2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </com.google.android.material.bottomappbar.BottomAppBar>


    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

CodePudding user response:

Your NestedScrollView has 0 width and 0 height so it's simply invisible. I suggest you changing it both to match_parent. Also LinearLayout inside your scrollview should has wrap_content height:

<androidx.core.widget.NestedScrollView
    android:id="@ id/scrollView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toTopOf="@ id/toolbar3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/toolbar2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        ...

    </LinearLayout>
</androidx.core.widget.NestedScrollView>

And then to scroll use this:

scrollView2.post({
   binding.scrollView2.scrollTo(0,100);
});
  • Related