Scrollview переходит наверх при выборе элемента Spinner в ConstraintLayout - PullRequest
0 голосов
/ 26 марта 2020

У меня есть ScrollView, содержащий один ConstraintLayout, со многими Spinners и их метками.

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

Я пытался:

  • Добавление android:descendantFocusability="beforeDescendants" android:focusable="true" к просмотру прокрутки.

  • Добавление android:descendantFocusability="blocksDescendants" к макету ограничения .

  • Добавление android:focusable="false" android:focusableInTouchMode="false" к каждому представлению.
  • Добавление пустого элемента с <requestFocus/> в верхнюю часть constraintLayout

Но независимо от того, что я делаю, представление прокрутки переходит наверх после выбора элемента с вращающимся элементом.

Вот мой xml макет:

<ScrollView 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="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout

        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView 

            android:id="@+id/label2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinner1" />

        <TextView
            android:id="@+id/label5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"


            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label4" />

        <TextView 

            android:id="@+id/label6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner6"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinner5" />

        <TextView
            android:id="@+id/label8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"


            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner8"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label7" />

        <TextView 

            android:id="@+id/label4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner4"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinner3" />

        <TextView
            android:id="@+id/label7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"


            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner7"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label6" />

        <Spinner 

            android:id="@+id/spinner1"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"
            app:layout_constraintBottom_toBottomOf="@+id/label1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/label1" />

        <Spinner 

            android:id="@+id/spinner2"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"
            app:layout_constraintBottom_toBottomOf="@+id/label2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/label2" />

        <Spinner
            android:id="@+id/spinner3"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label3"
            app:layout_constraintEnd_toEndOf="parent" />

        <Spinner
            android:id="@+id/spinner5"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label5"
            app:layout_constraintEnd_toEndOf="parent" />

        <Spinner
            android:id="@+id/spinner6"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label6"
            app:layout_constraintEnd_toEndOf="parent" />

        <Spinner
            android:id="@+id/spinner7"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label7"
            app:layout_constraintEnd_toEndOf="parent" />

        <Spinner
            android:id="@+id/spinner9"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="1dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label9"
            app:layout_constraintEnd_toEndOf="parent" />

        <Spinner
            android:id="@+id/spinner8"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label8"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/label8" />

        <TextView
            android:id="@+id/label9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"


            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner9"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label8" />

        <Spinner
            android:id="@+id/spinner4"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:entries="@array/yes_or_no"


            app:layout_constraintBottom_toBottomOf="@+id/label4"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView 

            android:id="@+id/label3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner3"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/spinner2" />

        <TextView 

            android:id="@+id/label1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            android:text="Test"
            app:layout_constraintEnd_toStartOf="@+id/spinner1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

и массив

<string-array name="yes_or_no">
        <item></item>
        <item>Yes</item>
        <item>No</item>
    </string-array>```

Thank you

1 Ответ

0 голосов
/ 26 марта 2020

Создание класса ScrollBehaviour

public class ScrollBehaviour {

private final String mCategory;

private final RecyclerView mRv;

private final RecyclerView.LayoutManager mLayoutManager;

public ScrollBehaviour(String category,
                       RecyclerView rv,
                       RecyclerView.LayoutManager layoutManager) {
    this.mCategory = category;
    this.mRv = rv;
    this.mLayoutManager = layoutManager;
}

/**
 * Scroll's target {@link RecyclerView} to top if current pos is zero and user is not
 * scrolling or this was an auto update. By definition auto update can never happen
 * when user has just returned from details activity.
 *
 * @param forceTop Flag to tell if auto update just happened.
 */
public void applyAutoScrollBehaviour(boolean forceTop) {

    boolean isAtTop = isAtTop();

    if (forceTop ||
            (isAtTop && mRv.getScrollState() != RecyclerView.SCROLL_STATE_DRAGGING)) {

        Timber.v(mCategory + " => applyAutoScrollBehaviour. "
                + " AutoUpdate: " + forceTop
                + " IsAtTop: " + isAtTop);

        scrollToTop();
    }
}

/**
 * Scroll's target {@link RecyclerView} to top.
 */
public void scrollToTop() {
    mRv.scrollToPosition(0);
}

private boolean isAtTop() {

    if(mLayoutManager instanceof LinearLayoutManager){
        return ((LinearLayoutManager)mLayoutManager).findFirstCompletelyVisibleItemPosition() == 0;
    }else{
        //TODO: Handler staggered grid layout manager
        return false;
    }
}
}

Внутри вашей деятельности / фрагмента

ScrollBehaviour scrollBehaviour = ScrollBehaviour(
            "String",
            "your_recyclerview,
            "recyclerview_layoutManger");

Вызывайте метод scrollToTop () везде, где вам нужно

scrollBehaviour.scrollToTop()
...