Освой программирование играючи

Сайт Александра Климова

Шкодим

/* Моя кошка замечательно разбирается в программировании. Стоит мне объяснить проблему ей - и все становится ясно. */
John Robbins, Debugging Applications, Microsoft Press, 2000

NestedScrollView

Иногда возникает потребность разместить прокрутку в другую прокрутку. Если это сделать с ScrollView, то возникают проблемы. Поэтому позже Гугл придумал новый компонент и включил его в состав библиотеки Android Support Design.

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

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


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="8dp"
            android:paddingTop="8dp"
            android:text="Section Scrolls"
            android:textSize="18sp"/>

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            app:cardBackgroundColor="#B39DDB"
            app:cardCornerRadius="2dp"
            app:cardElevation="3dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."/>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:padding="8dp"
            app:cardBackgroundColor="#B39DDB"
            app:cardCornerRadius="2dp"
            app:cardElevation="3dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."/>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:padding="8dp"
            app:cardBackgroundColor="#B39DDB"
            app:cardCornerRadius="2dp"
            app:cardElevation="3dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."/>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="#399D2B">

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

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp"
                    android:text="Content Scrolls"
                    android:textSize="18sp"/>

                <android.support.v4.widget.NestedScrollView
                    android:layout_width="match_parent"
                    android:layout_height="96dp"
                    android:paddingBottom="16dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, esse, voluptatibus illo voluptas iure deserunt quod dolorum ipsam vero sed. Delectus, iusto rerum ipsa quidem deserunt quae itaque earum magni!\n
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, esse, voluptatibus illo voluptas iure deserunt quod dolorum ipsam vero sed. Delectus, iusto rerum ipsa quidem deserunt quae itaque earum magni!\n
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, esse, voluptatibus illo voluptas iure deserunt quod dolorum ipsam vero sed. Delectus, iusto rerum ipsa quidem deserunt quae itaque earum magni!\n
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia, esse, voluptatibus illo voluptas iure deserunt quod dolorum ipsam vero sed. Delectus, iusto rerum ipsa quidem deserunt quae itaque earum magni!\n"/>


                </android.support.v4.widget.NestedScrollView>
            </LinearLayout>

        </android.support.v7.widget.CardView>

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

Данная функциональность редко используется, но может пригодиться.

Дополнительное чтение

SIMMORSAL/ConcealerNestedScrollView: A library to make views hide from top and bottom while scrolling a custom NestedScrollView

Реклама