質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

1回答

1830閲覧

Androidでスクロールするごとに高さを変えたい

yrema

総合スコア286

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2018/02/20 04:08

編集2018/02/20 16:33

タイトルの通りですが、スクロールするたびに上の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

1ScrollView継承ファイル 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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

以下のようにしてやや解決に近づいた感じなのですが問題があります。

java

1 @Override 2 protected void onScrollChanged(int x, int y, int oldx, int oldy) { 3 super.onScrollChanged(x, y, oldx, oldy); 4 5 LinearLayout titleLayout = (LinearLayout)((Activity)getContext()).findViewById(R.id.main_title_layout); 6 if (y < titleLayout.getHeight()) { 7 titleLayout.setVisibility(View.VISIBLE); 8 ViewGroup.LayoutParams lp = titleLayout.getLayoutParams(); 9 MarginLayoutParams mlp = (MarginLayoutParams) lp; 10 mlp.setMargins(mlp.leftMargin, y * (-1), mlp.rightMargin, mlp.bottomMargin); 11 titleLayout.setLayoutParams(mlp); 12 } else { 13 titleLayout.setVisibility(View.GONE); 14 } 15 }

■問題
タイトルが隠れ途中(前半のif文)の時にゆっくり動かすとガタついてしまいます。
また、以下のエラーがLogcatに出ます。

W/View: requestLayout() improperly called by android.widget.LinearLayout{4d150e1 V.E...... ......ID 0,1-720,123 #7f0e0081 app:id/main_title_layout} during layout: running second layout pass

これの解決方法が分かる方いらっしゃいましたら教えていただけないでしょうか。

投稿2018/03/04 05:32

yrema

総合スコア286

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問