Search code examples
javaandroidlayoutscrollview

Scroll through the RecyclerView


There is such a layout:

<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/dateTV"
        tools:context=".DayTasksActivity"
        tools:layout_editor_absoluteX="1dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/RWTasksOnDay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="16dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:padding="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/RWTasksOnDay">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/taskET"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="textPersonName" />

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/addTaskBTN"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="3"
                    android:enabled="false"
                    android:text="@string/add_task" />
            </LinearLayout>


        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

</LinearLayout>

And this listener:

addTaskBTN.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String task = String.valueOf(taskET.getText());
        mDayLab.addTask(mDay, task);

        updateRecyclerView();
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

    }
});

When you click a new item is added to the RecyclerView, you need to add ScrollView scrolled to the new items, down to the bottom. With fullScroll well, it turns out to achieve the desired result, or do something wrong. Please tell me how to solve this problem.


Solution

  • What about just scroll RecyclerView ? You can scroll it to the last item with

    your_recycler.scrollToPosition(last_item_index)