2017-11-10 7 views
1

私はAndroidアプリでSnackbarを使い始めましたが、ScrollViewを小さくしてScrollViewの内側を覆わないようにすることはできません。snackbarが表示されたときにScrollViewを小さくする

これは私の活動のXMLレイアウトです:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    android:id="@+id/container" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorBackground" 
    tools:context="com.example.MainActivity"> 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="16dp" 
      android:orientation="vertical"> 

      [LOT OF ELEMENTS IN HERE] 

     </LinearLayout> 
    </ScrollView> 
</android.support.constraint.ConstraintLayout> 

私はStackOverflowの上のソリューションの多くを試みたが、まだ何も私のために働いていません。

+0

scrollViewを小さくすることはどういう意味ですか? – trocchietto

+0

さて、高さはmatch_parentに設定されていますが、snackbarが表示されたらsnackbarの高さで小さくしたいです。 – Matej

答えて

-2

私は私の問題の解決策を見つけました。これが解決策です。 ConstraintLayoutandroid.support.design.widget.CoordinatorLayoutと置き換えて、自分のアクティビティのレイアウトxmlファイルに入れました。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorBackground" 
    tools:context="com.example.MainActivity"> 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="com.example.DecreaseHeightBehavior"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:layout_marginTop="16dp" 
      android:orientation="vertical"> 

      [ELEMENTS HERE] 

     </LinearLayout> 
    </ScrollView> 
</android.support.design.widget.CoordinatorLayout> 

そして、私はこのようになりますクラスDecreaseHeightBehaviorを、作成している:これは私のために働いたが、

package com.example; 

import android.content.Context; 
import android.support.annotation.Keep; 
import android.support.design.widget.CoordinatorLayout; 
import android.support.design.widget.Snackbar; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 

@Keep 
public class DecreaseHeightBehavior extends CoordinatorLayout.Behavior<View>{ 
    public DecreaseHeightBehavior(){ 
     super(); 
    } 

    public DecreaseHeightBehavior(Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency){ 
     return dependency instanceof Snackbar.SnackbarLayout; 
    } 

    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency){ 
     ViewGroup.LayoutParams layoutParams = child.getLayoutParams(); 
     layoutParams.height = parent.getHeight() - dependency.getHeight(); 
     child.setLayoutParams(layoutParams); 

     return true; 
    } 

    @Override 
    public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency){ 
     ViewGroup.LayoutParams layoutParams = child.getLayoutParams(); 
     layoutParams.height = MATCH_PARENT; 
     child.setLayoutParams(layoutParams); 
    } 
} 

このソリューションはScrollViewの高さをアニメーション化しません。

+0

CoordinatorLayoutを使用することをお勧めします。 – trocchietto

+1

私はあなたのコーディネーターのレイアウトをお勧めした後で自分で自分のやり取りをする代わりに私の返事を受け入れるべきです、そして/または少なくともあなたは私をアップアップすることができます – trocchietto

5

私の推測では、このpageに指定されているあなたのための最善の解決策は、CoordinatorLayoutを実装することですあなたがリンクにGIF形式を見れば、あなたはスナックバーがトリガされたときにフローティングボトンを上げる方法の動画を見ることができるということですチュートリアルの浮動ボタンの代わりにScrollView要素を代用しようとすると、うまくいくと思います(ScrollViewは他の属性の上にあるはずです)。

もしうまくいかない場合は、 ScrollViewとgoogle officialのマニュアルで説明されているように必要なメソッドをオーバーライドしてください。これはいつもうまくいくはずですが、もう少し作業する必要があります。

+0

悲しいことに、私の仕事はできませんでした。 'ConstraintLayout'を' android.support.design.widget.CoordinatorLayout'に置き換え、 'app:layout_behavior =" sk.tuke.smart.makac.utils.FloatingActionButtonBehavior "を私の' ScrollView'に追加しました。 – Matej

+0

どのようなstacktraceエラーがあなたに与えますか? – trocchietto

+0

ログには何も印刷されませんが、私の問題に対する解決策が見つかりました。明日投稿します。 – Matej

関連する問題