やりたいこと
こんな感じで、webviewとアプリ側のヘッダーを連結させる画面を作成しようとしていました。
今までLinearLayoutでヘッダーを上部に固定し、その下にwebviewを配置していたので、このような実装ができるか調べてみた次第です。
(紫のScrollable Header
がアプリ側で作成したヘッダー。その下のGoogleのページがwebview)
解決策
ScrollViewを使うことでめちゃくちゃ簡単に解決しました。
Sample.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/purple_200"
android:text="Scrollable Header"
android:textSize="32sp" />
<WebView
android:id="@+id/top_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>
注意点
ScrollViewは子となるViewGroupを1つしか持てないので、LinearLayoutなりを配置しましょう。