タイトルの通りですが、スクロールするたびに上のViewに合わせて高さを変えたいのですが、
どうも思い通りにいかないようです。何が悪いのでしょうか。
▼やりたいこと
下図のようにスクロールによって上のviewが消えていく。
xml
1▼レイアウト activity_text.xml 2 3<?xml version="1.0" encoding="utf-8"?> 4<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content" 7 android:orientation="vertical"> 8 9 <LinearLayout 10 android:id="@+id/main_title_layout" 11 android:layout_width="match_parent" 12 android:layout_height="50dp" 13 android:background="#8080FF" 14 android:orientation="vertical"> 15 </LinearLayout> 16 17 <LinearLayout 18 android:id="@+id/content_layout" 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:layout_weight="1" 22 android:background="#80cc00" 23 android:orientation="vertical"> 24 25 <***.views.ObservableScrollView2 26 android:layout_width="match_parent" 27 android:layout_height="match_parent"> 28 29 <LinearLayout 30 android:id="@+id/scroll_layout" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" 33 android:orientation="vertical" 34 android:background="#80cc00"> 35 36 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 37 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 38 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 39 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 40 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 41 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 42 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 43 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaaaa" android:textSize="60dp"/> 44 </LinearLayout> 45 46 </***.views.ObservableScrollView2> 47 48 </LinearLayout> 49 50 51</LinearLayout> 52 53
java
1▼ScrollView継承ファイル ObservableScrollView2.java 2 3public class ObservableScrollView2 extends ScrollView { 4※コンストラクタは省略 5 @Override 6 protected void onScrollChanged(int x, int y, int oldx, int oldy) { 7 super.onScrollChanged(x, y, oldx, oldy); 8 9 View titleLayout = ((Activity)getContext()).findViewById(R.id.main_title_layout); 10 titleLayout.setY(y * (-1)); 11 ViewGroup contentLayout = (ViewGroup)((Activity)getContext()).findViewById(R.id.content_layout); 12 ViewGroup.LayoutParams marginLayoutParams = contentLayout.getLayoutParams(); 13 marginLayoutParams.height += y; // これを記述しても解決しない。さらに動きがガタガタになる。 14 contentLayout.setLayoutParams(marginLayoutParams); 15 contentLayout.setY(titleLayout.getHeight() + y * (-1)); 16 //requestLayout(); // これを記述しても解決しない。さらに動きがガタガタになる。 17 } 18 19
◆実機確認
Android 7.1.1
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。