[Android Studio] sticky header

노유성·2023년 7월 1일
0
post-thumbnail

🌞들어가며

스크롤을 하더라도 상단에 붙어있는 헤더를 만들고 싶을 때가 있다.
https://github.com/amarjain07/StickyScrollView
위 깃허브를 참고해서 만들었다.

⭐설정

🪐build.gradle

dependencies {
    implementation 'com.github.amarjain07:StickyScrollView:1.0.3
}

의존성에 최신 버전을 추가해준다.

🪐settings.gradle

dependencyResolutionManagement 안에 repositories 안에

maven { url "https://jitpack.io" }

을 추가해준다.

🪐xml

<?xml version="1.0" encoding="utf-8"?>
<com.amar.library.ui.StickyScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:stickyHeader="@+id/title"
    app:stickyFooter="@+id/buttons">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="vertical"
            android:padding="15dp">
            ...
        </LinearLayout>
        <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </LinearLayout>
    </LinearLayout>
</com.amar.library.ui.StickyScrollView>

위와 같이 xml을 구성해준다. StickyScrollView 태그의 속성값에 stickyHeader와 stickyFooter가 핵심이다. header에 설정해주면 상단에 고정되어서 밀리지 않고 footer에 지정해도 하단에서 밀리지 않는다.

profile
풀스택개발자가되고싶습니다:)

0개의 댓글